Skip to content

armgit / ente

Public Imported from GitHub
0

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

tx

Manav Rathi65bf785ccedd
diff --git a/web/packages/gallery/services/files-db.ts b/web/packages/gallery/services/files-db.ts
index cb0e6e8f9f..5556f1bc7c 100644
--- a/web/packages/gallery/services/files-db.ts
+++ b/web/packages/gallery/services/files-db.ts
@@ -6,10 +6,14 @@
* Prior to us using idb for accessing IndexedDB, we used localForage (another
* IndexedDB library) for that purpose (See `docs/storage.md` for more context).
*
- * Our use of localForage was limited to a single IndexedDB database named
- * "ente-files" with a single (pertinent) table named "files". It stored more
- * than files though - files, collections, trash, their corresponding sync
- * times, and other bits and bobs.
+ * Our use of localForage was (and is) limited to:
+ *
+ * - A single IndexedDB database named "ente-files" containing
+ *
+ * - A single table named "files"
+ *
+ * It stored more than files though - files, collections, trash, their
+ * corresponding sync times, and other bits and bobs related to them.
*
* Since we've now switched to idb as our preferred IndexedDB library, the data
* stored in this files table could be considered legacy in a sense. But such
diff --git a/web/packages/media/file.ts b/web/packages/media/file.ts
index 9291247c81..6b498e09ad 100644
--- a/web/packages/media/file.ts
+++ b/web/packages/media/file.ts
@@ -466,7 +466,7 @@ const transformDecryptedMetadataJSON = (
* This function updates a single file, see {@link mergeMetadata} for a
* convenience function to run it on an array of files.
*/
-export const mergeMetadata1 = (file: EnteFile): EnteFile => {
+const mergeMetadata1 = (file: EnteFile): EnteFile => {
const mutableMetadata = file.pubMagicMetadata?.data;
if (mutableMetadata) {
const { editedTime, editedName, lat, long } = mutableMetadata;
diff --git a/web/packages/new/photos/services/files.ts b/web/packages/new/photos/services/files.ts
index eab9cc3cd1..cf67961b2a 100644
--- a/web/packages/new/photos/services/files.ts
+++ b/web/packages/new/photos/services/files.ts
@@ -18,15 +18,18 @@ import {
getCollectionLastSyncTime,
setCollectionLastSyncTime,
} from "./collections";
-import { savedFiles, savedHiddenFiles, savedNormalFiles } from "./photos-fdb";
-
-const FILES_TABLE = "files";
-const HIDDEN_FILES_TABLE = "hidden-files";
+import {
+ savedFiles,
+ savedHiddenFiles,
+ savedNormalFiles,
+ saveHiddenFiles,
+ saveNormalFiles,
+} from "./photos-fdb";
/**
* Return all files that we know about locally, both "normal" and "hidden".
*
- * Deprecated. Use {@link savedFiles} instead.
+ * Deprecated, use {@link savedFiles} instead.
*/
export const getAllLocalFiles = savedFiles;
@@ -35,7 +38,7 @@ export const getAllLocalFiles = savedFiles;
* "normal" (i.e. non-"hidden") files, but it can be passed the {@link type}
* "hidden" to get it to instead return hidden files that we know about locally.
*
- * Deprecated. Use {@link savedNormalFiles} or {@link savedHiddenFiles} instead.
+ * Deprecated, use {@link savedNormalFiles} or {@link savedHiddenFiles} instead.
*/
export const getLocalFiles = async (type: "normal" | "hidden" = "normal") =>
type == "normal" ? savedNormalFiles() : savedHiddenFiles();
@@ -44,14 +47,11 @@ export const getLocalFiles = async (type: "normal" | "hidden" = "normal") =>
* Update the files that we know about locally.
*
* Sibling of {@link getLocalFiles}.
+ *
+ * Deprecate, use {@link saveNormalFiles} or {@link saveHiddenFiles} instead.
*/
-export const setLocalFiles = async (
- type: "normal" | "hidden",
- files: EnteFile[],
-) => {
- const tableName = type == "normal" ? FILES_TABLE : HIDDEN_FILES_TABLE;
- await localForage.setItem(tableName, files);
-};
+export const setLocalFiles = (type: "normal" | "hidden", files: EnteFile[]) =>
+ type == "normal" ? saveNormalFiles(files) : saveHiddenFiles(files);
/**
* Fetch all files of the given {@link type}, belonging to the given
diff --git a/web/packages/new/photos/services/photos-fdb.ts b/web/packages/new/photos/services/photos-fdb.ts
index 30e0425730..3d68fd919b 100644
--- a/web/packages/new/photos/services/photos-fdb.ts
+++ b/web/packages/new/photos/services/photos-fdb.ts
@@ -3,8 +3,9 @@
*/
import { LocalCollections } from "ente-gallery/services/files-db";
-import { type Collection } from "ente-media/collection";
+import { ignore, type Collection } from "ente-media/collection";
import type { EnteFile } from "ente-media/file";
+import type { MagicMetadata } from "ente-media/magic-metadata";
import localForage from "ente-shared/storage/localForage";
import { z } from "zod/v4";
@@ -136,6 +137,14 @@ export const savedNormalFiles = async (): Promise<EnteFile[]> =>
// and (b) mark all such cases with the title of this note.
(await localForage.getItem<EnteFile[]>("files")) ?? [];
+/**
+ * Replace the list of files stored in our local database.
+ *
+ * This is the setter corresponding to {@link savedNormalFiles}.
+ */
+export const saveNormalFiles = (files: EnteFile[]) =>
+ localForage.setItem("files", transformFilesIfNeeded(files));
+
/**
* Return all hidden files present in our local database.
*
@@ -144,3 +153,42 @@ export const savedNormalFiles = async (): Promise<EnteFile[]> =>
export const savedHiddenFiles = async (): Promise<EnteFile[]> =>
// See: [Note: Avoiding Zod parsing for large DB arrays]
(await localForage.getItem<EnteFile[]>("hidden-files")) ?? [];
+
+/**
+ * Replace the list of files stored in our local database.
+ *
+ * This is the setter corresponding to {@link savedNormalFiles}.
+ */
+export const saveHiddenFiles = (files: EnteFile[]) =>
+ localForage.setItem("hidden-files", transformFilesIfNeeded(files));
+
+/**
+ * Remove unused fields from the file objects when saving 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 when we next write. Effectively, the end result
+ * should be the same. In any case, doing this cleanup has no functional impact.
+ *
+ * Added June 2025, 1.7.14-beta, can be removed after a bit (tag: Migration).
+ */
+const transformFilesIfNeeded = (files: EnteFile[]) =>
+ files.map((f) => ("isDeleted" in f ? transformFile(f) : f));
+
+const transformFile = (file: EnteFile & { isDeleted?: unknown }) => {
+ const { isDeleted, magicMetadata, pubMagicMetadata, ...rest } = file;
+ ignore(isDeleted);
+ if (magicMetadata) {
+ delete (magicMetadata as MagicMetadata & { header?: unknown }).header;
+ }
+ if (pubMagicMetadata) {
+ delete (pubMagicMetadata as MagicMetadata & { header?: unknown })
+ .header;
+ }
+ return { ...rest, magicMetadata, pubMagicMetadata };
+};