From 90e052fa49adb3f5f7d7f905e1b1cd16d79ebda5 Mon Sep 17 00:00:00 2001 From: Manav Rathi Date: Tue, 4 Feb 2025 07:58:43 +0530 Subject: [PATCH] Reorder --- .../photos/src/services/upload/takeout.ts | 67 ++++++++++--------- 1 file changed, 36 insertions(+), 31 deletions(-) diff --git a/web/apps/photos/src/services/upload/takeout.ts b/web/apps/photos/src/services/upload/takeout.ts index 9c5d806b2b..02fc1affe1 100644 --- a/web/apps/photos/src/services/upload/takeout.ts +++ b/web/apps/photos/src/services/upload/takeout.ts @@ -36,13 +36,13 @@ const METADATA_SUFFIX = ".supplemental-metadata"; /** * Derive a key for the given {@link jsonFileName} that should be used to index - * into the metadata JSON map. + * into the {@link ParsedMetadataJSON} JSON map. * * @param collectionID The collection to which we're uploading. * * @param jsonFileName The file name for the JSON file. * - * @returns A key suitable for indexing into the {@link ParsedMetadataJSON} map. + * @returns A key suitable for indexing into the metadata JSON map. */ export const metadataJSONMapKeyForJSON = ( collectionID: number, @@ -51,6 +51,40 @@ export const metadataJSONMapKeyForJSON = ( return `${collectionID}-${jsonFileName.slice(0, -1 * ".json".length)}`; }; +/** + * Return the matching entry, if any, from {@link parsedMetadataJSONMap} for the + * {@link fileName} and {@link collectionID} combination. + * + * This is the sibling of {@link metadataJSONMapKeyForJSON}, except for deriving + * the filename key we might have to try a bunch of different variations, so + * this does not return a single key but instead tries the combinations until it + * finds an entry in the map, and returns the found entry instead of the key. + */ +export const matchTakeoutMetadata = ( + fileName: string, + collectionID: number, + parsedMetadataJSONMap: Map, +) => { + const components = getFileNameComponents(fileName); + let key = getMetadataJSONMapKeyForFile(collectionID, components); + let takeoutMetadata = parsedMetadataJSONMap.get(key); + + if (!takeoutMetadata) { + key = getClippedMetadataJSONMapKeyForFile(collectionID, components); + takeoutMetadata = parsedMetadataJSONMap.get(key); + } + + if (!takeoutMetadata) { + key = getSupplementaryMetadataJSONMapKeyForFile( + collectionID, + components, + ); + takeoutMetadata = parsedMetadataJSONMap.get(key); + } + + return takeoutMetadata; +}; + // if the file name is greater than MAX_FILE_NAME_LENGTH_GOOGLE_EXPORT(46) , then google photos clips the file name // so we need to use the clipped file name to get the metadataJSON file export const getClippedMetadataJSONMapKeyForFile = ( @@ -210,32 +244,3 @@ const parseGTLocation = (o: unknown): Location | undefined => { */ const parseGTNonEmptyString = (o: unknown): string | undefined => o && typeof o == "string" ? o : undefined; - -/** - * Return the matching entry (if any) from {@link parsedMetadataJSONMap} for the - * {@link fileName} and {@link collectionID} combination. - */ -export const matchTakeoutMetadata = ( - fileName: string, - collectionID: number, - parsedMetadataJSONMap: Map, -) => { - const components = getFileNameComponents(fileName); - let key = getMetadataJSONMapKeyForFile(collectionID, components); - let takeoutMetadata = parsedMetadataJSONMap.get(key); - - if (!takeoutMetadata) { - key = getClippedMetadataJSONMapKeyForFile(collectionID, components); - takeoutMetadata = parsedMetadataJSONMap.get(key); - } - - if (!takeoutMetadata) { - key = getSupplementaryMetadataJSONMapKeyForFile( - collectionID, - components, - ); - takeoutMetadata = parsedMetadataJSONMap.get(key); - } - - return takeoutMetadata; -};