This commit is contained in:
Manav Rathi
2025-07-01 13:24:38 +05:30
parent c2b6bfa1ed
commit c9175bee04
3 changed files with 60 additions and 4 deletions

View File

@@ -35,9 +35,24 @@ export const publicRequestHeaders = () => ({
*/
export interface PublicAlbumsCredentials {
/**
* An access token that does the same job as the "X-Auth-Token" for usual
* authenticated API requests, except it will be passed as the
* ""X-Auth-Access-Token" header.
* [Note: Public album access token]
*
* The public album access is a token that serves a similar purpose as the
* "X-Auth-Token" for usual authenticated API requests that happen for a
* logged in user, except:
*
* - It will be passed as the "X-Auth-Access-Token" header, and
* - It also tells remote about the public album under consideration.
*
* This access token is variously referred to as the album token, or the
* auth token, when the context is clear. The client obtains this from the
* "t" query parameter of a public album URL, and then uses it both to:
*
* 1. Identify and authenticate itself with remote (this header).
*
* 2. Scope local storage per public album by using this access token as a
* part of the local storage key. In this context it is sometimes also
* referred to as a "collectionUID" by old code.
*/
accessToken: string;
/**

View File

@@ -2,9 +2,15 @@
* @file Public albums app specific files DB. See: [Note: Files DB].
*/
import { LocalCollections } from "ente-gallery/services/files-db";
import {
LocalCollections,
LocalEnteFiles,
transformFilesIfNeeded,
} from "ente-gallery/services/files-db";
import { type Collection } from "ente-media/collection";
import type { EnteFile } from "ente-media/file";
import localForage from "ente-shared/storage/localForage";
import { z } from "zod/v4";
/**
* Return all public collections present in our local database.
@@ -29,3 +35,35 @@ export const savedPublicCollections = async (): Promise<Collection[]> =>
*/
export const savePublicCollections = (collections: Collection[]) =>
localForage.setItem("public-collections", collections);
const LocalSavedPublicCollectionFilesEntry = z.object({
/**
* The collection, identified by its access token.
*
* See: [Note: Public album access token]
*/
collectionUID: z.string(),
files: LocalEnteFiles,
});
type LocalSavedPublicCollectionFilesEntry = z.infer<
typeof LocalSavedPublicCollectionFilesEntry
>;
/**
* Return all files for a public collection present in our local database.
*
* Use {@link savePublicCollectionFiles} to update the database.
*
* @param accessToken The access token of the public album whose files we want.
*/
export const savedPublicCollectionFiles = async (
accessToken: string,
): Promise<EnteFile[]> => {
type ES = LocalSavedPublicCollectionFilesEntry[];
// See: [Note: Avoiding Zod parsing for large DB arrays] for why we use an
// (implied) cast here instead of parsing using the Zod schema.
const entries = await localForage.getItem<ES>("public-collection-files");
const entry = (entries ?? []).find((e) => e.collectionUID == accessToken);
return transformFilesIfNeeded(entry ? entry.files : []);
};

View File

@@ -154,6 +154,9 @@ export const savedCollectionFiles = async (): Promise<EnteFile[]> => {
// As an optimization, we skip the runtime check here and cast. This might
// not be the most optimal choice in the future, so (a) use it sparingly,
// and (b) mark all such cases with the title of this note.
//
// Note that the cast is happening inside the local forage code since we're
// passing a type parameter.
let files = (await localForage.getItem<EnteFile[]>("files")) ?? [];
// Previously hidden files were stored separately. If that key is present,