Skip to content

armgit / ente

Public Imported from GitHub
0

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

Move

Manav Rathia292f01187f9
diff --git a/web/apps/photos/src/components/Collections/CollectionHeader.tsx b/web/apps/photos/src/components/Collections/CollectionHeader.tsx
index 25d0bd0261..0e48f2da78 100644
--- a/web/apps/photos/src/components/Collections/CollectionHeader.tsx
+++ b/web/apps/photos/src/components/Collections/CollectionHeader.tsx
@@ -50,7 +50,10 @@ import { GalleryContext } from "pages/gallery";
import React, { useCallback, useContext, useRef } from "react";
import { Trans } from "react-i18next";
import * as CollectionAPI from "services/collectionService";
-import * as TrashService from "services/trashService";
+import {
+ emptyTrash,
+ clearLocalTrash,
+} from "@/new/photos/services/collections";
import { SetFilesDownloadProgressAttributesCreator } from "types/gallery";
import {
changeCollectionOrder,
@@ -229,13 +232,13 @@ const CollectionOptions: React.FC<CollectionOptionsProps> = ({
continue: {
text: t("empty_trash"),
color: "critical",
- action: emptyTrash,
+ action: doEmptyTrash,
},
});
- const emptyTrash = wrap(async () => {
- await TrashService.emptyTrash();
- await TrashService.clearLocalTrash();
+ const doEmptyTrash = wrap(async () => {
+ await emptyTrash();
+ await clearLocalTrash();
setActiveCollectionID(ALL_SECTION);
});
diff --git a/web/apps/photos/src/pages/deduplicate.tsx b/web/apps/photos/src/pages/deduplicate.tsx
index 6defce11f2..fbeb5bcbd1 100644
--- a/web/apps/photos/src/pages/deduplicate.tsx
+++ b/web/apps/photos/src/pages/deduplicate.tsx
@@ -6,6 +6,7 @@ import { ALL_SECTION, moveToTrash } from "@/new/photos/services/collection";
import {
getAllLatestCollections,
getLocalCollections,
+ syncTrash,
} from "@/new/photos/services/collections";
import {
createFileCollectionIDs,
@@ -27,7 +28,6 @@ import { t } from "i18next";
import { default as Router, default as router } from "next/router";
import { createContext, useEffect, useState } from "react";
import { Duplicate, getDuplicates } from "services/deduplicationService";
-import { syncTrash } from "services/trashService";
import { SelectedState } from "types/gallery";
import { getSelectedFiles } from "utils/file";
diff --git a/web/apps/photos/src/pages/gallery.tsx b/web/apps/photos/src/pages/gallery.tsx
index 1b6f41b299..60ae8c915d 100644
--- a/web/apps/photos/src/pages/gallery.tsx
+++ b/web/apps/photos/src/pages/gallery.tsx
@@ -38,6 +38,7 @@ import { areOnlySystemCollections } from "@/new/photos/services/collection/ui";
import {
getAllLatestCollections,
getAllLocalCollections,
+ syncTrash,
} from "@/new/photos/services/collections";
import {
getLocalFiles,
@@ -120,7 +121,6 @@ import {
} from "services/collectionService";
import exportService from "services/export";
import { preCollectionsAndFilesSync, sync } from "services/sync";
-import { syncTrash } from "services/trashService";
import uploadManager from "services/upload/uploadManager";
import { isTokenValid } from "services/userService";
import {
diff --git a/web/apps/photos/src/services/sync.ts b/web/apps/photos/src/services/sync.ts
index 2bba28055f..63bb1841fc 100644
--- a/web/apps/photos/src/services/sync.ts
+++ b/web/apps/photos/src/services/sync.ts
@@ -1,11 +1,13 @@
import { isHiddenCollection } from "@/new/photos/services/collection";
-import { getAllLatestCollections } from "@/new/photos/services/collections";
+import {
+ getAllLatestCollections,
+ syncTrash,
+} from "@/new/photos/services/collections";
import { syncFiles } from "@/new/photos/services/files";
import { isMLSupported, mlStatusSync, mlSync } from "@/new/photos/services/ml";
import { searchDataSync } from "@/new/photos/services/search";
import { syncSettings } from "@/new/photos/services/settings";
import { splitByPredicate } from "@/utils/array";
-import { syncTrash } from "./trashService";
/**
* Part 1 of {@link sync}. See TODO below for why this is split.
diff --git a/web/apps/photos/src/services/trashService.ts b/web/apps/photos/src/services/trashService.ts
index 9d61cea1ed..e69de29bb2 100644
--- a/web/apps/photos/src/services/trashService.ts
+++ b/web/apps/photos/src/services/trashService.ts
@@ -1,160 +0,0 @@
-import log from "@/base/log";
-import { apiURL } from "@/base/origins";
-import type { Collection } from "@/media/collection";
-import {
- decryptFile,
- EncryptedTrashItem,
- Trash,
- type EnteFile,
-} from "@/media/file";
-import { getCollection } from "@/new/photos/services/collections";
-import {
- getLocalTrash,
- getTrashedFiles,
- TRASH,
-} from "@/new/photos/services/files";
-import HTTPService from "@ente/shared/network/HTTPService";
-import localForage from "@ente/shared/storage/localForage";
-import { getToken } from "@ente/shared/storage/localStorage/helpers";
-
-const TRASH_TIME = "trash-time";
-const DELETED_COLLECTION = "deleted-collection";
-
-export async function getLocalDeletedCollections() {
- const trashedCollections: Collection[] =
- (await localForage.getItem<Collection[]>(DELETED_COLLECTION)) || [];
- const nonUndefinedCollections = trashedCollections.filter(
- (collection) => !!collection,
- );
- if (nonUndefinedCollections.length !== trashedCollections.length) {
- await localForage.setItem(DELETED_COLLECTION, nonUndefinedCollections);
- }
- return nonUndefinedCollections;
-}
-
-export async function cleanTrashCollections(fileTrash: Trash) {
- const trashedCollections = await getLocalDeletedCollections();
- const neededTrashCollections = new Set<number>(
- fileTrash.map((item) => item.file.collectionID),
- );
- const filterCollections = trashedCollections.filter((item) =>
- neededTrashCollections.has(item.id),
- );
- await localForage.setItem(DELETED_COLLECTION, filterCollections);
-}
-
-async function getLastSyncTime() {
- return (await localForage.getItem<number>(TRASH_TIME)) ?? 0;
-}
-export async function syncTrash(
- collections: Collection[],
- setTrashedFiles: (fs: EnteFile[]) => void,
-): Promise<void> {
- const trash = await getLocalTrash();
- collections = [...collections, ...(await getLocalDeletedCollections())];
- const collectionMap = new Map<number, Collection>(
- collections.map((collection) => [collection.id, collection]),
- );
- if (!getToken()) {
- return;
- }
- const lastSyncTime = await getLastSyncTime();
-
- const updatedTrash = await updateTrash(
- collectionMap,
- lastSyncTime,
- setTrashedFiles,
- trash,
- );
- cleanTrashCollections(updatedTrash);
-}
-
-export const updateTrash = async (
- collections: Map<number, Collection>,
- sinceTime: number,
- setTrashedFiles: (fs: EnteFile[]) => void,
- currentTrash: Trash,
-): Promise<Trash> => {
- try {
- let updatedTrash: Trash = [...currentTrash];
- let time = sinceTime;
-
- let resp;
- do {
- const token = getToken();
- if (!token) {
- break;
- }
- resp = await HTTPService.get(
- await apiURL("/trash/v2/diff"),
- {
- sinceTime: time,
- },
- {
- "X-Auth-Token": token,
- },
- );
- // #Perf: This can be optimized by running the decryption in parallel
- for (const trashItem of resp.data.diff as EncryptedTrashItem[]) {
- const collectionID = trashItem.file.collectionID;
- let collection = collections.get(collectionID);
- if (!collection) {
- collection = await getCollection(collectionID);
- collections.set(collectionID, collection);
- localForage.setItem(DELETED_COLLECTION, [
- ...collections.values(),
- ]);
- }
- if (!trashItem.isDeleted && !trashItem.isRestored) {
- const decryptedFile = await decryptFile(
- trashItem.file,
- collection.key,
- );
- updatedTrash.push({ ...trashItem, file: decryptedFile });
- } else {
- updatedTrash = updatedTrash.filter(
- (item) => item.file.id !== trashItem.file.id,
- );
- }
- }
-
- if (resp.data.diff.length) {
- time = resp.data.diff.slice(-1)[0].updatedAt;
- }
-
- setTrashedFiles(getTrashedFiles(updatedTrash));
- await localForage.setItem(TRASH, updatedTrash);
- await localForage.setItem(TRASH_TIME, time);
- } while (resp.data.hasMore);
- return updatedTrash;
- } catch (e) {
- log.error("Get trash files failed", e);
- }
- return currentTrash;
-};
-
-export const emptyTrash = async () => {
- try {
- const token = getToken();
- if (!token) {
- return;
- }
- const lastUpdatedAt = await getLastSyncTime();
-
- await HTTPService.post(
- await apiURL("/trash/empty"),
- { lastUpdatedAt },
- null,
- {
- "X-Auth-Token": token,
- },
- );
- } catch (e) {
- log.error("empty trash failed", e);
- throw e;
- }
-};
-
-export const clearLocalTrash = async () => {
- await localForage.setItem(TRASH, []);
-};
diff --git a/web/packages/new/photos/services/collections.ts b/web/packages/new/photos/services/collections.ts
index 647f8a037c..a86e326baa 100644
--- a/web/packages/new/photos/services/collections.ts
+++ b/web/packages/new/photos/services/collections.ts
@@ -16,9 +16,20 @@ import {
type CollectionShareeMagicMetadata,
type EncryptedCollection,
} from "@/media/collection";
+import {
+ decryptFile,
+ type EncryptedTrashItem,
+ type EnteFile,
+ type Trash,
+} from "@/media/file";
+import {
+ getLocalTrash,
+ getTrashedFiles,
+ TRASH,
+} from "@/new/photos/services/files";
import HTTPService from "@ente/shared/network/HTTPService";
import localForage from "@ente/shared/storage/localForage";
-import { LS_KEYS, getData } from "@ente/shared/storage/localStorage";
+import { getData, LS_KEYS } from "@ente/shared/storage/localStorage";
import { getToken } from "@ente/shared/storage/localStorage/helpers";
import { getActualKey } from "@ente/shared/user";
import { isHiddenCollection } from "./collection";
@@ -305,3 +316,147 @@ export const getCollection = async (
throw e;
}
};
+
+const TRASH_TIME = "trash-time";
+const DELETED_COLLECTION = "deleted-collection";
+
+export async function getLocalDeletedCollections() {
+ const trashedCollections: Collection[] =
+ (await localForage.getItem<Collection[]>(DELETED_COLLECTION)) || [];
+ const nonUndefinedCollections = trashedCollections.filter(
+ (collection) => !!collection,
+ );
+ if (nonUndefinedCollections.length !== trashedCollections.length) {
+ await localForage.setItem(DELETED_COLLECTION, nonUndefinedCollections);
+ }
+ return nonUndefinedCollections;
+}
+
+export async function cleanTrashCollections(fileTrash: Trash) {
+ const trashedCollections = await getLocalDeletedCollections();
+ const neededTrashCollections = new Set<number>(
+ fileTrash.map((item) => item.file.collectionID),
+ );
+ const filterCollections = trashedCollections.filter((item) =>
+ neededTrashCollections.has(item.id),
+ );
+ await localForage.setItem(DELETED_COLLECTION, filterCollections);
+}
+
+async function getLastTrashSyncTime() {
+ return (await localForage.getItem<number>(TRASH_TIME)) ?? 0;
+}
+export async function syncTrash(
+ collections: Collection[],
+ setTrashedFiles: (fs: EnteFile[]) => void,
+): Promise<void> {
+ const trash = await getLocalTrash();
+ collections = [...collections, ...(await getLocalDeletedCollections())];
+ const collectionMap = new Map<number, Collection>(
+ collections.map((collection) => [collection.id, collection]),
+ );
+ if (!getToken()) {
+ return;
+ }
+ const lastSyncTime = await getLastTrashSyncTime();
+
+ const updatedTrash = await updateTrash(
+ collectionMap,
+ lastSyncTime,
+ setTrashedFiles,
+ trash,
+ );
+ await cleanTrashCollections(updatedTrash);
+}
+
+export const updateTrash = async (
+ collections: Map<number, Collection>,
+ sinceTime: number,
+ setTrashedFiles: (fs: EnteFile[]) => void,
+ currentTrash: Trash,
+): Promise<Trash> => {
+ try {
+ let updatedTrash: Trash = [...currentTrash];
+ let time = sinceTime;
+
+ let resp;
+ do {
+ const token = getToken();
+ if (!token) {
+ break;
+ }
+ resp = await HTTPService.get(
+ await apiURL("/trash/v2/diff"),
+ {
+ sinceTime: time,
+ },
+ {
+ "X-Auth-Token": token,
+ },
+ );
+ // #Perf: This can be optimized by running the decryption in parallel
+ for (const trashItem of resp.data.diff as EncryptedTrashItem[]) {
+ const collectionID = trashItem.file.collectionID;
+ let collection = collections.get(collectionID);
+ if (!collection) {
+ collection = await getCollection(collectionID);
+ collections.set(collectionID, collection);
+ await localForage.setItem(DELETED_COLLECTION, [
+ ...collections.values(),
+ ]);
+ }
+ if (!trashItem.isDeleted && !trashItem.isRestored) {
+ const decryptedFile = await decryptFile(
+ trashItem.file,
+ collection.key,
+ );
+ updatedTrash.push({ ...trashItem, file: decryptedFile });
+ } else {
+ updatedTrash = updatedTrash.filter(
+ (item) => item.file.id !== trashItem.file.id,
+ );
+ }
+ }
+
+ if (resp.data.diff.length) {
+ time = resp.data.diff.slice(-1)[0].updatedAt;
+ }
+
+ setTrashedFiles(getTrashedFiles(updatedTrash));
+ await localForage.setItem(TRASH, updatedTrash);
+ await localForage.setItem(TRASH_TIME, time);
+ } while (resp.data.hasMore);
+ return updatedTrash;
+ } catch (e) {
+ log.error("Get trash files failed", e);
+ }
+ return currentTrash;
+};
+
+export const emptyTrash = async () => {
+ try {
+ const token = getToken();
+ if (!token) {
+ return;
+ }
+ const lastUpdatedAt = await getLastTrashSyncTime();
+
+ await HTTPService.post(
+ await apiURL("/trash/empty"),
+ { lastUpdatedAt },
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
+ // @ts-ignore
+ null,
+ {
+ "X-Auth-Token": token,
+ },
+ );
+ } catch (e) {
+ log.error("empty trash failed", e);
+ throw e;
+ }
+};
+
+export const clearLocalTrash = async () => {
+ await localForage.setItem(TRASH, []);
+};