Skip to content

armgit / ente

Public Imported from GitHub
0

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

Move

Manav Rathic37d85f6c537
diff --git a/web/apps/photos/src/services/collectionService.ts b/web/apps/photos/src/services/collectionService.ts
index a8dc2bc61b..81864d4c95 100644
--- a/web/apps/photos/src/services/collectionService.ts
+++ b/web/apps/photos/src/services/collectionService.ts
@@ -2,7 +2,6 @@ import { encryptMetadataJSON, sharedCryptoWorker } from "@/base/crypto";
import log from "@/base/log";
import { apiURL } from "@/base/origins";
import {
- AddToCollectionRequest,
Collection,
CollectionMagicMetadata,
CollectionMagicMetadataProps,
@@ -11,8 +10,6 @@ import {
CollectionType,
CreatePublicAccessTokenRequest,
EncryptedCollection,
- EncryptedFileKey,
- MoveToCollectionRequest,
PublicURL,
RemoveFromCollectionRequest,
SUB_TYPE,
@@ -21,8 +18,10 @@ import {
import { EncryptedMagicMetadata, EnteFile } from "@/media/file";
import { ItemVisibility } from "@/media/file-metadata";
import {
+ addToCollection,
isDefaultHiddenCollection,
isHiddenCollection,
+ moveToCollection,
} from "@/new/photos/services/collection";
import type { CollectionSummary } from "@/new/photos/services/collection/ui";
import {
@@ -422,119 +421,6 @@ export const removeFromFavorites = async (file: EnteFile) => {
}
};
-export const addToCollection = async (
- collection: Collection,
- files: EnteFile[],
-) => {
- try {
- const token = getToken();
- const batchedFiles = batch(files, REQUEST_BATCH_SIZE);
- for (const batch of batchedFiles) {
- const fileKeysEncryptedWithNewCollection =
- await encryptWithNewCollectionKey(collection, batch);
-
- const requestBody: AddToCollectionRequest = {
- collectionID: collection.id,
- files: fileKeysEncryptedWithNewCollection,
- };
- await HTTPService.post(
- await apiURL("/collections/add-files"),
- requestBody,
- null,
- {
- "X-Auth-Token": token,
- },
- );
- }
- } catch (e) {
- log.error("Add to collection Failed ", e);
- throw e;
- }
-};
-
-export const restoreToCollection = async (
- collection: Collection,
- files: EnteFile[],
-) => {
- try {
- const token = getToken();
- const batchedFiles = batch(files, REQUEST_BATCH_SIZE);
- for (const batch of batchedFiles) {
- const fileKeysEncryptedWithNewCollection =
- await encryptWithNewCollectionKey(collection, batch);
-
- const requestBody: AddToCollectionRequest = {
- collectionID: collection.id,
- files: fileKeysEncryptedWithNewCollection,
- };
- await HTTPService.post(
- await apiURL("/collections/restore-files"),
- requestBody,
- null,
- {
- "X-Auth-Token": token,
- },
- );
- }
- } catch (e) {
- log.error("restore to collection Failed ", e);
- throw e;
- }
-};
-export const moveToCollection = async (
- fromCollectionID: number,
- toCollection: Collection,
- files: EnteFile[],
-) => {
- try {
- const token = getToken();
- const batchedFiles = batch(files, REQUEST_BATCH_SIZE);
- for (const batch of batchedFiles) {
- const fileKeysEncryptedWithNewCollection =
- await encryptWithNewCollectionKey(toCollection, batch);
-
- const requestBody: MoveToCollectionRequest = {
- fromCollectionID: fromCollectionID,
- toCollectionID: toCollection.id,
- files: fileKeysEncryptedWithNewCollection,
- };
- await HTTPService.post(
- await apiURL("/collections/move-files"),
- requestBody,
- null,
- {
- "X-Auth-Token": token,
- },
- );
- }
- } catch (e) {
- log.error("move to collection Failed ", e);
- throw e;
- }
-};
-
-const encryptWithNewCollectionKey = async (
- newCollection: Collection,
- files: EnteFile[],
-): Promise<EncryptedFileKey[]> => {
- const fileKeysEncryptedWithNewCollection: EncryptedFileKey[] = [];
- const cryptoWorker = await sharedCryptoWorker();
- for (const file of files) {
- const newEncryptedKey = await cryptoWorker.encryptToB64(
- file.key,
- newCollection.key,
- );
- const encryptedKey = newEncryptedKey.encryptedData;
- const keyDecryptionNonce = newEncryptedKey.nonce;
-
- fileKeysEncryptedWithNewCollection.push({
- id: file.id,
- encryptedKey,
- keyDecryptionNonce,
- });
- }
- return fileKeysEncryptedWithNewCollection;
-};
export const removeFromCollection = async (
collectionID: number,
toRemoveFiles: EnteFile[],
diff --git a/web/apps/photos/src/services/upload/upload-service.ts b/web/apps/photos/src/services/upload/upload-service.ts
index 72a475bc44..66ebe28406 100644
--- a/web/apps/photos/src/services/upload/upload-service.ts
+++ b/web/apps/photos/src/services/upload/upload-service.ts
@@ -29,6 +29,7 @@ import {
} from "@/media/file-metadata";
import { FileType, type FileTypeInfo } from "@/media/file-type";
import { encodeLivePhoto } from "@/media/live-photo";
+import { addToCollection } from "@/new/photos/services/collection";
import { extractExif } from "@/new/photos/services/exif";
import {
getNonEmptyMagicMetadataProps,
@@ -42,7 +43,6 @@ import {
import { mergeUint8Arrays } from "@/utils/array";
import { ensureInteger, ensureNumber } from "@/utils/ensure";
import { CustomError, handleUploadError } from "@ente/shared/error";
-import { addToCollection } from "services/collectionService";
import { type LivePhotoAssets } from "services/upload/uploadManager";
import * as convert from "xml-js";
import { tryParseEpochMicrosecondsFromFileName } from "./date";
diff --git a/web/apps/photos/src/services/watch.ts b/web/apps/photos/src/services/watch.ts
index bb1acdeca4..f7f6148419 100644
--- a/web/apps/photos/src/services/watch.ts
+++ b/web/apps/photos/src/services/watch.ts
@@ -13,7 +13,7 @@ import type {
} from "@/base/types/ipc";
import type { Collection } from "@/media/collection";
import { EncryptedEnteFile } from "@/media/file";
-import { groupFilesByCollectionID } from "@/new/photos/services/file";
+import { groupFilesByCollectionID } from "@/new/photos/services/files";
import { getLocalFiles } from "@/new/photos/services/files";
import { UPLOAD_RESULT } from "@/new/photos/services/upload/types";
import { ensureString } from "@/utils/ensure";
diff --git a/web/apps/photos/src/utils/collection.ts b/web/apps/photos/src/utils/collection.ts
index 90b663565c..a531e08c51 100644
--- a/web/apps/photos/src/utils/collection.ts
+++ b/web/apps/photos/src/utils/collection.ts
@@ -13,9 +13,12 @@ import { EnteFile } from "@/media/file";
import { ItemVisibility } from "@/media/file-metadata";
import {
DEFAULT_HIDDEN_COLLECTION_USER_FACING_NAME,
+ addToCollection,
findDefaultHiddenCollectionIDs,
isHiddenCollection,
isIncomingShare,
+ moveToCollection,
+ restoreToCollection,
} from "@/new/photos/services/collection";
import {
getAllLocalCollections,
@@ -28,11 +31,8 @@ import { LS_KEYS, getData } from "@ente/shared/storage/localStorage";
import type { User } from "@ente/shared/user/types";
import { t } from "i18next";
import {
- addToCollection,
createAlbum,
- moveToCollection,
removeFromCollection,
- restoreToCollection,
unhideToCollection,
updateCollectionMagicMetadata,
updatePublicCollectionMagicMetadata,
diff --git a/web/packages/media/collection.ts b/web/packages/media/collection.ts
index 910dc052af..e658ec1767 100644
--- a/web/packages/media/collection.ts
+++ b/web/packages/media/collection.ts
@@ -95,23 +95,6 @@ export interface CreatePublicAccessTokenRequest {
deviceLimit?: number;
}
-export interface EncryptedFileKey {
- id: number;
- encryptedKey: string;
- keyDecryptionNonce: string;
-}
-
-export interface AddToCollectionRequest {
- collectionID: number;
- files: EncryptedFileKey[];
-}
-
-export interface MoveToCollectionRequest {
- fromCollectionID: number;
- toCollectionID: number;
- files: EncryptedFileKey[];
-}
-
export interface collectionAttributes {
encryptedPath?: string;
pathDecryptionNonce?: string;
diff --git a/web/packages/new/photos/services/collection/index.ts b/web/packages/new/photos/services/collection/index.ts
index 7fd323b250..768ccfcae3 100644
--- a/web/packages/new/photos/services/collection/index.ts
+++ b/web/packages/new/photos/services/collection/index.ts
@@ -1,7 +1,16 @@
+import { sharedCryptoWorker } from "@/base/crypto";
+import log from "@/base/log";
+import { apiURL } from "@/base/origins";
import { SUB_TYPE, type Collection } from "@/media/collection";
+import { type EnteFile } from "@/media/file";
import { ItemVisibility } from "@/media/file-metadata";
+import { batch } from "@/utils/array";
+import HTTPService from "@ente/shared/network/HTTPService";
+import { getToken } from "@ente/shared/storage/localStorage/helpers";
import type { User } from "@ente/shared/user/types";
+export const REQUEST_BATCH_SIZE = 1000;
+
export const ARCHIVE_SECTION = -1;
export const TRASH_SECTION = -2;
export const DUMMY_UNCATEGORIZED_COLLECTION = -3;
@@ -62,3 +71,134 @@ export const getCollectionUserFacingName = (collection: Collection) => {
*/
export const createCollectionNameByID = (collections: Collection[]) =>
new Map(collections.map((c) => [c.id, getCollectionUserFacingName(c)]));
+
+export interface EncryptedFileKey {
+ id: number;
+ encryptedKey: string;
+ keyDecryptionNonce: string;
+}
+
+export interface AddToCollectionRequest {
+ collectionID: number;
+ files: EncryptedFileKey[];
+}
+
+export interface MoveToCollectionRequest {
+ fromCollectionID: number;
+ toCollectionID: number;
+ files: EncryptedFileKey[];
+}
+
+export const addToCollection = async (
+ collection: Collection,
+ files: EnteFile[],
+) => {
+ try {
+ const token = getToken();
+ const batchedFiles = batch(files, REQUEST_BATCH_SIZE);
+ for (const batch of batchedFiles) {
+ const fileKeysEncryptedWithNewCollection =
+ await encryptWithNewCollectionKey(collection, batch);
+
+ const requestBody: AddToCollectionRequest = {
+ collectionID: collection.id,
+ files: fileKeysEncryptedWithNewCollection,
+ };
+ await HTTPService.post(
+ await apiURL("/collections/add-files"),
+ requestBody,
+ null,
+ {
+ "X-Auth-Token": token,
+ },
+ );
+ }
+ } catch (e) {
+ log.error("Add to collection Failed ", e);
+ throw e;
+ }
+};
+
+export const restoreToCollection = async (
+ collection: Collection,
+ files: EnteFile[],
+) => {
+ try {
+ const token = getToken();
+ const batchedFiles = batch(files, REQUEST_BATCH_SIZE);
+ for (const batch of batchedFiles) {
+ const fileKeysEncryptedWithNewCollection =
+ await encryptWithNewCollectionKey(collection, batch);
+
+ const requestBody: AddToCollectionRequest = {
+ collectionID: collection.id,
+ files: fileKeysEncryptedWithNewCollection,
+ };
+ await HTTPService.post(
+ await apiURL("/collections/restore-files"),
+ requestBody,
+ null,
+ {
+ "X-Auth-Token": token,
+ },
+ );
+ }
+ } catch (e) {
+ log.error("restore to collection Failed ", e);
+ throw e;
+ }
+};
+export const moveToCollection = async (
+ fromCollectionID: number,
+ toCollection: Collection,
+ files: EnteFile[],
+) => {
+ try {
+ const token = getToken();
+ const batchedFiles = batch(files, REQUEST_BATCH_SIZE);
+ for (const batch of batchedFiles) {
+ const fileKeysEncryptedWithNewCollection =
+ await encryptWithNewCollectionKey(toCollection, batch);
+
+ const requestBody: MoveToCollectionRequest = {
+ fromCollectionID: fromCollectionID,
+ toCollectionID: toCollection.id,
+ files: fileKeysEncryptedWithNewCollection,
+ };
+ await HTTPService.post(
+ await apiURL("/collections/move-files"),
+ requestBody,
+ null,
+ {
+ "X-Auth-Token": token,
+ },
+ );
+ }
+ } catch (e) {
+ log.error("move to collection Failed ", e);
+ throw e;
+ }
+};
+
+const encryptWithNewCollectionKey = async (
+ newCollection: Collection,
+ files: EnteFile[],
+): Promise<EncryptedFileKey[]> => {
+ const fileKeysEncryptedWithNewCollection: EncryptedFileKey[] = [];
+ const cryptoWorker = await sharedCryptoWorker();
+ for (const file of files) {
+ const newEncryptedKey = await cryptoWorker.encryptToB64(
+ file.key,
+ newCollection.key,
+ );
+ const encryptedKey = newEncryptedKey.encryptedData;
+ const keyDecryptionNonce = newEncryptedKey.nonce;
+
+ fileKeysEncryptedWithNewCollection.push({
+ id: file.id,
+ encryptedKey,
+ keyDecryptionNonce,
+ });
+ }
+ return fileKeysEncryptedWithNewCollection;
+};