Working picture uploads. Videos failing due to URI.
This commit is contained in:
@@ -20,6 +20,7 @@ export const handleUpload = async (ev, context) => {
|
||||
try {
|
||||
|
||||
const imageData = await MediaLibrary.getAssetInfoAsync(mediaId);
|
||||
|
||||
const newFile = await (
|
||||
await fetch(imageData.localUri || imageData.uri)
|
||||
).blob();
|
||||
@@ -149,30 +150,33 @@ export const uploadToImgproxy = async (
|
||||
},
|
||||
};
|
||||
|
||||
const formData = new FormData();
|
||||
formData.append("file", {
|
||||
uri: imageData.localUri,
|
||||
type: fileType,
|
||||
name: file.data.name,
|
||||
});
|
||||
|
||||
try {
|
||||
const s3UploadResponse = await cleanAxios.put(
|
||||
preSignedUploadUrlToS3,
|
||||
file,
|
||||
options
|
||||
);
|
||||
await new Promise((resolve, reject) => {
|
||||
const xhr = new XMLHttpRequest();
|
||||
xhr.open("PUT", preSignedUploadUrlToS3);
|
||||
xhr.setRequestHeader("Content-Type", fileType);
|
||||
|
||||
if (s3UploadResponse.status !== 200) {
|
||||
console.log(
|
||||
"Error uploading to cloudinary.",
|
||||
s3UploadResponse.statusText,
|
||||
s3UploadResponse
|
||||
);
|
||||
if (onError) onError(s3UploadResponse.statusText);
|
||||
return { success: false, error: s3UploadResponse.statusText };
|
||||
}
|
||||
//debugger;
|
||||
xhr.upload.onprogress = (event) => {
|
||||
if (onProgress && event.lengthComputable) {
|
||||
onProgress({ percent: event.loaded / event.total, loaded: event.loaded });
|
||||
}
|
||||
};
|
||||
|
||||
xhr.onload = () => {
|
||||
if (xhr.status === 200) {
|
||||
resolve();
|
||||
} else {
|
||||
reject(new Error(`Upload failed: ${xhr.statusText}`));
|
||||
}
|
||||
};
|
||||
|
||||
xhr.onerror = () => {
|
||||
reject(new Error("Network error"));
|
||||
};
|
||||
|
||||
xhr.send(file);
|
||||
});
|
||||
} catch (error) {
|
||||
console.log("Error uploading to S3", error.message, error.stack);
|
||||
if (onError) onError(error.message);
|
||||
@@ -372,12 +376,16 @@ export function DetermineFileType(filetype) {
|
||||
}
|
||||
|
||||
export function formatBytes(a, b = 2) {
|
||||
if (0 === a || !a) return "0 Bytes";
|
||||
if (0 === a || !a || isNaN(a)) return "0 Bytes";
|
||||
const c = 0 > b ? 0 : b,
|
||||
d = Math.floor(Math.log(a) / Math.log(1024));
|
||||
|
||||
const parsedFloat = parseFloat((a / Math.pow(1024, d)).toFixed(c))
|
||||
if (isNaN(parsedFloat)) {
|
||||
return "0 Bytes";
|
||||
}
|
||||
return (
|
||||
parseFloat((a / Math.pow(1024, d)).toFixed(c)) +
|
||||
parsedFloat +
|
||||
" " +
|
||||
["Bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"][d]
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user