Skip to content

armgit / ente

Public Imported from GitHub
0

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

Swap

Manav Rathib752af70469e
diff --git a/web/apps/photos/src/pages/shared-albums.tsx b/web/apps/photos/src/pages/shared-albums.tsx
index 34d82c9f34..81cb4fe472 100644
--- a/web/apps/photos/src/pages/shared-albums.tsx
+++ b/web/apps/photos/src/pages/shared-albums.tsx
@@ -58,7 +58,10 @@ import {
savedPublicCollectionFiles,
savePublicCollectionAccessTokenJWT,
} from "ente-new/albums/services/public-albums-fdb";
-import { verifyPublicAlbumPassword } from "ente-new/albums/services/public-collection";
+import {
+ fetchAndSavePublicCollection,
+ verifyPublicAlbumPassword,
+} from "ente-new/albums/services/public-collection";
import {
GalleryItemsHeaderAdapter,
GalleryItemsSummary,
@@ -72,7 +75,6 @@ import { useRouter } from "next/router";
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
import { type FileWithPath } from "react-dropzone";
import {
- getPublicCollection,
getPublicCollectionUID,
removePublicCollectionWithFiles,
removePublicFiles,
@@ -299,10 +301,11 @@ export default function PublicCollectionGallery() {
try {
showLoadingBar();
setLoading(true);
- const [collection, userReferralCode] = await getPublicCollection(
- credentials.current.accessToken,
- collectionKey.current,
- );
+ const { collection, referralCode: userReferralCode } =
+ await fetchAndSavePublicCollection(
+ credentials.current.accessToken,
+ collectionKey.current,
+ );
referralCode.current = userReferralCode;
setPublicCollection(collection);
diff --git a/web/apps/photos/src/services/publicCollectionService.ts b/web/apps/photos/src/services/publicCollectionService.ts
index e51653efd0..360ad750dc 100644
--- a/web/apps/photos/src/services/publicCollectionService.ts
+++ b/web/apps/photos/src/services/publicCollectionService.ts
@@ -1,11 +1,7 @@
-import { sharedCryptoWorker } from "ente-base/crypto";
import log from "ente-base/log";
import { apiURL } from "ente-base/origins";
import { sortFiles } from "ente-gallery/utils/file";
-import type {
- Collection,
- CollectionPublicMagicMetadataData,
-} from "ente-media/collection";
+import type { Collection } from "ente-media/collection";
import type { EnteFile, RemoteEnteFile } from "ente-media/file";
import { decryptRemoteFile } from "ente-media/file";
import {
@@ -15,8 +11,6 @@ import {
removePublicCollectionLastSyncTime,
savedPublicCollectionFiles,
savedPublicCollectionLastSyncTime,
- saveLastPublicCollectionReferralCode,
- savePublicCollection,
savePublicCollectionFiles,
savePublicCollectionLastSyncTime,
} from "ente-new/albums/services/public-albums-fdb";
@@ -167,73 +161,6 @@ const getPublicFiles = async (
}
};
-export interface MagicMetadataCore<T> {
- version: number;
- count: number;
- header: string;
- data: T;
-}
-
-export const getPublicCollection = async (
- token: string,
- collectionKey: string,
-): Promise<[Collection, string]> => {
- try {
- if (!token) {
- return;
- }
- const resp = await HTTPService.get(
- await apiURL("/public-collection/info"),
- null,
- { "X-Auth-Access-Token": token },
- );
- const fetchedCollection = resp.data.collection;
- const referralCode = resp.data.referralCode ?? "";
-
- const cryptoWorker = await sharedCryptoWorker();
-
- const collectionName = (fetchedCollection.name =
- fetchedCollection.name ||
- new TextDecoder().decode(
- await cryptoWorker.decryptBoxBytes(
- {
- encryptedData: fetchedCollection.encryptedName,
- nonce: fetchedCollection.nameDecryptionNonce,
- },
- collectionKey,
- ),
- ));
-
- let collectionPublicMagicMetadata: MagicMetadataCore<CollectionPublicMagicMetadataData>;
- if (fetchedCollection.pubMagicMetadata?.data) {
- collectionPublicMagicMetadata = {
- ...fetchedCollection.pubMagicMetadata,
- data: await cryptoWorker.decryptMetadataJSON(
- {
- encryptedData: fetchedCollection.pubMagicMetadata.data,
- decryptionHeader:
- fetchedCollection.pubMagicMetadata.header,
- },
- collectionKey,
- ),
- };
- }
-
- const collection = {
- ...fetchedCollection,
- name: collectionName,
- key: collectionKey,
- pubMagicMetadata: collectionPublicMagicMetadata,
- };
- await savePublicCollection(collection);
- await saveLastPublicCollectionReferralCode(referralCode);
- return [collection, referralCode];
- } catch (e) {
- log.error("failed to get public collection", e);
- throw e;
- }
-};
-
export const removePublicCollectionWithFiles = async (
collectionUID: string,
collectionKey: string,
diff --git a/web/packages/new/albums/services/public-collection.ts b/web/packages/new/albums/services/public-collection.ts
index 679fa19a81..c4aa363ddd 100644
--- a/web/packages/new/albums/services/public-collection.ts
+++ b/web/packages/new/albums/services/public-collection.ts
@@ -63,21 +63,6 @@ export const verifyPublicAlbumPassword = async (
return z.object({ jwtToken: z.string() }).parse(await res.json()).jwtToken;
};
-// TODO(RE): Use me
-export const PublicCollectionInfo = z.object({
- collection: RemoteCollection,
- /**
- * A referral code of the owner of the public album.
- *
- * [Note: Public albums referral code]
- *
- * The information of a public collection contains the referral code of the
- * person who shared the album. This allows both the viewer and the sharer
- * to gain storage bonus.
- */
- referralCode: z.string(),
-});
-
/**
* Fetch a public collection from remote using its access key, decrypt it using
* the provided key, save the collection in our local database for subsequent
@@ -101,19 +86,35 @@ export const PublicCollectionInfo = z.object({
export const fetchAndSavePublicCollection = async (
accessToken: string,
collectionKey: string,
-): Promise<Collection> => {
- const collectionInfo = await getPublicCollectionInfo(accessToken);
+): Promise<{ collection: Collection; referralCode: string }> => {
+ const { collection: remoteCollection, referralCode } =
+ await getPublicCollectionInfo(accessToken);
+
const collection = await decryptRemoteCollection(
- collectionInfo.collection,
+ remoteCollection,
collectionKey,
);
await savePublicCollection(collection);
- await saveLastPublicCollectionReferralCode(collectionInfo.referralCode);
+ await saveLastPublicCollectionReferralCode(referralCode);
- return collection;
+ return { collection, referralCode };
};
+const PublicCollectionInfo = z.object({
+ collection: RemoteCollection,
+ /**
+ * A referral code of the owner of the public album.
+ *
+ * [Note: Public albums referral code]
+ *
+ * The information of a public collection contains the referral code of the
+ * person who shared the album. This allows both the viewer and the sharer
+ * to gain storage bonus.
+ */
+ referralCode: z.string(),
+});
+
/**
* Fetch information from remote about a public collection using its access key.
*