Skip to content

armgit / ente

Public Imported from GitHub
0

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

Move

Manav Rathi4b82516909ef
diff --git a/web/apps/photos/src/components/FileList.tsx b/web/apps/photos/src/components/FileList.tsx
index d5f460fa98..b7ad2c63ca 100644
--- a/web/apps/photos/src/components/FileList.tsx
+++ b/web/apps/photos/src/components/FileList.tsx
@@ -26,7 +26,7 @@ import {
} from "ente-new/photos/components/PlaceholderThumbnails";
import { TileBottomTextOverlay } from "ente-new/photos/components/Tiles";
import { PseudoCollectionID } from "ente-new/photos/services/collection-summary";
-import { enteFileDeletionDate } from "ente-new/photos/services/files";
+import { enteFileDeletionDate } from "ente-new/photos/services/trash";
import { t } from "i18next";
import memoize from "memoize-one";
import { GalleryContext } from "pages/gallery";
diff --git a/web/apps/photos/src/pages/gallery.tsx b/web/apps/photos/src/pages/gallery.tsx
index 6ac6e94c99..4332299a89 100644
--- a/web/apps/photos/src/pages/gallery.tsx
+++ b/web/apps/photos/src/pages/gallery.tsx
@@ -75,7 +75,6 @@ import {
} from "ente-new/photos/services/collection-summary";
import exportService from "ente-new/photos/services/export";
import { updateFilesVisibility } from "ente-new/photos/services/file";
-import { getLocalTrashedFiles } from "ente-new/photos/services/files";
import {
savedCollections,
savedHiddenFiles,
@@ -92,6 +91,7 @@ import {
preCollectionAndFilesSync,
syncCollectionAndFiles,
} from "ente-new/photos/services/sync";
+import { getLocalTrashedFiles } from "ente-new/photos/services/trash";
import {
initUserDetailsOrTriggerSync,
redirectToCustomerPortal,
diff --git a/web/apps/photos/src/pages/shared-albums.tsx b/web/apps/photos/src/pages/shared-albums.tsx
index 40e0dbe9bf..12a2b2d3e2 100644
--- a/web/apps/photos/src/pages/shared-albums.tsx
+++ b/web/apps/photos/src/pages/shared-albums.tsx
@@ -47,6 +47,7 @@ import { FullScreenDropZone } from "ente-gallery/components/FullScreenDropZone";
import { downloadManager } from "ente-gallery/services/download";
import { extractCollectionKeyFromShareURL } from "ente-gallery/services/share";
import { updateShouldDisableCFUploadProxy } from "ente-gallery/services/upload";
+import { sortFiles } from "ente-gallery/utils/files";
import type { Collection } from "ente-media/collection";
import { type EnteFile } from "ente-media/file";
import { verifyPublicAlbumPassword } from "ente-new/albums/services/publicCollection";
@@ -56,7 +57,6 @@ import {
} from "ente-new/photos/components/gallery/ListHeader";
import { isHiddenCollection } from "ente-new/photos/services/collection";
import { PseudoCollectionID } from "ente-new/photos/services/collection-summary";
-import { sortFiles } from "ente-new/photos/services/files";
import { usePhotosAppContext } from "ente-new/photos/types/context";
import { CustomError, parseSharingErrorCodes } from "ente-shared/error";
import { t } from "i18next";
diff --git a/web/apps/photos/src/services/collectionService.ts b/web/apps/photos/src/services/collectionService.ts
index 2bccb2a56b..95b58ca57d 100644
--- a/web/apps/photos/src/services/collectionService.ts
+++ b/web/apps/photos/src/services/collectionService.ts
@@ -2,6 +2,7 @@ import type { User } from "ente-accounts/services/user";
import { ensureLocalUser } from "ente-accounts/services/user";
import log from "ente-base/log";
import { apiURL } from "ente-base/origins";
+import { groupFilesByCollectionID, sortFiles } from "ente-gallery/utils/files";
import { Collection } from "ente-media/collection";
import { EnteFile } from "ente-media/file";
import {
@@ -18,11 +19,7 @@ import {
CollectionsSortBy,
} from "ente-new/photos/services/collection-summary";
import { getLocalCollections } from "ente-new/photos/services/collections";
-import {
- getLocalFiles,
- groupFilesByCollectionID,
- sortFiles,
-} from "ente-new/photos/services/files";
+import { getLocalFiles } from "ente-new/photos/services/files";
import HTTPService from "ente-shared/network/HTTPService";
import { getData } from "ente-shared/storage/localStorage";
import { getToken } from "ente-shared/storage/localStorage/helpers";
diff --git a/web/apps/photos/src/services/publicCollectionService.ts b/web/apps/photos/src/services/publicCollectionService.ts
index e30de3b4b0..075419e947 100644
--- a/web/apps/photos/src/services/publicCollectionService.ts
+++ b/web/apps/photos/src/services/publicCollectionService.ts
@@ -3,6 +3,7 @@ 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 { sortFiles } from "ente-gallery/utils/files";
import type {
Collection,
CollectionPublicMagicMetadataData,
@@ -10,7 +11,6 @@ import type {
import type { EnteFile, RemoteEnteFile } 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";
import HTTPService from "ente-shared/network/HTTPService";
import localForage from "ente-shared/storage/localForage";
diff --git a/web/apps/photos/src/services/watch.ts b/web/apps/photos/src/services/watch.ts
index e475302644..b02155ffd1 100644
--- a/web/apps/photos/src/services/watch.ts
+++ b/web/apps/photos/src/services/watch.ts
@@ -14,11 +14,9 @@ import type {
} from "ente-base/types/ipc";
import { type UploadResult } from "ente-gallery/services/upload";
import type { UploadAsset } from "ente-gallery/services/upload/upload-service";
+import { groupFilesByCollectionID } from "ente-gallery/utils/files";
import type { EnteFile } from "ente-media/file";
-import {
- getLocalFiles,
- groupFilesByCollectionID,
-} from "ente-new/photos/services/files";
+import { getLocalFiles } from "ente-new/photos/services/files";
import { ensureString } from "ente-utils/ensure";
import { removeFromCollection } from "./collectionService";
import { type UploadItemWithCollection, uploadManager } from "./upload-manager";
diff --git a/web/apps/photos/tests/upload.test.ts b/web/apps/photos/tests/upload.test.ts
index 327add692d..17eacfd919 100644
--- a/web/apps/photos/tests/upload.test.ts
+++ b/web/apps/photos/tests/upload.test.ts
@@ -7,6 +7,7 @@ import {
matchJSONMetadata,
metadataJSONMapKeyForJSON,
} from "ente-gallery/services/upload/metadata-json";
+import { groupFilesByCollectionID } from "ente-gallery/utils/files";
import {
fileCreationTime,
fileFileName,
@@ -14,7 +15,6 @@ import {
} from "ente-media/file-metadata";
import { FileType } from "ente-media/file-type";
import { getLocalCollections } from "ente-new/photos/services/collections";
-import { groupFilesByCollectionID } from "ente-new/photos/services/files";
import { savedNormalFiles } from "ente-new/photos/services/photos-fdb";
import { getUserDetailsV2 } from "ente-new/photos/services/user-details";
diff --git a/web/packages/gallery/services/video.ts b/web/packages/gallery/services/video.ts
index b352c5ba20..aff911a726 100644
--- a/web/packages/gallery/services/video.ts
+++ b/web/packages/gallery/services/video.ts
@@ -9,13 +9,11 @@ import { getKV, getKVB, getKVN, setKV } from "ente-base/kv";
import log from "ente-base/log";
import { apiURL } from "ente-base/origins";
import { ensureAuthToken } from "ente-base/token";
+import { uniqueFilesByID } from "ente-gallery/utils/files";
import { fileLogID, type EnteFile } from "ente-media/file";
import { FileType } from "ente-media/file-type";
import { updateFilePublicMagicMetadata } from "ente-new/photos/services/file";
-import {
- getLocalTrashFileIDs,
- uniqueFilesByID,
-} from "ente-new/photos/services/files";
+import { getLocalTrashFileIDs } from "ente-new/photos/services/files";
import { savedFiles } from "ente-new/photos/services/photos-fdb";
import { gunzip, gzip } from "ente-new/photos/utils/gzip";
import { randomSample } from "ente-utils/array";
diff --git a/web/packages/gallery/utils/files.ts b/web/packages/gallery/utils/files.ts
new file mode 100644
index 0000000000..ec3807c265
--- /dev/null
+++ b/web/packages/gallery/utils/files.ts
@@ -0,0 +1,75 @@
+import type { EnteFile } from "ente-media/file";
+import { fileCreationTime } from "ente-media/file-metadata";
+
+/**
+ * Sort the given list of {@link EnteFile}s in place.
+ *
+ * Like the JavaScript Array#sort, this method modifies the {@link files}
+ * argument. It sorts {@link files} in place, and then returns a reference to
+ * the same mutated array.
+ *
+ * By default, files are sorted so that the newest one is first. The optional
+ * {@link sortAsc} flag can be set to `true` to sort them so that the oldest one
+ * is first.
+ */
+export const sortFiles = (files: EnteFile[], sortAsc = false) => {
+ // Sort based on the time of creation time of the file.
+ //
+ // For files with same creation time, sort based on the time of last
+ // modification.
+ const factor = sortAsc ? -1 : 1;
+ return files.sort((a, b) => {
+ const at = fileCreationTime(a);
+ const bt = fileCreationTime(b);
+ return at == bt
+ ? factor *
+ (b.metadata.modificationTime - a.metadata.modificationTime)
+ : factor * (bt - at);
+ });
+};
+
+/**
+ * [Note: Collection file]
+ *
+ * File IDs themselves are unique across all the files for the user (in fact,
+ * they're unique across all the files in an Ente instance).
+ *
+ * However, we can have multiple entries for the same file ID in our local
+ * database and/or remote responses because the unit of account is not file, but
+ * a "Collection File" – a collection and file pair.
+ *
+ * For example, if the same file is symlinked into two collections, then we will
+ * have two "Collection File" entries for it, both with the same file ID, but
+ * with different collection IDs.
+ *
+ * This function returns files such that only one of these entries is returned.
+ * The entry that is returned is arbitrary in general, this function just picks
+ * the first one for each unique file ID.
+ *
+ * If this function is invoked on a list on which {@link sortFiles} has already
+ * been called, which by default sorts such that the newest file is first, then
+ * this function's behaviour would be to return the newest file from among
+ * multiple files with the same ID but different collections.
+ */
+export const uniqueFilesByID = (files: EnteFile[]) => {
+ const seen = new Set<number>();
+ return files.filter(({ id }) => {
+ if (seen.has(id)) return false;
+ seen.add(id);
+ return true;
+ });
+};
+
+/**
+ * Segment the given {@link files} into lists indexed by their collection ID.
+ *
+ * Order is preserved.
+ */
+export const groupFilesByCollectionID = (files: EnteFile[]) =>
+ files.reduce((result, file) => {
+ const id = file.collectionID;
+ let cfs = result.get(id);
+ if (!cfs) result.set(id, (cfs = []));
+ cfs.push(file);
+ return result;
+ }, new Map<number, EnteFile[]>());
diff --git a/web/packages/new/photos/components/gallery/reducer.ts b/web/packages/new/photos/components/gallery/reducer.ts
index 53e74e7d70..ca75a66dd6 100644
--- a/web/packages/new/photos/components/gallery/reducer.ts
+++ b/web/packages/new/photos/components/gallery/reducer.ts
@@ -3,6 +3,11 @@ import {
isArchivedCollection,
isPinnedCollection,
} from "ente-gallery/services/magic-metadata";
+import {
+ groupFilesByCollectionID,
+ sortFiles,
+ uniqueFilesByID,
+} from "ente-gallery/utils/files";
import { collectionTypes, type Collection } from "ente-media/collection";
import type { EnteFile } from "ente-media/file";
import {
@@ -14,6 +19,8 @@ import {
createCollectionNameByID,
isHiddenCollection,
} from "ente-new/photos/services/collection";
+import { getLatestVersionFiles } from "ente-new/photos/services/files";
+import { type TrashedEnteFile } from "ente-new/photos/services/trash";
import { splitByPredicate } from "ente-utils/array";
import { includes } from "ente-utils/type-guards";
import { t } from "i18next";
@@ -28,14 +35,6 @@ import {
type CollectionSummary,
type CollectionSummaryType,
} from "../../services/collection-summary";
-import {
- createFileCollectionIDs,
- getLatestVersionFiles,
- groupFilesByCollectionID,
- sortFiles,
- uniqueFilesByID,
- type TrashedEnteFile,
-} from "../../services/files";
import type { PeopleState, Person } from "../../services/ml/people";
import type { SearchSuggestion } from "../../services/search/types";
import type { FamilyData } from "../../services/user-details";
@@ -1251,6 +1250,19 @@ const deriveFavoriteFileIDs = (
return favoriteFileIDs;
};
+/**
+ * Construct a map from file IDs to the list of collections (IDs) to which the
+ * file belongs.
+ */
+const createFileCollectionIDs = (files: EnteFile[]) =>
+ files.reduce((result, file) => {
+ const id = file.id;
+ let fs = result.get(id);
+ if (!fs) result.set(id, (fs = []));
+ fs.push(file.collectionID);
+ return result;
+ }, new Map<number, number[]>());
+
/**
* Compute normal (non-hidden) collection summaries from their dependencies.
*/
diff --git a/web/packages/new/photos/services/collections.ts b/web/packages/new/photos/services/collections.ts
index 908fe9effd..38e7c63798 100644
--- a/web/packages/new/photos/services/collections.ts
+++ b/web/packages/new/photos/services/collections.ts
@@ -11,8 +11,6 @@ import {
getLocalTrash,
getTrashedFiles,
TRASH,
-} from "ente-new/photos/services/files";
-import {
type EncryptedTrashItem,
type Trash,
} from "ente-new/photos/services/trash";
diff --git a/web/packages/new/photos/services/files.ts b/web/packages/new/photos/services/files.ts
index f3bcaacf44..7a9133fe95 100644
--- a/web/packages/new/photos/services/files.ts
+++ b/web/packages/new/photos/services/files.ts
@@ -1,5 +1,4 @@
import { blobCache } from "ente-base/blob-cache";
-import { dateFromEpochMicroseconds } from "ente-base/date";
import log from "ente-base/log";
import { apiURL } from "ente-base/origins";
import type { Collection } from "ente-media/collection";
@@ -8,10 +7,8 @@ import {
type EnteFile,
type RemoteEnteFile,
} from "ente-media/file";
-import { fileCreationTime, metadataHash } from "ente-media/file-metadata";
-import { type Trash } from "ente-new/photos/services/trash";
+import { metadataHash } from "ente-media/file-metadata";
import HTTPService from "ente-shared/network/HTTPService";
-import localForage from "ente-shared/storage/localForage";
import { getToken } from "ente-shared/storage/localStorage/helpers";
import {
getCollectionLastSyncTime,
@@ -151,130 +148,6 @@ const removeDeletedCollectionFiles = (
return files;
};
-/**
- * Sort the given list of {@link EnteFile}s in place.
- *
- * Like the JavaScript Array#sort, this method modifies the {@link files}
- * argument. It sorts {@link files} in place, and then returns a reference to
- * the same mutated array.
- *
- * By default, files are sorted so that the newest one is first. The optional
- * {@link sortAsc} flag can be set to `true` to sort them so that the oldest one
- * is first.
- */
-export const sortFiles = (files: EnteFile[], sortAsc = false) => {
- // Sort based on the time of creation time of the file.
- //
- // For files with same creation time, sort based on the time of last
- // modification.
- const factor = sortAsc ? -1 : 1;
- return files.sort((a, b) => {
- const at = fileCreationTime(a);
- const bt = fileCreationTime(b);
- return at == bt
- ? factor *
- (b.metadata.modificationTime - a.metadata.modificationTime)
- : factor * (bt - at);
- });
-};
-
-/**
- * [Note: Collection file]
- *
- * File IDs themselves are unique across all the files for the user (in fact,
- * they're unique across all the files in an Ente instance).
- *
- * However, we can have multiple entries for the same file ID in our local
- * database and/or remote responses because the unit of account is not file, but
- * a "Collection File" – a collection and file pair.
- *
- * For example, if the same file is symlinked into two collections, then we will
- * have two "Collection File" entries for it, both with the same file ID, but
- * with different collection IDs.
- *
- * This function returns files such that only one of these entries is returned.
- * The entry that is returned is arbitrary in general, this function just picks
- * the first one for each unique file ID.
- *
- * If this function is invoked on a list on which {@link sortFiles} has already
- * been called, which by default sorts such that the newest file is first, then
- * this function's behaviour would be to return the newest file from among
- * multiple files with the same ID but different collections.
- */
-export const uniqueFilesByID = (files: EnteFile[]) => {
- const seen = new Set<number>();
- return files.filter(({ id }) => {
- if (seen.has(id)) return false;
- seen.add(id);
- return true;
- });
-};
-
-export const TRASH = "file-trash";
-
-export async function getLocalTrash() {
- const trash = (await localForage.getItem<Trash>(TRASH)) ?? [];
- return trash;
-}
-
-export async function getLocalTrashedFiles() {
- return getTrashedFiles(await getLocalTrash());
-}
-
-export type TrashedEnteFile = EnteFile & {
- /**
- * `true` if this file is in trash (i.e. it has been deleted by the user,
- * and will be permanently deleted after 30 days of being moved to trash).
- */
- isTrashed?: boolean;
- /**
- * If {@link isTrashed} is `true`, then {@link deleteBy} contains the epoch
- * microseconds when this file will be permanently deleted.
- */
- deleteBy?: number;
-};
-
-/**
- * Return the date when the file will be deleted permanently. Only valid for
- * files that are in the user's trash.
- *
- * This is a convenience wrapper over the {@link deleteBy} property of a file,
- * converting that epoch microsecond value into a JavaScript date.
- */
-export const enteFileDeletionDate = (file: TrashedEnteFile) =>
- dateFromEpochMicroseconds(file.deleteBy);
-
-export function getTrashedFiles(trash: Trash): TrashedEnteFile[] {
- return sortTrashFiles(
- trash.map((trashedFile) => ({
- ...trashedFile.file,
- updationTime: trashedFile.updatedAt,
- deleteBy: trashedFile.deleteBy,
- isTrashed: true,
- })),
- );
-}
-
-/**
- * Return the IDs of all the files that are part of the trash as per our local
- * database.
- */
-export const getLocalTrashFileIDs = () =>
- getLocalTrash().then((trash) => new Set(trash.map((f) => f.file.id)));
-
-const sortTrashFiles = (files: TrashedEnteFile[]) => {
- return files.sort((a, b) => {
- if (a.deleteBy === b.deleteBy) {
- const at = fileCreationTime(a);
- const bt = fileCreationTime(b);
- return at == bt
- ? b.metadata.modificationTime - a.metadata.modificationTime
- : bt - at;
- }
- return (a.deleteBy ?? 0) - (b.deleteBy ?? 0);
- });
-};
-
/**
* Clear cached thumbnails for existing files if the thumbnail data has changed.
*
@@ -295,7 +168,7 @@ const sortTrashFiles = (files: TrashedEnteFile[]) => {
*
* @param newFiles The {@link EnteFile}s which we got in the diff response.
*/
-export const clearCachedThumbnailsIfChanged = async (
+const clearCachedThumbnailsIfChanged = async (
existingFiles: EnteFile[],
newFiles: EnteFile[],
) => {
@@ -328,33 +201,6 @@ export const clearCachedThumbnailsIfChanged = async (
}
};
-/**
- * Segment the given {@link files} into lists indexed by their collection ID.
- *
- * Order is preserved.
- */
-export const groupFilesByCollectionID = (files: EnteFile[]) =>
- files.reduce((result, file) => {
- const id = file.collectionID;
- let cfs = result.get(id);
- if (!cfs) result.set(id, (cfs = []));
- cfs.push(file);
- return result;
- }, new Map<number, EnteFile[]>());
-
-/**
- * Construct a map from file IDs to the list of collections (IDs) to which the
- * file belongs.
- */
-export const createFileCollectionIDs = (files: EnteFile[]) =>
- files.reduce((result, file) => {
- const id = file.id;
- let fs = result.get(id);
- if (!fs) result.set(id, (fs = []));
- fs.push(file.collectionID);
- return result;
- }, new Map<number, number[]>());
-
export function getLatestVersionFiles(files: EnteFile[]) {
const latestVersionFiles = new Map<string, EnteFile>();
files.forEach((file) => {
diff --git a/web/packages/new/photos/services/search/index.ts b/web/packages/new/photos/services/search/index.ts
index 4e30266995..d06447638b 100644
--- a/web/packages/new/photos/services/search/index.ts
+++ b/web/packages/new/photos/services/search/index.ts
@@ -1,9 +1,9 @@
import log from "ente-base/log";
import { ensureMasterKeyFromSession } from "ente-base/session";
import { ComlinkWorker } from "ente-base/worker/comlink-worker";
+import { uniqueFilesByID } from "ente-gallery/utils/files";
import { FileType } from "ente-media/file-type";
import i18n, { t } from "i18next";
-import { uniqueFilesByID } from "../files";
import { clipMatches, isMLEnabled, isMLSupported } from "../ml";
import type { NamedPerson } from "../ml/people";
import type {
diff --git a/web/packages/new/photos/services/trash.ts b/web/packages/new/photos/services/trash.ts
index 7ddb27ed03..8770c1dbd4 100644
--- a/web/packages/new/photos/services/trash.ts
+++ b/web/packages/new/photos/services/trash.ts
@@ -1,4 +1,7 @@
+import { dateFromEpochMicroseconds } from "ente-base/date";
import type { EnteFile, RemoteEnteFile } from "ente-media/file";
+import { fileCreationTime } from "ente-media/file-metadata";
+import localForage from "ente-shared/storage/localForage";
export interface TrashItem extends Omit<EncryptedTrashItem, "file"> {
file: EnteFile;
@@ -29,3 +32,68 @@ export interface EncryptedTrashItem {
}
export type Trash = TrashItem[];
+
+export const TRASH = "file-trash";
+
+export async function getLocalTrash() {
+ const trash = (await localForage.getItem<Trash>(TRASH)) ?? [];
+ return trash;
+}
+
+export async function getLocalTrashedFiles() {
+ return getTrashedFiles(await getLocalTrash());
+}
+
+export type TrashedEnteFile = EnteFile & {
+ /**
+ * `true` if this file is in trash (i.e. it has been deleted by the user,
+ * and will be permanently deleted after 30 days of being moved to trash).
+ */
+ isTrashed?: boolean;
+ /**
+ * If {@link isTrashed} is `true`, then {@link deleteBy} contains the epoch
+ * microseconds when this file will be permanently deleted.
+ */
+ deleteBy?: number;
+};
+
+/**
+ * Return the date when the file will be deleted permanently. Only valid for
+ * files that are in the user's trash.
+ *
+ * This is a convenience wrapper over the {@link deleteBy} property of a file,
+ * converting that epoch microsecond value into a JavaScript date.
+ */
+export const enteFileDeletionDate = (file: TrashedEnteFile) =>
+ dateFromEpochMicroseconds(file.deleteBy);
+
+export function getTrashedFiles(trash: Trash): TrashedEnteFile[] {
+ return sortTrashFiles(
+ trash.map((trashedFile) => ({
+ ...trashedFile.file,
+ updationTime: trashedFile.updatedAt,
+ deleteBy: trashedFile.deleteBy,
+ isTrashed: true,
+ })),
+ );
+}
+
+const sortTrashFiles = (files: TrashedEnteFile[]) => {
+ return files.sort((a, b) => {
+ if (a.deleteBy === b.deleteBy) {
+ const at = fileCreationTime(a);
+ const bt = fileCreationTime(b);
+ return at == bt
+ ? b.metadata.modificationTime - a.metadata.modificationTime
+ : bt - at;
+ }
+ return (a.deleteBy ?? 0) - (b.deleteBy ?? 0);
+ });
+};
+
+/**
+ * Return the IDs of all the files that are part of the trash as per our local
+ * database.
+ */
+export const getLocalTrashFileIDs = () =>
+ getLocalTrash().then((trash) => new Set(trash.map((f) => f.file.id)));