π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Prune
Manav Rathi6b50b24abfbb
diff --git a/web/apps/photos/src/pages/shared-albums.tsx b/web/apps/photos/src/pages/shared-albums.tsxindex b879e3eaf7..40e0dbe9bf 100644--- a/web/apps/photos/src/pages/shared-albums.tsx+++ b/web/apps/photos/src/pages/shared-albums.tsx@@ -48,7 +48,7 @@ import { downloadManager } from "ente-gallery/services/download";import { extractCollectionKeyFromShareURL } from "ente-gallery/services/share";import { updateShouldDisableCFUploadProxy } from "ente-gallery/services/upload";import type { Collection } from "ente-media/collection";-import { mergeMetadata, type EnteFile } from "ente-media/file";+import { type EnteFile } from "ente-media/file";import { verifyPublicAlbumPassword } from "ente-new/albums/services/publicCollection";import {GalleryItemsHeaderAdapter,@@ -229,10 +229,7 @@ export default function PublicCollectionGallery() {setIsPasswordProtected(isPasswordProtected);const collectionUID = getPublicCollectionUID(accessToken);const localFiles = await getLocalPublicFiles(collectionUID);- const localPublicFiles = sortFiles(- mergeMetadata(localFiles),- sortAsc,- );+ const localPublicFiles = sortFiles(localFiles, sortAsc);setPublicFiles(localPublicFiles);accessTokenJWT =await getLocalPublicCollectionPassword(collectionUID);diff --git a/web/apps/photos/src/services/publicCollectionService.ts b/web/apps/photos/src/services/publicCollectionService.tsindex 1a9d598a68..e30de3b4b0 100644--- a/web/apps/photos/src/services/publicCollectionService.ts+++ b/web/apps/photos/src/services/publicCollectionService.ts@@ -1,13 +1,14 @@import { sharedCryptoWorker } from "ente-base/crypto";import log from "ente-base/log";import { apiURL } from "ente-base/origins";+import { transformFilesIfNeeded } from "ente-gallery/services/files-db";import { type MagicMetadataCore } from "ente-gallery/services/magic-metadata";import type {Collection,CollectionPublicMagicMetadataData,} from "ente-media/collection";import type { EnteFile, RemoteEnteFile } from "ente-media/file";-import { decryptRemoteFile, mergeMetadata } from "ente-media/file";+import { decryptRemoteFile } from "ente-media/file";import { savedPublicCollections } from "ente-new/albums/services/public-albums-fdb";import { sortFiles } from "ente-new/photos/services/files";import { CustomError, parseSharingErrorCodes } from "ente-shared/error";@@ -64,7 +65,7 @@ export const getLocalPublicFiles = async (collectionUID: string) => {collectionUID: null,files: [] as EnteFile[],} as LocalSavedPublicCollectionFiles);- return localSavedPublicCollectionFiles.files;+ return transformFilesIfNeeded(localSavedPublicCollectionFiles.files);};export const savePublicCollectionFiles = async (collectionUID: string,@@ -228,7 +229,7 @@ export const syncPublicFiles = async (collectionUID,collection.updationTime,);- setPublicFiles([...sortFiles(mergeMetadata(files), sortAsc)]);+ setPublicFiles([...sortFiles(files, sortAsc)]);} catch (e) {const parsedError = parseSharingErrorCodes(e);log.error("failed to sync shared collection files", e);@@ -236,7 +237,7 @@ export const syncPublicFiles = async (throw e;}}- return [...sortFiles(mergeMetadata(files), sortAsc)];+ return [...sortFiles(files, sortAsc)];} catch (e) {log.error("failed to get local or sync shared collection files", e);throw e;@@ -291,12 +292,10 @@ const getPublicFiles = async (}setPublicFiles(sortFiles(- mergeMetadata(- [...(files || []), ...decryptedFiles].filter(- // TODO(RE):- // (item) => !item.isDeleted,- (file) => !("isDeleted" in file && file.isDeleted),- ),+ [...(files || []), ...decryptedFiles].filter(+ // TODO(RE):+ // (item) => !item.isDeleted,+ (file) => !("isDeleted" in file && file.isDeleted),),sortAsc,),diff --git a/web/packages/gallery/services/files-db.ts b/web/packages/gallery/services/files-db.tsindex 5556f1bc7c..f8c682f890 100644--- a/web/packages/gallery/services/files-db.ts+++ b/web/packages/gallery/services/files-db.ts@@ -49,12 +49,18 @@ import {RemoteCollectionUser,RemotePublicURL,} from "ente-media/collection";-import { RemoteFileInfo, RemoteFileObjectAttributes } from "ente-media/file";+import {+ RemoteFileInfo,+ RemoteFileObjectAttributes,+ transformDecryptedMetadataJSON,+ type EnteFile,+} from "ente-media/file";import {FileMetadata,FilePrivateMagicMetadataData,FilePublicMagicMetadataData,} from "ente-media/file-metadata";+import type { MagicMetadata } from "ente-media/magic-metadata";import { nullishToEmpty, nullToUndefined } from "ente-utils/transform";import { z } from "zod/v4";@@ -149,3 +155,84 @@ export const LocalEnteFile = z.looseObject({});export const LocalEnteFiles = z.array(LocalEnteFile);++/**+ * Apply transformations when reading files from the DB.+ *+ * There are two parts to it -+ *+ * 1. the required part (patching old entries that might be present in the local+ * database),+ * 2. the optional part (removing some unused fields).+ *+ * Part 1 ---+ *+ * Transform metadata in legacy files that might be present in the local+ * database. Note that this will not be needed for files that are fetched+ * afresh, since the corresponding transform is already done during+ * {@link decryptRemoteFile}; this is only for handling potentially items that+ * might've been already present locally.+ *+ * Part 2 ---+ *+ * Remove unused fields from the file objects when reading them.+ *+ * This is similar to the transformation we perform when reading collections+ * from the database, to discard fields that are no longer forwarded when we+ * parse the remote object, and thus will not be present in the local DB either+ * when going forward. They might be present for the existing entries in DB+ * though, which is why these functions are needed.+ *+ * However, since unlike collections, we don't route the files through Zod when+ * reading. So instead we do it using this function. Effectively, the end result+ * should be the same. In any case, doing this cleanup has no functional impact.+ *+ * Both parts added June 2025, 1.7.14-beta, prune eventually (tag: Migration).+ */+export const transformFilesIfNeeded = (files: EnteFile[]) =>+ isFilesTransformNeeded(files) ? files.map(transformFile) : files;++// Preflight check to turn the potentially non-trivial overhead (~50ms for 200k+// files if everything runs through transformFile) into an effective no-op+// (2-3ms) for the majority happy paths which don't need any transform.+const isFilesTransformNeeded = (+ files: (EnteFile & { isDeleted?: unknown })[],+) =>+ !!files.find(+ (file) =>+ "isDeleted" in file ||+ !file.metadata.modificationTime ||+ typeof file.metadata.fileType != "number",+ );++const transformFile = (file: EnteFile & { isDeleted?: unknown }) => {+ const {+ isDeleted,+ metadata: origMetadata,+ magicMetadata,+ pubMagicMetadata,+ ...rest+ } = file;+ ignore(isDeleted);+ // We live with the cast here since this migration code should eventually be+ // removed. The cast is needed because in the original context,+ // transformDecryptedMetadataJSON acts on arbitrary JSON objects that have+ // not yet been validated to be of type FileMetadata.+ const metadata = transformDecryptedMetadataJSON(+ file.id,+ origMetadata,+ ) as FileMetadata;+ if (magicMetadata) {+ delete (magicMetadata as MagicMetadata & { header?: unknown }).header;+ }+ if (pubMagicMetadata) {+ delete (pubMagicMetadata as MagicMetadata & { header?: unknown })+ .header;+ }+ return {+ ...rest,+ metadata,+ magicMetadata,+ pubMagicMetadata,+ } satisfies EnteFile;+};diff --git a/web/packages/media/file.ts b/web/packages/media/file.tsindex 7d445fd07f..044efc1ae1 100644--- a/web/packages/media/file.ts+++ b/web/packages/media/file.ts@@ -459,48 +459,6 @@ export const transformDecryptedMetadataJSON = (return metadataJSON;};-/**- * Update the immutable fields of an (in-memory) {@link EnteFile} with any edits- * that the user has made to their corresponding mutable metadata fields.- *- * This function updates a single file, see {@link mergeMetadata} for a- * convenience function to run it on an array of files.- */-const mergeMetadata1 = (file: EnteFile): EnteFile => {- const mutableMetadata = file.pubMagicMetadata?.data;- if (mutableMetadata) {- const { editedTime, editedName, lat, long } = mutableMetadata;- // Not needed, fileCreationTime is used instead.- if (editedTime) file.metadata.creationTime = editedTime;- // Not needed, fileFileName is used instead.- if (editedName) file.metadata.title = editedName;- // Not needed, fileLocation is used instead.- if (lat && long) {- file.metadata.latitude = lat;- file.metadata.longitude = long;- }- }-- // Moved to transformDecryptedMetadataJSON. Not needed.- if (!file.metadata.modificationTime)- file.metadata.modificationTime = file.metadata.creationTime;-- // Moved to transformDecryptedMetadataJSON. Not needed.- if (!file.metadata.fileType && file.id < 100000000)- file.metadata.fileType = FileType.image;-- return file;-};--/**- * Update the in-memory representation of an array of {@link EnteFile} to- * reflect user edits since the file was uploaded.- *- * This is a list variant of {@link mergeMetadata1}.- */-export const mergeMetadata = (files: EnteFile[]) =>- files.map((file) => mergeMetadata1(file));-/*** A short identifier for a file in log messages.*diff --git a/web/packages/new/photos/components/gallery/reducer.ts b/web/packages/new/photos/components/gallery/reducer.tsindex 84200af715..53e74e7d70 100644--- a/web/packages/new/photos/components/gallery/reducer.ts+++ b/web/packages/new/photos/components/gallery/reducer.ts@@ -5,7 +5,6 @@ import {} from "ente-gallery/services/magic-metadata";import { collectionTypes, type Collection } from "ente-media/collection";import type { EnteFile } from "ente-media/file";-import { mergeMetadata } from "ente-media/file";import {isArchivedFile,type FilePrivateMagicMetadataData,@@ -515,12 +514,8 @@ const galleryReducer: React.Reducer<GalleryState, GalleryAction> = (if (process.env.NEXT_PUBLIC_ENTE_TRACE) console.log("dispatch", action);switch (action.type) {case "mount": {- const lastSyncedNormalFiles = sortFiles(- mergeMetadata(action.normalFiles),- );- const lastSyncedHiddenFiles = sortFiles(- mergeMetadata(action.hiddenFiles),- );+ const lastSyncedNormalFiles = sortFiles(action.normalFiles);+ const lastSyncedHiddenFiles = sortFiles(action.hiddenFiles);// During mount there are no unsynced updates, and we can directly// use the provided files.@@ -721,9 +716,7 @@ const galleryReducer: React.Reducer<GalleryState, GalleryAction> = (state.unsyncedPrivateMagicMetadataUpdates,action.files,);- const lastSyncedNormalFiles = sortFiles(- mergeMetadata(action.files),- );+ const lastSyncedNormalFiles = sortFiles(action.files);const normalFiles = deriveFiles(lastSyncedNormalFiles,unsyncedPrivateMagicMetadataUpdates,@@ -743,10 +736,8 @@ const galleryReducer: React.Reducer<GalleryState, GalleryAction> = (action.files,);const lastSyncedNormalFiles = sortFiles(- mergeMetadata(- getLatestVersionFiles(- state.lastSyncedNormalFiles.concat(action.files),- ),+ getLatestVersionFiles(+ state.lastSyncedNormalFiles.concat(action.files),),);const normalFiles = deriveFiles(@@ -786,9 +777,7 @@ const galleryReducer: React.Reducer<GalleryState, GalleryAction> = (state.unsyncedPrivateMagicMetadataUpdates,action.files,);- const lastSyncedHiddenFiles = sortFiles(- mergeMetadata(action.files),- );+ const lastSyncedHiddenFiles = sortFiles(action.files);const hiddenFiles = deriveFiles(lastSyncedHiddenFiles,unsyncedPrivateMagicMetadataUpdates,@@ -808,10 +797,8 @@ const galleryReducer: React.Reducer<GalleryState, GalleryAction> = (action.files,);const lastSyncedHiddenFiles = sortFiles(- mergeMetadata(- getLatestVersionFiles(- state.lastSyncedHiddenFiles.concat(action.files),- ),+ getLatestVersionFiles(+ state.lastSyncedHiddenFiles.concat(action.files),),);const hiddenFiles = deriveFiles(diff --git a/web/packages/new/photos/services/export-migration.ts b/web/packages/new/photos/services/export-migration.tsindex 466add9e5a..6ed083c63b 100644--- a/web/packages/new/photos/services/export-migration.ts+++ b/web/packages/new/photos/services/export-migration.ts@@ -10,7 +10,7 @@ import log from "ente-base/log";import { exportMetadataDirectoryName } from "ente-gallery/export-dirs";import { downloadManager } from "ente-gallery/services/download";import type { Collection } from "ente-media/collection";-import { mergeMetadata, type EnteFile } from "ente-media/file";+import { type EnteFile } from "ente-media/file";import { fileFileName } from "ente-media/file-metadata";import { FileType } from "ente-media/file-type";import { decodeLivePhoto } from "ente-media/live-photo";@@ -140,7 +140,7 @@ async function migrationV0ToV1(}const collectionIDPathMap = new Map<number, string>();const user: User = getData("user");- const localFiles = mergeMetadata(await savedFiles());+ const localFiles = await savedFiles();const localCollections = await getLocalCollections();const personalFiles = getIDBasedSortedFiles(localFiles.filter((file) => file.ownerID == user.id),@@ -179,7 +179,7 @@ async function migrationV2ToV3(return;}const user: User = getData("user");- const localFiles = mergeMetadata(await savedFiles());+ const localFiles = await savedFiles();const personalFiles = getIDBasedSortedFiles(localFiles.filter((file) => file.ownerID == user.id),);diff --git a/web/packages/new/photos/services/export.ts b/web/packages/new/photos/services/export.tsindex e5c39a9baa..116e0d88f1 100644--- a/web/packages/new/photos/services/export.ts+++ b/web/packages/new/photos/services/export.ts@@ -15,7 +15,7 @@ import {import { downloadManager } from "ente-gallery/services/download";import { writeStream } from "ente-gallery/utils/native-stream";import type { Collection } from "ente-media/collection";-import { fileLogID, mergeMetadata, type EnteFile } from "ente-media/file";+import { fileLogID, type EnteFile } from "ente-media/file";import {fileCreationTime,fileFileName,@@ -357,7 +357,7 @@ class ExportService {{ resync }: ExportOpts,) {try {- const files = mergeMetadata(await savedFiles());+ const files = await savedFiles();const collections = await savedCollections();const exportRecord = await this.getExportRecord(exportFolder);diff --git a/web/packages/new/photos/services/files.ts b/web/packages/new/photos/services/files.tsindex 854b84f6fe..f3bcaacf44 100644--- a/web/packages/new/photos/services/files.ts+++ b/web/packages/new/photos/services/files.ts@@ -5,7 +5,6 @@ import { apiURL } from "ente-base/origins";import type { Collection } from "ente-media/collection";import {decryptRemoteFile,- mergeMetadata,type EnteFile,type RemoteEnteFile,} from "ente-media/file";@@ -247,14 +246,12 @@ export const enteFileDeletionDate = (file: TrashedEnteFile) =>export function getTrashedFiles(trash: Trash): TrashedEnteFile[] {return sortTrashFiles(- mergeMetadata(- trash.map((trashedFile) => ({- ...trashedFile.file,- updationTime: trashedFile.updatedAt,- deleteBy: trashedFile.deleteBy,- isTrashed: true,- })),- ),+ trash.map((trashedFile) => ({+ ...trashedFile.file,+ updationTime: trashedFile.updatedAt,+ deleteBy: trashedFile.deleteBy,+ isTrashed: true,+ })),);}diff --git a/web/packages/new/photos/services/ml/cluster.ts b/web/packages/new/photos/services/ml/cluster.tsindex 716e9e3946..ff7590f1a5 100644--- a/web/packages/new/photos/services/ml/cluster.ts+++ b/web/packages/new/photos/services/ml/cluster.ts@@ -2,6 +2,7 @@ import { assertionFailed } from "ente-base/assert";import { newNonSecureID } from "ente-base/id-worker";import log from "ente-base/log";import type { EnteFile } from "ente-media/file";+import { fileCreationTime } from "ente-media/file-metadata";import { wait } from "ente-utils/promise";import {pullUserEntities,@@ -16,7 +17,6 @@ import {type FaceIndex,} from "./face";import { dotProduct } from "./math";-import { fileCreationTime } from "ente-media/file-metadata";/*** A face cluster is an set of faces, and a nanoid to uniquely identify it.diff --git a/web/packages/new/photos/services/photos-fdb.ts b/web/packages/new/photos/services/photos-fdb.tsindex be55ad3dc7..bf054be6f1 100644--- a/web/packages/new/photos/services/photos-fdb.ts+++ b/web/packages/new/photos/services/photos-fdb.ts@@ -2,11 +2,12 @@* @file Photos app specific files DB. See: [Note: Files DB].*/-import { LocalCollections } from "ente-gallery/services/files-db";-import { ignore, type Collection } from "ente-media/collection";-import { transformDecryptedMetadataJSON, type EnteFile } from "ente-media/file";-import type { FileMetadata } from "ente-media/file-metadata";-import type { MagicMetadata } from "ente-media/magic-metadata";+import {+ LocalCollections,+ transformFilesIfNeeded,+} from "ente-gallery/services/files-db";+import { type Collection } from "ente-media/collection";+import { type EnteFile } from "ente-media/file";import localForage from "ente-shared/storage/localForage";import { z } from "zod/v4";@@ -166,84 +167,3 @@ export const savedHiddenFiles = async (): Promise<EnteFile[]> =>*/export const saveHiddenFiles = (files: EnteFile[]) =>localForage.setItem("hidden-files", transformFilesIfNeeded(files));--/**- * Apply transformations when reading files from the DB.- *- * There are two parts to it -- *- * 1. the required part (patching old entries that might be present in the local- * database),- * 2. the optional part (removing some unused fields).- *- * Part 1 ---- *- * Transform metadata in legacy files that might be present in the local- * database. Note that this will not be needed for files that are fetched- * afresh, since the corresponding transform is already done during- * {@link decryptRemoteFile}; this is only for handling potentially items that- * might've been already present locally.- *- * Part 2 ---- *- * Remove unused fields from the file objects when reading them.- *- * This is similar to the transformation we perform when reading collections- * from the database, to discard fields that are no longer forwarded when we- * parse the remote object, and thus will not be present in the local DB either- * when going forward. They might be present for the existing entries in DB- * though, which is why these functions are needed.- *- * However, since unlike collections, we don't route the files through Zod when- * reading. So instead we do it using this function. Effectively, the end result- * should be the same. In any case, doing this cleanup has no functional impact.- *- * Both parts added June 2025, 1.7.14-beta, prune eventually (tag: Migration).- */-const transformFilesIfNeeded = (files: EnteFile[]) =>- isFilesTransformNeeded(files) ? files.map(transformFile) : files;--// Preflight check to turn the potentially non-trivial overhead (~50ms for 200k-// files if everything runs through transformFile) into an effective no-op-// (2-3ms) for the majority happy paths which don't need any transform.-const isFilesTransformNeeded = (- files: (EnteFile & { isDeleted?: unknown })[],-) =>- !!files.find(- (file) =>- "isDeleted" in file ||- !file.metadata.modificationTime ||- typeof file.metadata.fileType != "number",- );--const transformFile = (file: EnteFile & { isDeleted?: unknown }) => {- const {- isDeleted,- metadata: origMetadata,- magicMetadata,- pubMagicMetadata,- ...rest- } = file;- ignore(isDeleted);- // We live with the cast here since this migration code should eventually be- // removed. The cast is needed because in the original context,- // transformDecryptedMetadataJSON acts on arbitrary JSON objects that have- // not yet been validated to be of type FileMetadata.- const metadata = transformDecryptedMetadataJSON(- file.id,- origMetadata,- ) as FileMetadata;- if (magicMetadata) {- delete (magicMetadata as MagicMetadata & { header?: unknown }).header;- }- if (pubMagicMetadata) {- delete (pubMagicMetadata as MagicMetadata & { header?: unknown })- .header;- }- return {- ...rest,- metadata,- magicMetadata,- pubMagicMetadata,- } satisfies EnteFile;-};