Skip to content

armgit / ente

Public Imported from GitHub
0

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

rework

Manav Rathib092fcaf29eb
diff --git a/web/packages/new/photos/services/collections.ts b/web/packages/new/photos/services/collections.ts
index 38e7c63798..004ff946e5 100644
--- a/web/packages/new/photos/services/collections.ts
+++ b/web/packages/new/photos/services/collections.ts
@@ -8,11 +8,9 @@ import { apiURL } from "ente-base/origins";
import { type Collection } from "ente-media/collection";
import { decryptRemoteFile, type EnteFile } from "ente-media/file";
import {
- getLocalTrash,
getTrashedFiles,
- TRASH,
- type EncryptedTrashItem,
- type Trash,
+ type RemoteTrashItem,
+ type TrashItem,
} from "ente-new/photos/services/trash";
import HTTPService from "ente-shared/network/HTTPService";
import localForage from "ente-shared/storage/localForage";
@@ -25,7 +23,9 @@ import {
import {
savedCollections,
savedTrashItemCollectionKeys,
+ savedTrashItems,
saveTrashItemCollectionKeys,
+ saveTrashItems,
} from "./photos-fdb";
const COLLECTION_TABLE = "collections";
@@ -151,7 +151,7 @@ export async function syncTrash(
onUpdateTrashFiles: ((files: EnteFile[]) => void) | undefined,
onPruneDeletedFileIDs: (deletedFileIDs: Set<number>) => Promise<void>,
): Promise<void> {
- const trash = await getLocalTrash();
+ const trash = await savedTrashItems();
const sinceTime = await getLastTrashSyncTime();
// Data structures:
@@ -180,7 +180,7 @@ export async function syncTrash(
collectionKeyByID.set(id, key);
}
- let updatedTrash: Trash = [...trash];
+ let updatedTrash: TrashItem[] = [...trash];
try {
let time = sinceTime;
@@ -197,7 +197,7 @@ export async function syncTrash(
);
const deletedFileIDs = new Set<number>();
// #Perf: This can be optimized by running the decryption in parallel
- for (const trashItem of resp.data.diff as EncryptedTrashItem[]) {
+ for (const trashItem of resp.data.diff as RemoteTrashItem[]) {
const collectionID = trashItem.file.collectionID;
let collectionKey = collectionKeyByID.get(collectionID);
if (!collectionKey) {
@@ -235,7 +235,7 @@ export async function syncTrash(
if (deletedFileIDs.size > 0) {
await onPruneDeletedFileIDs(deletedFileIDs);
}
- await localForage.setItem(TRASH, updatedTrash);
+ await saveTrashItems(updatedTrash);
await localForage.setItem(TRASH_TIME, time);
} while (resp.data.hasMore);
} catch (e) {
@@ -275,5 +275,5 @@ export const emptyTrash = async () => {
};
export const clearLocalTrash = async () => {
- await localForage.setItem(TRASH, []);
+ await saveTrashItems([]);
};
diff --git a/web/packages/new/photos/services/photos-fdb.ts b/web/packages/new/photos/services/photos-fdb.ts
index bf054be6f1..e2f0064d07 100644
--- a/web/packages/new/photos/services/photos-fdb.ts
+++ b/web/packages/new/photos/services/photos-fdb.ts
@@ -4,12 +4,14 @@
import {
LocalCollections,
+ LocalEnteFile,
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";
+import type { TrashItem } from "./trash";
/**
* Return all collections present in our local database.
@@ -38,8 +40,9 @@ export const savedCollections = async (): Promise<Collection[]> =>
* collections (the split between normal and hidden is not at the database level
* but is a filter when they are accessed).
*/
-export const saveCollections = (collections: Collection[]) =>
- localForage.setItem("collections", collections);
+export const saveCollections = async (collections: Collection[]) => {
+ await localForage.setItem("collections", collections);
+};
const TrashItemCollectionKey = z.object({
/**
@@ -103,8 +106,11 @@ export const savedTrashItemCollectionKeys = async (): Promise<
*
* This is the setter corresponding to {@link saveTrashItemCollectionKeys}.
*/
-export const saveTrashItemCollectionKeys = (cks: TrashItemCollectionKey[]) =>
- localForage.setItem("deleted-collection", cks);
+export const saveTrashItemCollectionKeys = async (
+ cks: TrashItemCollectionKey[],
+) => {
+ await localForage.setItem("deleted-collection", cks);
+};
/**
* Return all files present in our local database.
@@ -146,8 +152,9 @@ export const savedNormalFiles = async (): Promise<EnteFile[]> =>
*
* This is the setter corresponding to {@link savedNormalFiles}.
*/
-export const saveNormalFiles = (files: EnteFile[]) =>
- localForage.setItem("files", transformFilesIfNeeded(files));
+export const saveNormalFiles = async (files: EnteFile[]) => {
+ await localForage.setItem("files", transformFilesIfNeeded(files));
+};
/**
* Return all hidden files present in our local database.
@@ -165,5 +172,33 @@ export const savedHiddenFiles = async (): Promise<EnteFile[]> =>
*
* This is the setter corresponding to {@link savedNormalFiles}.
*/
-export const saveHiddenFiles = (files: EnteFile[]) =>
- localForage.setItem("hidden-files", transformFilesIfNeeded(files));
+export const saveHiddenFiles = async (files: EnteFile[]) => {
+ await localForage.setItem("hidden-files", transformFilesIfNeeded(files));
+};
+
+/**
+ * Zod schema for a trash entry saved in our local database.
+ */
+const LocalTrashItem = z.looseObject({
+ file: LocalEnteFile,
+ updatedAt: z.number(),
+ deleteBy: z.number(),
+});
+
+/**
+ * Return all trash entries present in our local database.
+ *
+ * Use {@link saveTrashItems} to update the database
+ */
+export const savedTrashItems = async (): Promise<TrashItem[]> =>
+ LocalTrashItem.array().parse(
+ (await localForage.getItem("file-trash")) ?? [],
+ );
+/**
+ * Replace the list of trash items stored in our local database.
+ *
+ * This is the setter corresponding to {@link savedTrashItems}.
+ */
+export const saveTrashItems = async (trashItems: TrashItem[]) => {
+ await localForage.setItem("file-trash", trashItems);
+};
diff --git a/web/packages/new/photos/services/trash.ts b/web/packages/new/photos/services/trash.ts
index f14c794e0e..f590dae3af 100644
--- a/web/packages/new/photos/services/trash.ts
+++ b/web/packages/new/photos/services/trash.ts
@@ -1,13 +1,35 @@
-import type { EnteFile, RemoteEnteFile } from "ente-media/file";
+import { RemoteEnteFile, type EnteFile } from "ente-media/file";
import { fileCreationTime } from "ente-media/file-metadata";
-import localForage from "ente-shared/storage/localForage";
+import { z } from "zod/v4";
+import { savedTrashItems } from "./photos-fdb";
-export interface TrashItem extends Omit<EncryptedTrashItem, "file"> {
+/**
+ * A trash item indicates a file in trash.
+ *
+ * On being deleted by the user, files move to trash, and gain this associated
+ * trash item, which we can fetch with correspoding diff APIs etc. Files will be
+ * permanently deleted after 30 days of being moved to trash, but can be
+ * restored or permanently deleted before that by explicit user action.
+ *
+ * See: [Note: File lifecycle]
+ */
+export interface TrashItem {
file: EnteFile;
+ /**
+ * Timestamp (epoch microseconds) when the trash entry was last updated.
+ */
+ updatedAt: number;
+ /**
+ * Timestamp (epoch microseconds) when the file will be permanently deleted.
+ */
+ deleteBy: number;
}
-export interface EncryptedTrashItem {
- file: RemoteEnteFile;
+/**
+ * Zod schema for a trash item that we receive from remote.
+ */
+const RemoteTrashItem = z.looseObject({
+ file: RemoteEnteFile,
/**
* `true` if the file no longer in trash because it was permanently deleted.
*
@@ -15,7 +37,7 @@ export interface EncryptedTrashItem {
* diff. It indicates that the file which was previously in trash is no
* longer in the trash because it was permanently deleted.
*/
- isDeleted: boolean;
+ isDeleted: z.boolean(),
/**
* `true` if the file no longer in trash because it was restored to some
* collection.
@@ -24,23 +46,15 @@ export interface EncryptedTrashItem {
* diff. It indicates that the file which was previously in trash is no
* longer in the trash because it was restored to a collection.
*/
- isRestored: boolean;
- deleteBy: number;
- createdAt: number;
- updatedAt: number;
-}
-
-export type Trash = TrashItem[];
+ isRestored: z.boolean(),
+ updatedAt: z.number(),
+ deleteBy: z.number(),
+});
-export const TRASH = "file-trash";
-
-export async function getLocalTrash() {
- const trash = (await localForage.getItem<Trash>(TRASH)) ?? [];
- return trash;
-}
+export type RemoteTrashItem = z.infer<typeof RemoteTrashItem>;
export async function getLocalTrashedFiles() {
- return getTrashedFiles(await getLocalTrash());
+ return getTrashedFiles(await savedTrashItems());
}
/**
@@ -50,14 +64,12 @@ export type EnteTrashFile = EnteFile & {
/**
* Timestamp (epoch microseconds) when this file, which is already in trash,
* will be permanently deleted.
- *
- * On being deleted by the user, files move to trash, will be permanently
- * deleted after 30 days of being moved to trash)
+
*/
deleteBy?: number;
};
-export const getTrashedFiles = (trash: Trash): EnteTrashFile[] =>
+export const getTrashedFiles = (trash: TrashItem[]): EnteTrashFile[] =>
sortTrashFiles(
trash.map(({ file, updatedAt, deleteBy }) => ({
...file,
@@ -79,8 +91,8 @@ const sortTrashFiles = (files: EnteTrashFile[]) =>
});
/**
- * Return the IDs of all the files that are part of the trash as per our local
+ * Return the IDs of all the files that are part of the trash in our local
* database.
*/
export const getLocalTrashFileIDs = () =>
- getLocalTrash().then((trash) => new Set(trash.map((f) => f.file.id)));
+ savedTrashItems().then((items) => new Set(items.map((f) => f.file.id)));