From 0bbb15337fea50304e299a6660ac60a8d54ae113 Mon Sep 17 00:00:00 2001 From: Manav Rathi Date: Fri, 2 Aug 2024 14:35:11 +0530 Subject: [PATCH] Rework --- web/packages/new/photos/services/fix-exif.ts | 90 ++++++++------------ 1 file changed, 36 insertions(+), 54 deletions(-) diff --git a/web/packages/new/photos/services/fix-exif.ts b/web/packages/new/photos/services/fix-exif.ts index 9be79dcb1f..8d7452e942 100644 --- a/web/packages/new/photos/services/fix-exif.ts +++ b/web/packages/new/photos/services/fix-exif.ts @@ -6,17 +6,7 @@ import { import { FileType } from "@/media/file-type"; import downloadManager from "@/new/photos/services/download"; import type { EnteFile } from "@/new/photos/types/file"; -import { detectFileTypeInfo } from "@/new/photos/utils/detect-type"; -import { validateAndGetCreationUnixTimeInMicroSeconds } from "@ente/shared/time"; -import { getParsedExifData } from "@ente/shared/utils/exif-old"; - -const EXIF_TIME_TAGS = [ - "DateTimeOriginal", - "CreateDate", - "ModifyDate", - "DateCreated", - "MetadataDate", -]; +import { extractExifDates } from "./exif"; export type FixOption = | "date-time-original" @@ -27,65 +17,57 @@ export type FixOption = /** * Update the date associated with a given {@link enteFile}. * - * This is generally viewed as the creation date of the underlying asset + * This is generally treated as the creation date of the underlying asset * (photo, video, live photo) that this file stores. * - * - For images, this function allows us to update this date from the - * Exif and other metadata embedded in the file. + * - For images, this function allows us to update this date from the Exif and + * other metadata embedded in the file. * - * - For all types of files (including images), this function allows us to - * update this date to an explicitly provided value. + * - For all types of files (including images), this function allows us to + * update this date to an explicitly provided value. * - * If an Exif-involving {@link FixOption} is passed for an non-image file, - * then that file is just skipped over. + * If an Exif-involving {@link fixOption} is passed for an non-image file, then + * that file is just skipped over. Similarly, if an Exif-involving + * {@link fixOption} is provided, but the given underlying image for the given + * {@link enteFile} does not have a corresponding Exif (or related) value, then + * that file is skipped. * - * Note that the metadata associated with a file is immutable, and we + * Note that metadata associated with an {@link EnteFile} is immutable, and we * instead modify the mutable metadata section associated with the file. See * [Note: Metadatum] for more details. */ export const updateEnteFileDate = async ( - file: EnteFile, + enteFile: EnteFile, fixOption: FixOption, customDate: ParsedMetadataDate, ) => { - let correctCreationTime: number | null; + let newDate: ParsedMetadataDate | undefined; if (fixOption === "custom") { - correctCreationTime = customDate.timestamp; - } else { - if (file.metadata.fileType !== FileType.image) { - return; - } - const fileStream = await downloadManager.getFile(file); - const fileBlob = await new Response(fileStream).blob(); - const fileObject = new File([fileBlob], file.metadata.title); - const fileTypeInfo = await detectFileTypeInfo(fileObject); - const exifData = await getParsedExifData( - fileObject, - fileTypeInfo, - EXIF_TIME_TAGS, - ); - if (fixOption === "date-time-original") { - correctCreationTime = validateAndGetCreationUnixTimeInMicroSeconds( - exifData?.DateTimeOriginal ?? exifData?.DateCreated, - ); - } else if (fixOption === "date-time-digitized") { - correctCreationTime = validateAndGetCreationUnixTimeInMicroSeconds( - exifData?.CreateDate, - ); - } else if (fixOption === "metadata-date") { - correctCreationTime = validateAndGetCreationUnixTimeInMicroSeconds( - exifData?.MetadataDate, - ); + newDate = customDate; + } else if (enteFile.metadata.fileType == FileType.image) { + const stream = await downloadManager.getFile(enteFile); + const blob = await new Response(stream).blob(); + const file = new File([blob], enteFile.metadata.title); + const { DateTimeOriginal, DateTimeDigitized, MetadataDate, DateTime } = + await extractExifDates(file); + switch (fixOption) { + case "date-time-original": + newDate = DateTimeOriginal ?? DateTime; + break; + case "date-time-digitized": + newDate = DateTimeDigitized; + break; + case "metadata-date": + newDate = MetadataDate; + break; } } - if ( - correctCreationTime && - correctCreationTime !== file.metadata.creationTime - ) { + + if (newDate && newDate.timestamp !== enteFile.metadata.creationTime) { const updatedFile = await changeFileCreationTime( - file, - correctCreationTime, + enteFile, + newDate.timestamp, ); - updateExistingFilePubMetadata(file, updatedFile); + updateExistingFilePubMetadata(enteFile, updatedFile); } };