Skip to content

armgit / ente

Public Imported from GitHub
0

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

3.0 KB
Raw Blame
import { deriveKey } from "ente-base/crypto";
import { fetchPublicCollectionFile } from "ente-base/file-download";
import {
    authenticatedPublicAlbumsDeviceLimitRequestHeaders,
    authenticatedPublicAlbumsInfoRequestHeaders,
    authenticatedPublicAlbumsRequestHeaders,
    ensureOk,
    linkDeviceTokenFromResponse,
    type PublicAlbumsCredentials,
} from "ente-base/http";
import { apiURL, customAPIOrigin } from "ente-base/origins";
import { z } from "zod";
import { RemoteCollection, type PublicURL } from "./collection";
import { FileDiffResponse } from "./file";

const PublicCollectionInfo = z.object({
    collection: RemoteCollection,
    referralCode: z.string(),
});

export const getPublicCollectionInfo = async (
    credentials: PublicAlbumsCredentials,
) => {
    const res = await fetch(await apiURL("/public-collection/info"), {
        headers: authenticatedPublicAlbumsInfoRequestHeaders(credentials),
    });
    ensureOk(res);
    const info = PublicCollectionInfo.parse(await res.json());
    return { ...info, linkDeviceToken: linkDeviceTokenFromResponse(res) };
};

export const verifyPublicCollectionPassword = async (
    credentials: PublicAlbumsCredentials,
    publicURL: PublicURL,
    password: string,
) => {
    const passHash = await deriveKey(
        password,
        publicURL.nonce!,
        publicURL.opsLimit!,
        publicURL.memLimit!,
    );
    const res = await fetch(
        await apiURL("/public-collection/verify-password"),
        {
            method: "POST",
            headers: {
                ...authenticatedPublicAlbumsRequestHeaders(credentials),
                "Content-Type": "application/json",
            },
            body: JSON.stringify({ passHash }),
        },
    );
    ensureOk(res);
    return z.object({ jwtToken: z.string() }).parse(await res.json()).jwtToken;
};

export const getPublicCollectionDiff = async (
    credentials: PublicAlbumsCredentials,
    sinceTime: number,
) => {
    const res = await fetch(
        await apiURL("/public-collection/diff", { sinceTime }),
        {
            headers:
                authenticatedPublicAlbumsDeviceLimitRequestHeaders(credentials),
        },
    );
    ensureOk(res);
    return FileDiffResponse.parse(await res.json());
};

export const downloadPublicCollectionFile = async (
    credentials: PublicAlbumsCredentials,
    fileID: number,
    thumbnail: boolean,
    downloadOrigin?: string,
) => {
    const customOrigin = await customAPIOrigin();
    const origin =
        downloadOrigin ?? customOrigin ?? "https://public-albums.ente.com";
    const path = thumbnail ? "preview" : "download";
    const res =
        customOrigin && origin === customOrigin
            ? await fetchPublicCollectionFile(
                  fileID,
                  thumbnail ? "thumbnail" : "file",
                  credentials,
              )
            : await fetch(`${origin}/${path}/?fileID=${fileID}`, {
                  headers: authenticatedPublicAlbumsRequestHeaders(credentials),
              });
    ensureOk(res);
    return new Uint8Array(await res.arrayBuffer());
};