Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Move the catch up

Manav Rathi09036bb57fdb
diff --git a/web/apps/photos/src/services/export/index.ts b/web/apps/photos/src/services/export/index.ts
index 651526203b..4400471c31 100644
--- a/web/apps/photos/src/services/export/index.ts
+++ b/web/apps/photos/src/services/export/index.ts
@@ -36,7 +36,7 @@ import {
getCollectionUserFacingName,
getNonEmptyPersonalCollections,
} from "utils/collection";
-import { getPersonalFiles, streamWithUpdatedExif } from "utils/file";
+import { getPersonalFiles, updateExifIfNeeded } from "utils/file";
import { getAllLocalCollections } from "../collectionService";
import { migrateExport } from "./migration";
@@ -970,7 +970,7 @@ class ExportService {
try {
const fileUID = getExportRecordFileUID(file);
const originalFileStream = await downloadManager.getFile(file);
- const updatedFileStream = await streamWithUpdatedExif(
+ const updatedFileStream = await updateExifIfNeeded(
file,
originalFileStream,
);
diff --git a/web/apps/photos/src/utils/file/index.ts b/web/apps/photos/src/utils/file/index.ts
index 77144ade45..3b13c2febe 100644
--- a/web/apps/photos/src/utils/file/index.ts
+++ b/web/apps/photos/src/utils/file/index.ts
@@ -74,7 +74,7 @@ export async function downloadFile(file: EnteFile) {
new File([fileBlob], file.metadata.title),
);
fileBlob = await new Response(
- await streamWithUpdatedExif(file, fileBlob.stream()),
+ await updateExifIfNeeded(file, fileBlob.stream()),
).blob();
fileBlob = new Blob([fileBlob], { type: fileType.mimeType });
const tempURL = URL.createObjectURL(fileBlob);
@@ -107,23 +107,32 @@ export async function downloadFile(file: EnteFile) {
* @returns A new {@link ReadableStream} with updates if any updates were
* needed, otherwise return the original stream.
*/
-export const streamWithUpdatedExif = async (
+export const updateExifIfNeeded = async (
enteFile: EnteFile,
stream: ReadableStream<Uint8Array>,
): Promise<ReadableStream<Uint8Array>> => {
- const extension = lowercaseExtension(enteFile.metadata.title);
- if (
- enteFile.metadata.fileType === FILE_TYPE.IMAGE &&
- enteFile.pubMagicMetadata?.data.editedTime &&
- (extension == "jpeg" || extension == "jpg")
- ) {
- const fileBlob = await new Response(stream).blob();
- const updatedFileBlob = await setJPEGExifDateTimeOriginal(
- fileBlob,
+ // Not an image.
+ if (enteFile.metadata.fileType != FILE_TYPE.IMAGE) return stream;
+ // Time was not edited.
+ if (!enteFile.pubMagicMetadata?.data.editedTime) return stream;
+
+ const fileName = enteFile.metadata.title;
+ const extension = lowercaseExtension(fileName);
+ // Not a JPEG (likely).
+ if (extension != "jpeg" && extension != "jpg") return stream;
+
+ try {
+ const updatedBlob = await setJPEGExifDateTimeOriginal(
+ await new Response(stream).blob(),
new Date(enteFile.pubMagicMetadata.data.editedTime / 1000),
);
- return updatedFileBlob.stream();
- } else {
+ return updatedBlob.stream();
+ } catch (e) {
+ // We used the file's extension to determine if this was a JPEG, but
+ // this is not a guarantee. Misnamed files, while rare, do exist. So
+ // in case of errors, return the original back instead of causing the
+ // entire download or export to fail.
+ log.error(`Failed to modify Exif date for ${fileName}`, e);
return stream;
}
};
@@ -491,7 +500,7 @@ async function downloadFileDesktop(
const fs = electron.fs;
const stream = await DownloadManager.getFile(file);
- const updatedStream = await streamWithUpdatedExif(file, stream);
+ const updatedStream = await updateExifIfNeeded(file, stream);
if (file.metadata.fileType === FILE_TYPE.LIVE_PHOTO) {
const fileBlob = await new Response(updatedStream).blob();
diff --git a/web/packages/new/photos/services/exif-update.ts b/web/packages/new/photos/services/exif-update.ts
index afc591510a..7440d7ce7a 100644
--- a/web/packages/new/photos/services/exif-update.ts
+++ b/web/packages/new/photos/services/exif-update.ts
@@ -1,4 +1,3 @@
-import log from "@/base/log";
import piexif from "piexifjs";
/**
@@ -14,31 +13,23 @@ import piexif from "piexifjs";
*/
export const setJPEGExifDateTimeOriginal = async (
jpegBlob: Blob,
- updatedDate: Date,
+ date: Date,
) => {
- try {
- let imageDataURL = await blobToDataURL(jpegBlob);
- // Since we pass a Blob without an associated type, we get back a
- // generic data URL like "data:application/octet-stream;base64,...".
- // Modify it to have a `image/jpeg` MIME type.
- imageDataURL =
- "data:image/jpeg;base64" +
- imageDataURL.slice(imageDataURL.indexOf(","));
- const exifObj = piexif.load(imageDataURL);
- if (!exifObj.Exif) exifObj.Exif = {};
- exifObj.Exif[piexif.ExifIFD.DateTimeOriginal] =
- convertToExifDateFormat(updatedDate);
- log.debug(() => [
- "updateFileCreationDateInEXIF",
- { updatedDate, exifObj },
- ]);
- const exifBytes = piexif.dump(exifObj);
- const exifInsertedFile = piexif.insert(exifBytes, imageDataURL);
- return dataURLToBlob(exifInsertedFile);
- } catch (e) {
- log.error("updateFileModifyDateInEXIF failed", e);
- return jpegBlob;
- }
+ let dataURL = await blobToDataURL(jpegBlob);
+ // Since we pass a Blob without an associated type, we get back a generic
+ // data URL of the form "data:application/octet-stream;base64,...".
+ //
+ // Modify it to have a `image/jpeg` MIME type.
+ dataURL = "data:image/jpeg;base64" + dataURL.slice(dataURL.indexOf(","));
+
+ const exifObj = piexif.load(dataURL);
+ if (!exifObj.Exif) exifObj.Exif = {};
+ exifObj.Exif[piexif.ExifIFD.DateTimeOriginal] =
+ convertToExifDateFormat(date);
+ const exifBytes = piexif.dump(exifObj);
+ const exifInsertedFile = piexif.insert(exifBytes, dataURL);
+
+ return dataURLToBlob(exifInsertedFile);
};
/**