π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Move
Manav Rathiadeab53d3bd4
diff --git a/web/apps/photos/src/pages/gallery.tsx b/web/apps/photos/src/pages/gallery.tsxindex 2d30b1a56b..15e229fdb9 100644--- a/web/apps/photos/src/pages/gallery.tsx+++ b/web/apps/photos/src/pages/gallery.tsx@@ -40,6 +40,7 @@ import {getLocalFiles,getLocalTrashedFiles,sortFiles,+ syncFiles,} from "@/new/photos/services/files";import {filterSearchableFiles,@@ -115,7 +116,7 @@ import {createUnCategorizedCollection,getAllLatestCollections,} from "services/collectionService";-import { syncFiles } from "services/fileService";+import exportService from "services/export";import { preCollectionsAndFilesSync, sync } from "services/sync";import { syncTrash } from "services/trashService";import uploadManager from "services/upload/uploadManager";@@ -577,13 +578,13 @@ export default function Gallery() {collections,hiddenCollections,});- await syncFiles(+ const didUpdateNormalFiles = await syncFiles("normal",collections,(files) => dispatch({ type: "setFiles", files }),(files) => dispatch({ type: "fetchFiles", files }),);- await syncFiles(+ const didUpdateHiddenFiles = await syncFiles("hidden",hiddenCollections,(hiddenFiles) =>@@ -591,6 +592,8 @@ export default function Gallery() {(hiddenFiles) =>dispatch({ type: "fetchHiddenFiles", hiddenFiles }),);+ if (didUpdateNormalFiles || didUpdateHiddenFiles)+ exportService.onLocalFilesUpdated();await syncTrash(allCollections, (trashedFiles: EnteFile[]) =>dispatch({ type: "setTrashedFiles", trashedFiles }),);diff --git a/web/apps/photos/src/services/fileService.ts b/web/apps/photos/src/services/fileService.tsindex 3c41d700dc..f948e45067 100644--- a/web/apps/photos/src/services/fileService.ts+++ b/web/apps/photos/src/services/fileService.ts@@ -1,134 +1,13 @@import { encryptMetadataJSON } from "@/base/crypto";-import log from "@/base/log";import { apiURL } from "@/base/origins";-import type { Collection } from "@/media/collection";import {type EncryptedMagicMetadata,- decryptFile,- EncryptedEnteFile,EnteFile,FileWithUpdatedMagicMetadata,FileWithUpdatedPublicMagicMetadata,} from "@/media/file";-import {- getCollectionLastSyncTime,- setCollectionLastSyncTime,-} from "@/new/photos/services/collections";-import {- clearCachedThumbnailsIfChanged,- getLatestVersionFiles,- getLocalFiles,- setLocalFiles,-} from "@/new/photos/services/files";import HTTPService from "@ente/shared/network/HTTPService";import { getToken } from "@ente/shared/storage/localStorage/helpers";-import exportService from "services/export";--/**- * Fetch all files of the given {@link type}, belonging to the given- * {@link collections}, from remote and update our local database.- *- * If this is the initial read, or if the count of files we have differs from- * the state of the local database (these two are expected to be the same case),- * then the {@link onResetFiles} callback is invoked to give the caller a chance- * to bring its state up to speed.- *- * In addition to updating the local database, it also calls the provided- * {@link onFetchFiles} callback with the latest decrypted files after each- * batch the new and/or updated files are received from remote.- */-export const syncFiles = async (- type: "normal" | "hidden",- collections: Collection[],- onResetFiles: (fs: EnteFile[]) => void,- onFetchFiles: (fs: EnteFile[]) => void,-) => {- const localFiles = await getLocalFiles(type);- let files = await removeDeletedCollectionFiles(collections, localFiles);- let didUpdateFiles = false;- if (files.length !== localFiles.length) {- await setLocalFiles(type, files);- onResetFiles(files);- didUpdateFiles = true;- }- for (const collection of collections) {- if (!getToken()) {- continue;- }- const lastSyncTime = await getCollectionLastSyncTime(collection);- if (collection.updationTime === lastSyncTime) {- continue;- }-- const newFiles = await getFiles(collection, lastSyncTime, onFetchFiles);- await clearCachedThumbnailsIfChanged(localFiles, newFiles);- files = getLatestVersionFiles([...files, ...newFiles]);- await setLocalFiles(type, files);- didUpdateFiles = true;- setCollectionLastSyncTime(collection, collection.updationTime);- }- if (didUpdateFiles) exportService.onLocalFilesUpdated();-};--export const getFiles = async (- collection: Collection,- sinceTime: number,- onFetchFiles: (fs: EnteFile[]) => void,-): Promise<EnteFile[]> => {- try {- let decryptedFiles: EnteFile[] = [];- let time = sinceTime;- let resp;- do {- const token = getToken();- if (!token) {- break;- }- resp = await HTTPService.get(- await apiURL("/collections/v2/diff"),- {- collectionID: collection.id,- sinceTime: time,- },- {- "X-Auth-Token": token,- },- );-- const newDecryptedFilesBatch = await Promise.all(- resp.data.diff.map(async (file: EncryptedEnteFile) => {- if (!file.isDeleted) {- return await decryptFile(file, collection.key);- } else {- return file;- }- }) as Promise<EnteFile>[],- );- decryptedFiles = [...decryptedFiles, ...newDecryptedFilesBatch];-- onFetchFiles(decryptedFiles);- if (resp.data.diff.length) {- time = resp.data.diff.slice(-1)[0].updationTime;- }- } while (resp.data.hasMore);- return decryptedFiles;- } catch (e) {- log.error("Get files failed", e);- throw e;- }-};--const removeDeletedCollectionFiles = async (- collections: Collection[],- files: EnteFile[],-) => {- const syncedCollectionIds = new Set<number>();- for (const collection of collections) {- syncedCollectionIds.add(collection.id);- }- files = files.filter((file) => syncedCollectionIds.has(file.collectionID));- return files;-};export interface UpdateMagicMetadataRequest {id: number;diff --git a/web/packages/new/photos/services/files.ts b/web/packages/new/photos/services/files.tsindex 6c3c61cf7e..bca53f6132 100644--- a/web/packages/new/photos/services/files.ts+++ b/web/packages/new/photos/services/files.ts@@ -1,7 +1,22 @@import { blobCache } from "@/base/blob-cache";-import { mergeMetadata, type EnteFile, type Trash } from "@/media/file";+import log from "@/base/log";+import { apiURL } from "@/base/origins";+import type { Collection } from "@/media/collection";+import {+ decryptFile,+ mergeMetadata,+ type EncryptedEnteFile,+ type EnteFile,+ type Trash,+} from "@/media/file";import { metadataHash } from "@/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,+ setCollectionLastSyncTime,+} from "./collections";const FILES_TABLE = "files";const HIDDEN_FILES_TABLE = "hidden-files";@@ -37,6 +52,118 @@ export const setLocalFiles = async (await localForage.setItem(tableName, files);};+/**+ * Fetch all files of the given {@link type}, belonging to the given+ * {@link collections}, from remote and update our local database.+ *+ * If this is the initial read, or if the count of files we have differs from+ * the state of the local database (these two are expected to be the same case),+ * then the {@link onResetFiles} callback is invoked to give the caller a chance+ * to bring its state up to speed.+ *+ * In addition to updating the local database, it also calls the provided+ * {@link onFetchFiles} callback with the latest decrypted files after each+ * batch the new and/or updated files are received from remote.+ *+ * @returns true if one or more files were updated locally, false otherwise.+ */+export const syncFiles = async (+ type: "normal" | "hidden",+ collections: Collection[],+ onResetFiles: (fs: EnteFile[]) => void,+ onFetchFiles: (fs: EnteFile[]) => void,+) => {+ const localFiles = await getLocalFiles(type);+ let files = removeDeletedCollectionFiles(collections, localFiles);+ let didUpdateFiles = false;+ if (files.length !== localFiles.length) {+ await setLocalFiles(type, files);+ onResetFiles(files);+ didUpdateFiles = true;+ }+ for (const collection of collections) {+ if (!getToken()) {+ continue;+ }+ const lastSyncTime = await getCollectionLastSyncTime(collection);+ if (collection.updationTime === lastSyncTime) {+ continue;+ }++ const newFiles = await getFiles(collection, lastSyncTime, onFetchFiles);+ await clearCachedThumbnailsIfChanged(localFiles, newFiles);+ files = getLatestVersionFiles([...files, ...newFiles]);+ await setLocalFiles(type, files);+ didUpdateFiles = true;+ await setCollectionLastSyncTime(collection, collection.updationTime);+ }+ return didUpdateFiles;+};++export const getFiles = async (+ collection: Collection,+ sinceTime: number,+ onFetchFiles: (fs: EnteFile[]) => void,+): Promise<EnteFile[]> => {+ try {+ let decryptedFiles: EnteFile[] = [];+ let time = sinceTime;+ let resp;+ do {+ const token = getToken();+ if (!token) {+ break;+ }+ resp = await HTTPService.get(+ await apiURL("/collections/v2/diff"),+ {+ collectionID: collection.id,+ sinceTime: time,+ },+ {+ "X-Auth-Token": token,+ },+ );++ const newDecryptedFilesBatch = await Promise.all(+ // eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access+ resp.data.diff.map(async (file: EncryptedEnteFile) => {+ if (!file.isDeleted) {+ return await decryptFile(file, collection.key);+ } else {+ return file;+ }+ }) as Promise<EnteFile>[],+ );+ decryptedFiles = [...decryptedFiles, ...newDecryptedFilesBatch];++ onFetchFiles(decryptedFiles);+ // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access+ if (resp.data.diff.length) {+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access+ time = resp.data.diff.slice(-1)[0].updationTime;+ }+ // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access+ } while (resp.data.hasMore);+ return decryptedFiles;+ } catch (e) {+ log.error("Get files failed", e);+ throw e;+ }+};++const removeDeletedCollectionFiles = (+ collections: Collection[],+ files: EnteFile[],+) => {+ const syncedCollectionIds = new Set<number>();+ for (const collection of collections) {+ syncedCollectionIds.add(collection.id);+ }+ files = files.filter((file) => syncedCollectionIds.has(file.collectionID));+ return files;+};+/*** Sort the given list of {@link EnteFile}s in place.*