move updateFile creation time exif logic in else block and wrap the function in try catch block

This commit is contained in:
Abhinav 2023-02-10 16:48:38 +05:30
parent 5856fbd06c
commit a19ff7d216

View file

@ -48,6 +48,7 @@ export async function downloadFile(
token?: string,
passwordToken?: string
) {
try {
let fileBlob: Blob;
const fileReader = new FileReader();
if (accessedThroughSharedURL) {
@ -67,7 +68,9 @@ export async function downloadFile(
fileBlob = await (await fetch(fileURL)).blob();
}
} else {
const fileURL = await DownloadManager.getCachedOriginalFile(file)[0];
const fileURL = await DownloadManager.getCachedOriginalFile(
file
)[0];
if (!fileURL) {
fileBlob = await new Response(
await DownloadManager.downloadFile(file)
@ -77,12 +80,34 @@ export async function downloadFile(
}
}
if (file.metadata.fileType === FILE_TYPE.LIVE_PHOTO) {
const motionPhoto = await decodeMotionPhoto(file, fileBlob);
const image = new File(
[motionPhoto.image],
motionPhoto.imageNameTitle
);
const imageType = await getFileType(image);
const tempImageURL = URL.createObjectURL(
new Blob([motionPhoto.image], { type: imageType.mimeType })
);
const video = new File(
[motionPhoto.video],
motionPhoto.videoNameTitle
);
const videoType = await getFileType(video);
const tempVideoURL = URL.createObjectURL(
new Blob([motionPhoto.video], { type: videoType.mimeType })
);
downloadUsingAnchor(tempImageURL, motionPhoto.imageNameTitle);
downloadUsingAnchor(tempVideoURL, motionPhoto.videoNameTitle);
} else {
const fileType = await getFileType(
new File([fileBlob], file.metadata.title)
);
if (
file.pubMagicMetadata?.data.editedTime &&
(fileType.exactType === TYPE_JPEG || fileType.exactType === TYPE_JPG)
(fileType.exactType === TYPE_JPEG ||
fileType.exactType === TYPE_JPG)
) {
fileBlob = await updateFileCreationDateInEXIF(
fileReader,
@ -90,29 +115,13 @@ export async function downloadFile(
new Date(file.pubMagicMetadata.data.editedTime / 1000)
);
}
let tempImageURL: string;
let tempVideoURL: string;
let tempURL: string;
if (file.metadata.fileType === FILE_TYPE.LIVE_PHOTO) {
const motionPhoto = await decodeMotionPhoto(file, fileBlob);
const image = new File([motionPhoto.image], motionPhoto.imageNameTitle);
const imageType = await getFileType(image);
tempImageURL = URL.createObjectURL(
new Blob([motionPhoto.image], { type: imageType.mimeType })
);
const video = new File([motionPhoto.video], motionPhoto.videoNameTitle);
const videoType = await getFileType(video);
tempVideoURL = URL.createObjectURL(
new Blob([motionPhoto.video], { type: videoType.mimeType })
);
downloadUsingAnchor(tempImageURL, motionPhoto.imageNameTitle);
downloadUsingAnchor(tempVideoURL, motionPhoto.videoNameTitle);
} else {
fileBlob = new Blob([fileBlob], { type: fileType.mimeType });
tempURL = URL.createObjectURL(fileBlob);
const tempURL = URL.createObjectURL(fileBlob);
downloadUsingAnchor(tempURL, file.metadata.title);
}
} catch (e) {
logError(e, 'failed to download file');
}
}
function downloadUsingAnchor(link: string, name: string) {