From 1e25a7e6f70a18b675a5883a12b45ddfadfaee61 Mon Sep 17 00:00:00 2001 From: Abhinav Date: Mon, 5 Jun 2023 17:37:21 +0530 Subject: [PATCH 01/10] extract image width and height from exif and store it in metadata --- .../photos/src/services/upload/exifService.ts | 68 ++++++++++++++++++- .../src/services/upload/metadataService.ts | 20 ++++++ apps/photos/src/types/upload/index.ts | 9 +++ 3 files changed, 95 insertions(+), 2 deletions(-) diff --git a/apps/photos/src/services/upload/exifService.ts b/apps/photos/src/services/upload/exifService.ts index 8fa5b9cc4..d77d386c1 100644 --- a/apps/photos/src/services/upload/exifService.ts +++ b/apps/photos/src/services/upload/exifService.ts @@ -17,6 +17,8 @@ type ParsedEXIFData = Record & DateCreated: Date; latitude: number; longitude: number; + imageWidth: number; + imageHeight: number; }>; type RawEXIFData = Record & @@ -29,6 +31,8 @@ type RawEXIFData = Record & GPSLongitude: number[]; GPSLatitudeRef: string; GPSLongitudeRef: string; + ImageWidth: number; + ImageHeight: number; }>; export async function getParsedExifData( @@ -76,8 +80,19 @@ function parseExifData(exifData: RawEXIFData): ParsedEXIFData { if (!exifData) { return null; } - const { DateTimeOriginal, CreateDate, ModifyDate, DateCreated, ...rest } = - exifData; + const { + DateTimeOriginal, + CreateDate, + ModifyDate, + DateCreated, + ImageHeight, + ImageWidth, + ExifImageHeight, + ExifImageWidth, + PixelXDimension, + PixelYDimension, + ...rest + } = exifData; const parsedExif: ParsedEXIFData = { ...rest }; if (DateTimeOriginal) { parsedExif.DateTimeOriginal = parseEXIFDate(exifData.DateTimeOriginal); @@ -101,6 +116,55 @@ function parseExifData(exifData: RawEXIFData): ParsedEXIFData { parsedExif.latitude = parsedLocation.latitude; parsedExif.longitude = parsedLocation.longitude; } + if (ImageWidth && ImageHeight) { + if (typeof ImageWidth === 'number' && typeof ImageHeight === 'number') { + parsedExif.imageWidth = ImageWidth; + parsedExif.imageHeight = ImageHeight; + } else { + logError( + new Error('ImageWidth or ImageHeight is not a number'), + 'Image dimension parsing failed', + { + ImageWidth, + ImageHeight, + } + ); + } + } else if (ExifImageWidth && ExifImageHeight) { + if ( + typeof ExifImageWidth === 'number' && + typeof ExifImageHeight === 'number' + ) { + parsedExif.imageWidth = ExifImageWidth; + parsedExif.imageHeight = ExifImageHeight; + } else { + logError( + new Error('ExifImageWidth or ExifImageHeight is not a number'), + 'Image dimension parsing failed', + { + ExifImageWidth, + ExifImageHeight, + } + ); + } + } else if (PixelXDimension && PixelYDimension) { + if ( + typeof PixelXDimension === 'number' && + typeof PixelYDimension === 'number' + ) { + parsedExif.imageWidth = PixelXDimension; + parsedExif.imageHeight = PixelYDimension; + } else { + logError( + new Error('PixelXDimension or PixelYDimension is not a number'), + 'Image dimension parsing failed', + { + PixelXDimension, + PixelYDimension, + } + ); + } + } return parsedExif; } diff --git a/apps/photos/src/services/upload/metadataService.ts b/apps/photos/src/services/upload/metadataService.ts index cd11c20c9..c1b3d79b1 100644 --- a/apps/photos/src/services/upload/metadataService.ts +++ b/apps/photos/src/services/upload/metadataService.ts @@ -8,6 +8,7 @@ import { FileTypeInfo, ParsedExtractedMetadata, ElectronFile, + Dimensions, } from 'types/upload'; import { NULL_EXTRACTED_METADATA, NULL_LOCATION } from 'constants/upload'; import { getVideoMetadata } from './videoMetadataService'; @@ -40,6 +41,12 @@ const EXIF_TAGS_NEEDED = [ 'GPSLatitudeRef', 'GPSLongitudeRef', 'DateCreated', + 'ExifImageWidth', + 'ExifImageHeight', + 'ImageWidth', + 'ImageHeight', + 'PixelXDimension', + 'PixelYDimension', ]; export async function extractMetadata( @@ -55,6 +62,14 @@ export async function extractMetadata( } const fileHash = await getFileHash(worker, receivedFile); + let dimensions: Dimensions; + if (extractedMetadata.width && extractedMetadata.height) { + dimensions = { + width: extractedMetadata.width, + height: extractedMetadata.height, + }; + } + const metadata: Metadata = { title: receivedFile.name, creationTime: @@ -66,6 +81,8 @@ export async function extractMetadata( longitude: extractedMetadata.location.longitude, fileType: fileTypeInfo.fileType, hash: fileHash, + w: dimensions.width, + h: dimensions.height, }; return metadata; } @@ -90,9 +107,12 @@ export async function getImageMetadata( fileTypeInfo, EXIF_TAGS_NEEDED ); + imageMetadata = { location: getEXIFLocation(exifData), creationTime: getEXIFTime(exifData), + width: exifData?.imageWidth, + height: exifData?.imageHeight, }; } catch (e) { logError(e, 'getExifData failed'); diff --git a/apps/photos/src/types/upload/index.ts b/apps/photos/src/types/upload/index.ts index edf3b2f6b..4063be4f4 100644 --- a/apps/photos/src/types/upload/index.ts +++ b/apps/photos/src/types/upload/index.ts @@ -27,6 +27,8 @@ export interface Metadata { hash?: string; imageHash?: string; videoHash?: string; + w?: number; + h?: number; } export interface Location { @@ -34,6 +36,11 @@ export interface Location { longitude: number; } +export interface Dimensions { + width: number; + height: number; +} + export interface ParsedMetadataJSON { creationTime: number; modificationTime: number; @@ -137,6 +144,8 @@ export interface UploadFile extends BackupedFile { export interface ParsedExtractedMetadata { location: Location; creationTime: number; + width: number; + height: number; } // This is used to prompt the user the make upload strategy choice From 90f00b8b0d5b8a547963fc0565b6bee3b1cc8c1e Mon Sep 17 00:00:00 2001 From: Abhinav Date: Mon, 5 Jun 2023 18:00:31 +0530 Subject: [PATCH 02/10] added width and height info in newer uploaded files --- apps/photos/src/constants/upload.ts | 2 ++ .../src/services/upload/metadataService.ts | 17 ++++------------- apps/photos/src/utils/ffmpeg/index.ts | 2 ++ 3 files changed, 8 insertions(+), 13 deletions(-) diff --git a/apps/photos/src/constants/upload.ts b/apps/photos/src/constants/upload.ts index d12bb2255..6ec37fb73 100644 --- a/apps/photos/src/constants/upload.ts +++ b/apps/photos/src/constants/upload.ts @@ -85,6 +85,8 @@ export const LIVE_PHOTO_ASSET_SIZE_LIMIT = 20 * 1024 * 1024; // 20MB export const NULL_EXTRACTED_METADATA: ParsedExtractedMetadata = { location: NULL_LOCATION, creationTime: null, + width: null, + height: null, }; export const A_SEC_IN_MICROSECONDS = 1e6; diff --git a/apps/photos/src/services/upload/metadataService.ts b/apps/photos/src/services/upload/metadataService.ts index c1b3d79b1..e3924f981 100644 --- a/apps/photos/src/services/upload/metadataService.ts +++ b/apps/photos/src/services/upload/metadataService.ts @@ -8,7 +8,6 @@ import { FileTypeInfo, ParsedExtractedMetadata, ElectronFile, - Dimensions, } from 'types/upload'; import { NULL_EXTRACTED_METADATA, NULL_LOCATION } from 'constants/upload'; import { getVideoMetadata } from './videoMetadataService'; @@ -62,14 +61,6 @@ export async function extractMetadata( } const fileHash = await getFileHash(worker, receivedFile); - let dimensions: Dimensions; - if (extractedMetadata.width && extractedMetadata.height) { - dimensions = { - width: extractedMetadata.width, - height: extractedMetadata.height, - }; - } - const metadata: Metadata = { title: receivedFile.name, creationTime: @@ -81,8 +72,8 @@ export async function extractMetadata( longitude: extractedMetadata.location.longitude, fileType: fileTypeInfo.fileType, hash: fileHash, - w: dimensions.width, - h: dimensions.height, + w: extractedMetadata.width, + h: extractedMetadata.height, }; return metadata; } @@ -111,8 +102,8 @@ export async function getImageMetadata( imageMetadata = { location: getEXIFLocation(exifData), creationTime: getEXIFTime(exifData), - width: exifData?.imageWidth, - height: exifData?.imageHeight, + width: exifData?.imageWidth ?? null, + height: exifData?.imageHeight ?? null, }; } catch (e) { logError(e, 'getExifData failed'); diff --git a/apps/photos/src/utils/ffmpeg/index.ts b/apps/photos/src/utils/ffmpeg/index.ts index 916806c3e..80673c907 100644 --- a/apps/photos/src/utils/ffmpeg/index.ts +++ b/apps/photos/src/utils/ffmpeg/index.ts @@ -38,6 +38,8 @@ export function parseFFmpegExtractedMetadata(encodedMetadata: Uint8Array) { latitude: location.latitude, longitude: location.longitude, }, + width: null, + height: null, }; return parsedMetadata; } From e9f8a2d24e168f0a55bd70d05de8d22799a5e21b Mon Sep 17 00:00:00 2001 From: Abhinav Date: Mon, 5 Jun 2023 18:02:24 +0530 Subject: [PATCH 03/10] remove unneeded type --- apps/photos/src/types/upload/index.ts | 5 ----- 1 file changed, 5 deletions(-) diff --git a/apps/photos/src/types/upload/index.ts b/apps/photos/src/types/upload/index.ts index 4063be4f4..8dd225b63 100644 --- a/apps/photos/src/types/upload/index.ts +++ b/apps/photos/src/types/upload/index.ts @@ -36,11 +36,6 @@ export interface Location { longitude: number; } -export interface Dimensions { - width: number; - height: number; -} - export interface ParsedMetadataJSON { creationTime: number; modificationTime: number; From d0e0428ed817218b599054fc870e25a5dd559e62 Mon Sep 17 00:00:00 2001 From: Abhinav Date: Mon, 5 Jun 2023 18:22:22 +0530 Subject: [PATCH 04/10] update error message --- apps/photos/src/utils/error/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/photos/src/utils/error/index.ts b/apps/photos/src/utils/error/index.ts index cbdfe3811..feee70709 100644 --- a/apps/photos/src/utils/error/index.ts +++ b/apps/photos/src/utils/error/index.ts @@ -30,7 +30,7 @@ export const CustomError = { INVALID_COLLECTION_OPERATION: 'invalid collection operation', TO_MOVE_FILES_FROM_MULTIPLE_COLLECTIONS: 'to move files from multiple collections', - WAIT_TIME_EXCEEDED: 'thumbnail generation wait time exceeded', + WAIT_TIME_EXCEEDED: 'operation wait time exceeded', REQUEST_CANCELLED: 'request canceled', REQUEST_FAILED: 'request failed', TOKEN_EXPIRED: 'token expired', From 6a8a6cae764670bef4c473afc99695f25f6bdcfe Mon Sep 17 00:00:00 2001 From: Abhinav Date: Mon, 5 Jun 2023 18:22:41 +0530 Subject: [PATCH 05/10] added fileDimensionExtractionCheck --- apps/photos/tests/upload.test.ts | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/apps/photos/tests/upload.test.ts b/apps/photos/tests/upload.test.ts index 53a0fc6ae..c078d4cbb 100644 --- a/apps/photos/tests/upload.test.ts +++ b/apps/photos/tests/upload.test.ts @@ -23,6 +23,7 @@ export async function testUpload() { await thumbnailGenerationFailedFilesCheck(expectedState); await livePhotoClubbingCheck(expectedState); await exifDataParsingCheck(expectedState); + await fileDimensionExtractionCheck(expectedState); await googleMetadataReadingCheck(expectedState); await totalFileCountCheck(expectedState); } catch (e) { @@ -210,6 +211,33 @@ async function exifDataParsingCheck(expectedState) { console.log('exif data parsing check passed ✅'); } +async function fileDimensionExtractionCheck(expectedState) { + const files = await getLocalFiles(); + Object.entries(expectedState['file_dimensions']).map( + ([fileName, dimensions]) => { + const matchingFile = files.find( + (file) => file.metadata.title === fileName + ); + if (!matchingFile) { + throw Error( + `fileDimensionExtractionCheck failed , ${fileName} missing` + ); + } + if ( + dimensions['width'] && + dimensions['width'] !== matchingFile.metadata.w && + dimensions['height'] && + dimensions['height'] !== matchingFile.metadata.h + ) { + throw Error(`fileDimensionExtractionCheck failed ❌ , + for ${fileName} + expected: ${dimensions['width']} x ${dimensions['height']} got: ${matchingFile.metadata.w} x ${matchingFile.metadata.h}`); + } + } + ); + console.log('file dimension extraction check passed ✅'); +} + async function googleMetadataReadingCheck(expectedState) { const files = await getLocalFiles(); Object.entries(expectedState['google_import']).map( From d61d788d1e0b412b852e86971be518142e90a99b Mon Sep 17 00:00:00 2001 From: Abhinav Date: Mon, 5 Jun 2023 23:22:17 +0530 Subject: [PATCH 06/10] update ExtractMetadata to return both metadata and publicMagicMetadata --- .../photos/src/services/upload/fileService.ts | 11 +++++---- .../src/services/upload/livePhotoService.ts | 23 ++++++++++++------- .../src/services/upload/metadataService.ts | 8 +++++-- .../src/services/upload/uploadService.ts | 4 ++-- apps/photos/src/services/upload/uploader.ts | 19 +++++++++------ apps/photos/src/types/magicMetadata/index.ts | 2 ++ apps/photos/src/types/upload/index.ts | 8 +++++-- 7 files changed, 49 insertions(+), 26 deletions(-) diff --git a/apps/photos/src/services/upload/fileService.ts b/apps/photos/src/services/upload/fileService.ts index 38a32455e..ca3c5dc55 100644 --- a/apps/photos/src/services/upload/fileService.ts +++ b/apps/photos/src/services/upload/fileService.ts @@ -2,12 +2,12 @@ import { MULTIPART_PART_SIZE, FILE_READER_CHUNK_SIZE } from 'constants/upload'; import { FileTypeInfo, FileInMemory, - Metadata, EncryptedFile, FileWithMetadata, ParsedMetadataJSONMap, DataStream, ElectronFile, + ExtractMetadataResult, } from 'types/upload'; import { splitFilenameAndExtension } from 'utils/file'; import { logError } from 'utils/sentry'; @@ -74,13 +74,14 @@ export async function extractFileMetadata( collectionID: number, fileTypeInfo: FileTypeInfo, rawFile: File | ElectronFile -) { +): Promise { const originalName = getFileOriginalName(rawFile); const googleMetadata = parsedMetadataJSONMap.get( getMetadataJSONMapKey(collectionID, originalName) ) ?? {}; - const extractedMetadata: Metadata = await extractMetadata( + + const { metadata, publicMagicMetadata } = await extractMetadata( worker, rawFile, fileTypeInfo @@ -90,9 +91,9 @@ export async function extractFileMetadata( if (!value) { continue; } - extractedMetadata[key] = value; + metadata[key] = value; } - return extractedMetadata; + return { metadata, publicMagicMetadata }; } export async function encryptFile( diff --git a/apps/photos/src/services/upload/livePhotoService.ts b/apps/photos/src/services/upload/livePhotoService.ts index 98a21a803..fd345a345 100644 --- a/apps/photos/src/services/upload/livePhotoService.ts +++ b/apps/photos/src/services/upload/livePhotoService.ts @@ -8,6 +8,7 @@ import { FileWithCollection, LivePhotoAssets, ParsedMetadataJSONMap, + ExtractMetadataResult, } from 'types/upload'; import { CustomError } from 'utils/error'; import { getFileTypeFromExtensionForLivePhotoClustering } from 'utils/file/livePhoto'; @@ -51,12 +52,15 @@ export async function extractLivePhotoMetadata( collectionID: number, fileTypeInfo: FileTypeInfo, livePhotoAssets: LivePhotoAssets -) { +): Promise { const imageFileTypeInfo: FileTypeInfo = { fileType: FILE_TYPE.IMAGE, exactType: fileTypeInfo.imageType, }; - const imageMetadata = await extractFileMetadata( + const { + metadata: imageMetadata, + publicMagicMetadata: imagePublicMagicMetadata, + } = await extractFileMetadata( worker, parsedMetadataJSONMap, collectionID, @@ -65,12 +69,15 @@ export async function extractLivePhotoMetadata( ); const videoHash = await getFileHash(worker, livePhotoAssets.video); return { - ...imageMetadata, - title: getLivePhotoName(livePhotoAssets), - fileType: FILE_TYPE.LIVE_PHOTO, - imageHash: imageMetadata.hash, - videoHash: videoHash, - hash: undefined, + metadata: { + ...imageMetadata, + title: getLivePhotoName(livePhotoAssets), + fileType: FILE_TYPE.LIVE_PHOTO, + imageHash: imageMetadata.hash, + videoHash: videoHash, + hash: undefined, + }, + publicMagicMetadata: imagePublicMagicMetadata, }; } diff --git a/apps/photos/src/services/upload/metadataService.ts b/apps/photos/src/services/upload/metadataService.ts index e3924f981..e480745aa 100644 --- a/apps/photos/src/services/upload/metadataService.ts +++ b/apps/photos/src/services/upload/metadataService.ts @@ -8,6 +8,7 @@ import { FileTypeInfo, ParsedExtractedMetadata, ElectronFile, + ExtractMetadataResult as ExtractMetadataResult, } from 'types/upload'; import { NULL_EXTRACTED_METADATA, NULL_LOCATION } from 'constants/upload'; import { getVideoMetadata } from './videoMetadataService'; @@ -19,6 +20,7 @@ import { import { getFileHash } from './hashService'; import { Remote } from 'comlink'; import { DedicatedCryptoWorker } from 'worker/crypto.worker'; +import { FilePublicMagicMetadataProps } from 'types/magicMetadata'; interface ParsedMetadataJSONWithTitle { title: string; @@ -52,7 +54,7 @@ export async function extractMetadata( worker: Remote, receivedFile: File | ElectronFile, fileTypeInfo: FileTypeInfo -) { +): Promise { let extractedMetadata: ParsedExtractedMetadata = NULL_EXTRACTED_METADATA; if (fileTypeInfo.fileType === FILE_TYPE.IMAGE) { extractedMetadata = await getImageMetadata(receivedFile, fileTypeInfo); @@ -72,10 +74,12 @@ export async function extractMetadata( longitude: extractedMetadata.location.longitude, fileType: fileTypeInfo.fileType, hash: fileHash, + }; + const publicMagicMetadata: FilePublicMagicMetadataProps = { w: extractedMetadata.width, h: extractedMetadata.height, }; - return metadata; + return { metadata, publicMagicMetadata }; } export async function getImageMetadata( diff --git a/apps/photos/src/services/upload/uploadService.ts b/apps/photos/src/services/upload/uploadService.ts index 8d143869f..35947cb26 100644 --- a/apps/photos/src/services/upload/uploadService.ts +++ b/apps/photos/src/services/upload/uploadService.ts @@ -7,11 +7,11 @@ import { CustomError, handleUploadError } from 'utils/error'; import { BackupedFile, EncryptedFile, + ExtractMetadataResult, FileTypeInfo, FileWithCollection, FileWithMetadata, isDataStream, - Metadata, ParsedMetadataJSON, ParsedMetadataJSONMap, ProcessedFile, @@ -109,7 +109,7 @@ class UploadService { { isLivePhoto, file, livePhotoAssets }: UploadAsset, collectionID: number, fileTypeInfo: FileTypeInfo - ): Promise { + ): Promise { return isLivePhoto ? extractLivePhotoMetadata( worker, diff --git a/apps/photos/src/services/upload/uploader.ts b/apps/photos/src/services/upload/uploader.ts index 61f30e2ee..bec61cb7f 100644 --- a/apps/photos/src/services/upload/uploader.ts +++ b/apps/photos/src/services/upload/uploader.ts @@ -55,12 +55,13 @@ export default async function uploader( ); addLogLine(`extracting metadata ${fileNameSize}`); - const metadata = await UploadService.extractAssetMetadata( - worker, - uploadAsset, - collection.id, - fileTypeInfo - ); + const { metadata, publicMagicMetadata } = + await UploadService.extractAssetMetadata( + worker, + uploadAsset, + collection.id, + fileTypeInfo + ); const matchingExistingFiles = findMatchingExistingFiles( existingFiles, @@ -117,7 +118,11 @@ export default async function uploader( metadata.hasStaticThumbnail = true; } let pubMagicMetadata: FilePublicMagicMetadata; - if (uploaderName) { + if (publicMagicMetadata) { + pubMagicMetadata = await uploadService.constructPublicMagicMetadata( + publicMagicMetadata + ); + } else if (uploaderName) { pubMagicMetadata = await uploadService.constructPublicMagicMetadata( { uploaderName } ); diff --git a/apps/photos/src/types/magicMetadata/index.ts b/apps/photos/src/types/magicMetadata/index.ts index 14c5dcae9..3a1c5cdec 100644 --- a/apps/photos/src/types/magicMetadata/index.ts +++ b/apps/photos/src/types/magicMetadata/index.ts @@ -24,6 +24,8 @@ export interface FilePublicMagicMetadataProps { editedName?: string; caption?: string; uploaderName?: string; + w?: number; + h?: number; } export interface FilePublicMagicMetadata diff --git a/apps/photos/src/types/upload/index.ts b/apps/photos/src/types/upload/index.ts index 8dd225b63..f33c686f8 100644 --- a/apps/photos/src/types/upload/index.ts +++ b/apps/photos/src/types/upload/index.ts @@ -5,6 +5,7 @@ import { MetadataFileAttributes, S3FileAttributes } from 'types/file'; import { EncryptedMagicMetadata, FilePublicMagicMetadata, + FilePublicMagicMetadataProps, } from 'types/magicMetadata'; export interface DataStream { @@ -27,8 +28,6 @@ export interface Metadata { hash?: string; imageHash?: string; videoHash?: string; - w?: number; - h?: number; } export interface Location { @@ -155,3 +154,8 @@ export interface PublicUploadProps { passwordToken: string; accessedThroughSharedURL: boolean; } + +export interface ExtractMetadataResult { + metadata: Metadata; + publicMagicMetadata: FilePublicMagicMetadataProps; +} From 6e651ae71e3684e92db66dd7639502b0cc390b50 Mon Sep 17 00:00:00 2001 From: Abhinav Date: Mon, 5 Jun 2023 23:26:06 +0530 Subject: [PATCH 07/10] fix test --- apps/photos/tests/upload.test.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/apps/photos/tests/upload.test.ts b/apps/photos/tests/upload.test.ts index c078d4cbb..d7f26fda9 100644 --- a/apps/photos/tests/upload.test.ts +++ b/apps/photos/tests/upload.test.ts @@ -225,13 +225,13 @@ async function fileDimensionExtractionCheck(expectedState) { } if ( dimensions['width'] && - dimensions['width'] !== matchingFile.metadata.w && + dimensions['width'] !== matchingFile.pubMagicMetadata.data.w && dimensions['height'] && - dimensions['height'] !== matchingFile.metadata.h + dimensions['height'] !== matchingFile.pubMagicMetadata.data.h ) { throw Error(`fileDimensionExtractionCheck failed ❌ , for ${fileName} - expected: ${dimensions['width']} x ${dimensions['height']} got: ${matchingFile.metadata.w} x ${matchingFile.metadata.h}`); + expected: ${dimensions['width']} x ${dimensions['height']} got: ${matchingFile.pubMagicMetadata.data.w} x ${matchingFile.pubMagicMetadata.data.h}`); } } ); From 7c81d3f6c9bd21f504e7b227e967dad30c90e954 Mon Sep 17 00:00:00 2001 From: Abhinav Date: Mon, 5 Jun 2023 23:40:26 +0530 Subject: [PATCH 08/10] fix constructPublicMagicMetadata --- apps/photos/src/services/upload/uploader.ts | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/apps/photos/src/services/upload/uploader.ts b/apps/photos/src/services/upload/uploader.ts index bec61cb7f..1ae675760 100644 --- a/apps/photos/src/services/upload/uploader.ts +++ b/apps/photos/src/services/upload/uploader.ts @@ -20,7 +20,6 @@ import uploadCancelService from './uploadCancelService'; import { Remote } from 'comlink'; import { DedicatedCryptoWorker } from 'worker/crypto.worker'; import uploadService from './uploadService'; -import { FilePublicMagicMetadata } from 'types/magicMetadata'; interface UploadResponse { fileUploadResult: UPLOAD_RESULT; @@ -117,16 +116,13 @@ export default async function uploader( if (file.hasStaticThumbnail) { metadata.hasStaticThumbnail = true; } - let pubMagicMetadata: FilePublicMagicMetadata; - if (publicMagicMetadata) { - pubMagicMetadata = await uploadService.constructPublicMagicMetadata( - publicMagicMetadata - ); - } else if (uploaderName) { - pubMagicMetadata = await uploadService.constructPublicMagicMetadata( - { uploaderName } - ); - } + + const pubMagicMetadata = + await uploadService.constructPublicMagicMetadata({ + ...publicMagicMetadata, + uploaderName, + }); + const fileWithMetadata: FileWithMetadata = { localID, filedata: file.filedata, From a629542b7e5862416617c44845c4e20bc3a06a56 Mon Sep 17 00:00:00 2001 From: Abhinav Date: Tue, 6 Jun 2023 11:20:13 +0530 Subject: [PATCH 09/10] add check for removing null or undefined value from magicMetadataProps --- apps/photos/src/utils/magicMetadata/index.ts | 32 ++++++++++++-------- 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/apps/photos/src/utils/magicMetadata/index.ts b/apps/photos/src/utils/magicMetadata/index.ts index 3883330ca..6218873c0 100644 --- a/apps/photos/src/utils/magicMetadata/index.ts +++ b/apps/photos/src/utils/magicMetadata/index.ts @@ -1,6 +1,7 @@ import { Collection } from 'types/collection'; import { EnteFile } from 'types/file'; import { + FileMagicMetadata, FileMagicMetadataProps, MagicMetadataCore, VISIBILITY_STATE, @@ -37,20 +38,27 @@ export async function updateMagicMetadata( decryptionKey )) as FileMagicMetadataProps; } - if (magicMetadataUpdates) { - // copies the existing magic metadata properties of the files and updates the visibility value - // The expected behavior while updating magic metadata is to let the existing property as it is and update/add the property you want - const magicMetadataProps: FileMagicMetadataProps = { - ...originalMagicMetadata.data, - ...magicMetadataUpdates, - }; + // copies the existing magic metadata properties of the files and updates the visibility value + // The expected behavior while updating magic metadata is to let the existing property as it is and update/add the property you want + const magicMetadataProps: FileMagicMetadataProps = { + ...originalMagicMetadata.data, + ...magicMetadataUpdates, + }; - return { + const nonEmptyMagicMetadataProps = Object.fromEntries( + Object.entries(magicMetadataProps).filter( + // eslint-disable-next-line @typescript-eslint/no-unused-vars + ([_, v]) => v !== null && v !== undefined + ) + ); + + let pubMagicMetadata: FileMagicMetadata; + if (Object.values(nonEmptyMagicMetadataProps)?.length > 0) { + pubMagicMetadata = { ...originalMagicMetadata, - data: magicMetadataProps, - count: Object.keys(magicMetadataProps).length, + data: nonEmptyMagicMetadataProps, + count: Object.keys(nonEmptyMagicMetadataProps).length, }; - } else { - return originalMagicMetadata; } + return pubMagicMetadata; } From 5255152883f6913443842ef697eba4d30c5fdabb Mon Sep 17 00:00:00 2001 From: Abhinav Date: Tue, 6 Jun 2023 12:30:19 +0530 Subject: [PATCH 10/10] fix type issues --- apps/photos/src/services/upload/metadataService.ts | 2 +- apps/photos/src/types/file/index.ts | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/apps/photos/src/services/upload/metadataService.ts b/apps/photos/src/services/upload/metadataService.ts index 92fec3fe7..19c351a6b 100644 --- a/apps/photos/src/services/upload/metadataService.ts +++ b/apps/photos/src/services/upload/metadataService.ts @@ -20,7 +20,7 @@ import { import { getFileHash } from './hashService'; import { Remote } from 'comlink'; import { DedicatedCryptoWorker } from 'worker/crypto.worker'; -import { FilePublicMagicMetadataProps } from 'types/magicMetadata'; +import { FilePublicMagicMetadataProps } from 'types/file'; interface ParsedMetadataJSONWithTitle { title: string; diff --git a/apps/photos/src/types/file/index.ts b/apps/photos/src/types/file/index.ts index 7514ce628..62455f34c 100644 --- a/apps/photos/src/types/file/index.ts +++ b/apps/photos/src/types/file/index.ts @@ -93,6 +93,8 @@ export interface FilePublicMagicMetadataProps { editedName?: string; caption?: string; uploaderName?: string; + w?: number; + h?: number; } export type FilePublicMagicMetadata =