Skip to content

armgit / ente

Public Imported from GitHub
0

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

Use new type in cast

Manav Rathid49288a1c0c8
diff --git a/web/apps/cast/src/services/render.ts b/web/apps/cast/src/services/render.ts
index 72cf1256df..9300364bf3 100644
--- a/web/apps/cast/src/services/render.ts
+++ b/web/apps/cast/src/services/render.ts
@@ -1,35 +1,25 @@
-/* TODO: Various lint issues in the decryptEnteFile function */
-/* eslint-disable @typescript-eslint/no-unsafe-call */
-/* eslint-disable @typescript-eslint/no-unsafe-argument */
-/* eslint-disable @typescript-eslint/no-unnecessary-condition */
-/* eslint-disable @typescript-eslint/no-unsafe-member-access */
-/* eslint-disable @typescript-eslint/no-unsafe-assignment */
-
-import type { AxiosResponse } from "axios";
import { sharedCryptoWorker } from "ente-base/crypto";
import { nameAndExtension } from "ente-base/file-name";
import { ensureOk, publicRequestHeaders } from "ente-base/http";
import log from "ente-base/log";
import { apiURL, customAPIOrigin } from "ente-base/origins";
import {
+ decryptRemoteFile,
+ FileDiffResponse,
mergeMetadata1,
RemoteEnteFile,
- type EncryptedEnteFile,
type EnteFile,
- type FileMagicMetadata,
- type FilePublicMagicMetadata,
+ type EnteFile2,
} from "ente-media/file";
import { FileType } from "ente-media/file-type";
import { isHEICExtension, needsJPEGConversion } from "ente-media/formats";
import { heicToJPEG } from "ente-media/heic-convert";
import { decodeLivePhoto } from "ente-media/live-photo";
import { ApiError } from "ente-shared/error";
-import HTTPService from "ente-shared/network/HTTPService";
import { shuffled } from "ente-utils/array";
import { wait } from "ente-utils/promise";
import type { CastData } from "services/cast-data";
import { detectMediaMIMEType } from "services/detect-type";
-import { z } from "zod/v4";
import { isChromecast } from "./chromecast-receiver";
/**
@@ -94,13 +84,19 @@ export const imageURLGenerator = async function* (castData: CastData) {
while (true) {
const encryptedFiles = shuffled(
- await getEncryptedCollectionFiles(castToken),
+ await getRemoteCastCollectionFiles(castToken),
);
let haveEligibleFiles = false;
for (const encryptedFile of encryptedFiles) {
- const file = await decryptEnteFile(encryptedFile, collectionKey);
+ // TODO(RE):
+ const file = mergeMetadata1(
+ (await decryptRemoteFile(
+ encryptedFile,
+ collectionKey,
+ )) as EnteFile,
+ ) as EnteFile2;
if (!isFileEligible(file)) continue;
@@ -153,39 +149,6 @@ export const imageURLGenerator = async function* (castData: CastData) {
}
};
-/**
- * Fetch the list of non-deleted files in the given collection.
- *
- * The returned files are not decrypted yet, so their metadata will not be
- * readable.
- */
-const getEncryptedCollectionFiles = async (
- castToken: string,
-): Promise<EncryptedEnteFile[]> => {
- let files: EncryptedEnteFile[] = [];
- let sinceTime = 0;
- let resp: AxiosResponse;
- do {
- resp = await HTTPService.get(
- await apiURL("/cast/diff"),
- { sinceTime },
- { "X-Cast-Access-Token": castToken },
- );
- const diff = resp.data.diff;
- files = files.concat(diff.filter((file: EnteFile) => !file.isDeleted));
- sinceTime = diff.reduce(
- (max: number, file: EnteFile) => Math.max(max, file.updationTime),
- sinceTime,
- );
- } while (resp.data.hasMore);
- return files;
-};
-
-const CastCollectionFilesResponse = z.object({
- diff: RemoteEnteFile.array(),
- hasMore: z.boolean(),
-});
-
/**
* Fetch all the {@link RemoteEnteFile}s present in the collection corresponding
* to the given {@link castToken}.
@@ -198,7 +161,7 @@ const CastCollectionFilesResponse = z.object({
*/
export const getRemoteCastCollectionFiles = async (
castToken: string,
-): Promise<RemoteEnteFile> => {
+): Promise<RemoteEnteFile[]> => {
const files: RemoteEnteFile[] = [];
let sinceTime = 0;
while (true) {
@@ -207,85 +170,23 @@ export const getRemoteCastCollectionFiles = async (
{ headers: { "X-Cast-Access-Token": castToken } },
);
ensureOk(res);
- const { diff, hasMore } = CastCollectionFilesResponse.parse(
- await res.json(),
- );
+ const { diff, hasMore } = FileDiffResponse.parse(await res.json());
if (!diff.length) break;
- if (!hasMore) break;
for (const change of diff) {
sinceTime = Math.max(sinceTime, change.updationTime);
if (!change.isDeleted) {
files.push(change);
}
}
+ if (!hasMore) break;
}
return files;
};
-/**
- * Decrypt the given {@link EncryptedEnteFile}, returning a {@link EnteFile}.
- */
-const decryptEnteFile = async (
- encryptedFile: EncryptedEnteFile,
- collectionKey: string,
-): Promise<EnteFile> => {
- const worker = await sharedCryptoWorker();
- const {
- encryptedKey,
- keyDecryptionNonce,
- metadata,
- magicMetadata,
- pubMagicMetadata,
- ...restFileProps
- } = encryptedFile;
- const fileKey = await worker.decryptBox(
- { encryptedData: encryptedKey, nonce: keyDecryptionNonce },
- collectionKey,
- );
- const fileMetadata = await worker.decryptMetadataJSON(metadata, fileKey);
- let fileMagicMetadata: FileMagicMetadata | undefined;
- let filePubMagicMetadata: FilePublicMagicMetadata | undefined;
- if (magicMetadata?.data) {
- fileMagicMetadata = {
- ...encryptedFile.magicMetadata,
- // @ts-expect-error TODO: Need to use zod here.
- data: await worker.decryptMetadataJSON(
- {
- encryptedData: magicMetadata.data,
- decryptionHeader: magicMetadata.header,
- },
- fileKey,
- ),
- };
- }
- if (pubMagicMetadata?.data) {
- filePubMagicMetadata = {
- ...pubMagicMetadata,
- // @ts-expect-error TODO: Need to use zod here.
- data: await worker.decryptMetadataJSON(
- {
- encryptedData: pubMagicMetadata.data,
- decryptionHeader: pubMagicMetadata.header,
- },
- fileKey,
- ),
- };
- }
- return mergeMetadata1({
- ...restFileProps,
- key: fileKey,
- // @ts-expect-error The types need to be updated here
- metadata: fileMetadata,
- magicMetadata: fileMagicMetadata,
- pubMagicMetadata: filePubMagicMetadata,
- });
-};
-
-const isFileEligible = (file: EnteFile) => {
+const isFileEligible = (file: EnteFile2) => {
if (!isImageOrLivePhoto(file)) return false;
- // @ts-expect-error TODO: The core types need to be updated to allow the
- // possibility of missing info fields (or do they?)
- if (file.info.fileSize > 100 * 1024 * 1024) return false;
+
+ if ((file.info?.fileSize ?? 0) > 100 * 1024 * 1024) return false;
// This check is fast but potentially incorrect because in practice we do
// encounter files that are incorrectly named and have a misleading
@@ -300,7 +201,7 @@ const isFileEligible = (file: EnteFile) => {
return true;
};
-const isImageOrLivePhoto = (file: EnteFile) => {
+const isImageOrLivePhoto = (file: EnteFile2) => {
const fileType = file.metadata.fileType;
return fileType == FileType.image || fileType == FileType.livePhoto;
};
@@ -312,12 +213,12 @@ const isImageOrLivePhoto = (file: EnteFile) => {
* Once we're done showing the file, the URL should be revoked using
* {@link URL.revokeObjectURL} to free up browser resources.
*/
-const createRenderableURL = async (castToken: string, file: EnteFile) => {
+const createRenderableURL = async (castToken: string, file: EnteFile2) => {
const imageBlob = await renderableImageBlob(castToken, file);
return URL.createObjectURL(imageBlob);
};
-const renderableImageBlob = async (castToken: string, file: EnteFile) => {
+const renderableImageBlob = async (castToken: string, file: EnteFile2) => {
const shouldUseThumbnail = isChromecast();
let blob = await downloadFile(castToken, file, shouldUseThumbnail);
@@ -347,7 +248,7 @@ const renderableImageBlob = async (castToken: string, file: EnteFile) => {
const downloadFile = async (
castToken: string,
- file: EnteFile,
+ file: EnteFile2,
shouldUseThumbnail: boolean,
) => {
if (!isImageOrLivePhoto(file))
diff --git a/web/packages/media/collection.ts b/web/packages/media/collection.ts
index 78fcf9325e..81d90909e6 100644
--- a/web/packages/media/collection.ts
+++ b/web/packages/media/collection.ts
@@ -458,6 +458,8 @@ export const decryptRemoteCollection = async (
// RemoteCollection is a looseObject, and we want to retain that semantic
// for the parsed Collection. Mention all fields that we want to explicitly
// drop or transform, passthrough the rest unchanged in the return value.
+ //
+ // See: [Note: Use looseObject when parsing JSON that will get persisted].
const {
owner,
encryptedKey,
diff --git a/web/packages/media/file.ts b/web/packages/media/file.ts
index be10defd13..2afc00bfbf 100644
--- a/web/packages/media/file.ts
+++ b/web/packages/media/file.ts
@@ -1,21 +1,178 @@
-import { sharedCryptoWorker } from "ente-base/crypto";
+import {
+ decryptBox,
+ decryptMetadataJSON,
+ sharedCryptoWorker,
+} from "ente-base/crypto";
import { dateFromEpochMicroseconds } from "ente-base/date";
import log from "ente-base/log";
import { nullishToBlank, nullToUndefined } from "ente-utils/transform";
import { z } from "zod/v4";
+import { ignore } from "./collection";
import { type Metadata, ItemVisibility } from "./file-metadata";
import { FileType } from "./file-type";
-import { RemoteMagicMetadata } from "./magic-metadata";
+import { decryptMagicMetadata, RemoteMagicMetadata } from "./magic-metadata";
-export interface MagicMetadataCore<T> {
- version: number;
- count: number;
- header: string;
- data: T;
+/**
+ * A File.
+ *
+ * An EnteFile represents a file in Ente. It does not contain the actual data.
+ *
+ * It is named with an "Ente" prefix to disambiguate it from the web's native
+ * {@link File} type.
+ *
+ * All files have an id (numeric) that is unique across all the files stored by
+ * an Ente instance. Each file is also always associated with a collection, and
+ * has an owner (both of these linkages are stored as the corresponding numeric
+ * IDs within the EnteFile structure).
+ *
+ * > For shared files, the owner of the file is not necessarily the owner of all
+ * > the collections to which the file belongs.
+ *
+ * While the file ID is unique, we'd can still have multiple entries for each
+ * file ID in our local state, one for each {@link Collection} to which the file
+ * belongs. That is, the uniqueness is across the (fileID, collectionID) pairs.
+ * See [Note: Collection file].
+ *
+ * [Note: File lifecycle]
+ *
+ * 1. **Normal**: A file starts off by belonging to at least one collection. It
+ * can then be added to additional collections, each of which will be an
+ * EnteFile with a distinct (fileID, collectionID) pair.
+ *
+ * 2. **Trash**: If the user deletes the file, then the file moves to trash.
+ * Such files will have {@link isDeleted} `true` in their
+ * {@link RemoteEnteFile} entries when we perform a collection diff to let
+ * the client know that the corresponding file is no longer part of the
+ * collection.
+ *
+ * Such files will now be returned as part of the trash diff, with both
+ * {@link isDeleted} and {@link isRestored} set to `false` for their trash
+ * entry.
+ *
+ * 3. **Restore**: If the the user were to restore the file before permanent
+ * deletion, then it effectively works as moving a normal file to an normal
+ * collection. In particular, if we were to restore the file back to one of
+ * the collections it was part of pre-trash, and then fetch the collection
+ * diff for that collection, we would get back the same file object, except
+ * with {@link isDeleted} will now be `false`.
+ *
+ * The trash entry for restored files will have {@link isRestored} set to
+ * `true` to notify clients that an item that was previously in trash is no
+ * longer there because it has been restored.
+ *
+ * 4. **Permanent deletion**: If the file remains in trash for 30 days, or if
+ * the user explictly permanently deletes it from the trash, or if the user
+ * explicitly clears the trash, then it will get permanently deleted.
+ *
+ * The trash entry for such permanently deleted files will have
+ * {@link isDeleted} set to `true` to notify clients that an item that was
+ * previously in trash is no longer there because it has been permanently
+ * deleted.
+ *
+ * When a file is permanently deleted, remote will scrub off data from its
+ * fields. See: [Note: Optionality of remote file fields].
+ */
+export interface EnteFile2 {
+ /**
+ * The file's globally unique ID.
+ *
+ * The file's ID is a integer assigned by remote as the identifier for an
+ * {@link EnteFile} when it is created. It is globally unique across all
+ * files stored by an Ente instance, and is not scoped to the current user.
+ */
+ id: number;
+ /**
+ * The ID of the collection with which this file as associated.
+ *
+ * The same file (ID) may be associated with multiple collectionID, each of
+ * which will come and stay as distinct {@link EnteFile} instances - all of
+ * which will have the same {@link id} but distinct {@link collectionID}.
+ *
+ * So the ({@link id}, {@link collectionID}) pair is a primary key, not the
+ * {@link id} on its own. See: [Note: Collection file].
+ */
+ collectionID: number;
+ /**
+ * The ID of the Ente user who owns the file.
+ *
+ * Files uploaded by non users on public links belong to the owner of the
+ * collection who created the public link (See {@link uploaderName} in
+ * {@link FilePublicMagicMetadataData}).
+ */
+ ownerID: number;
+ /**
+ * The file's key.
+ *
+ * This is the base64 representation of the decrypted encryption key
+ * associated with this file. When we get the file from remote (as a
+ * {@link RemoteEnteFile}), the file key itself would have been encrypted by
+ * the key of the {@link Collection} to which this file belongs.
+ *
+ * This key is used to encrypt both the file's contents, and any associated
+ * data (e.g., metadatum, thumbnail) for the file.
+ */
+ key: string;
+ /**
+ * Information pertaining to the encrypted S3 object that has the file's
+ * contents.
+ */
+ file: FileObjectAttributes;
+ /**
+ * Information pertaining to the encrypted S3 object that has the contents
+ * of the file's thumbnail.
+ */
+ thumbnail: FileObjectAttributes;
+ /**
+ * Static, remote visible, information associated with a file.
+ *
+ * This is information about storage used by the file and its thumbnail.
+ * Unlike {@link metadata} which is E2EE, the {@link FileInfo} is remote
+ * visible for bookkeeping purposes.
+ *
+ * Files uploaded by very old versions of Ente might not have this field.
+ */
+ info?: FileInfo;
+ /**
+ * The last time the file was updated (epoch microseconds).
+ *
+ * (e.g. magic metadata updates).
+ */
+ updationTime: number;
+ /**
+ * Public static metadata associated with a file.
+ *
+ * This is the immutable metadata that gets associated with a file when it
+ * is uploaded, and there after cannot be changed.
+ *
+ * It is visible to all users with whom the file gets shared.
+ *
+ * > {@link pubMagicMetadata} contains fields that override fields present
+ * > in the metadata. Clients overlay those atop the metadata fields, and
+ * > thus they can be used to implement edits.
+ *
+ * See: [Note: Metadatum].
+ */
+ metadata: Metadata;
+ /**
+ * Private mutable metadata associated with the file that is only visible to
+ * the owner of the file.
+ *
+ * See: [Note: Metadatum]
+ */
+ magicMetadata?: FileMagicMetadata;
+ /**
+ * Public mutable metadata associated with the file that is visible to all
+ * users with whom the file has been shared.
+ *
+ * While in almost all cases, files will have associated public magic
+ * metadata since newer clients have something or the other they need to add
+ * to it, its presence is not guaranteed.
+ *
+ * See: [Note: Metadatum]
+ */
+ pubMagicMetadata?: FilePublicMagicMetadata;
}
-export type EncryptedMagicMetadata = MagicMetadataCore<string>;
-
export interface EncryptedEnteFile {
/**
* The file's globally unique ID.
@@ -92,66 +249,6 @@ export interface EncryptedEnteFile {
updationTime: number;
}
-/**
- * A File.
- *
- * An EnteFile represents a file in Ente. It does not contain the actual data.
- *
- * It is named with an "Ente" prefix to disambiguate it from the web's native
- * {@link File} type.
- *
- * All files have an id (numeric) that is unique across all the files stored by
- * an Ente instance. Each file is also always associated with a collection, and
- * has an owner (both of these linkages are stored as the corresponding numeric
- * IDs within the EnteFile structure).
- *
- * > For shared files, the owner of the file is not necessarily the owner of all
- * > the collections to which the file belongs.
- *
- * While the file ID is unique, we'd can still have multiple entries for each
- * file ID in our local state, one for each {@link Collection} to which the file
- * belongs. That is, the uniqueness is across the (fileID, collectionID) pairs.
- * See [Note: Collection file].
- *
- * [Note: File lifecycle]
- *
- * 1. **Normal**: A file starts off by belonging to at least one collection. It
- * can then be added to additional collections, each of which will be an
- * EnteFile with a distinct (fileID, collectionID) pair.
- *
- * 2. **Trash**: If the user deletes the file, then the file moves to trash.
- * Such files will have {@link isDeleted} `true` in their
- * {@link RemoteEnteFile} entries when we perform a collection diff to let
- * the client know that the corresponding file is no longer part of the
- * collection.
- *
- * Such files will now be returned as part of the trash diff, with both
- * {@link isDeleted} and {@link isRestored} set to `false` for their trash
- * entry.
- *
- * 3. **Restore**: If the the user were to restore the file before permanent
- * deletion, then it effectively works as moving a normal file to an normal
- * collection. In particular, if we were to restore the file back to one of
- * the collections it was part of pre-trash, and then fetch the collection
- * diff for that collection, we would get back the same file object, except
- * with {@link isDeleted} will now be `false`.
- *
- * The trash entry for restored files will have {@link isRestored} set to
- * `true` to notify clients that an item that was previously in trash is no
- * longer there because it has been restored.
- *
- * 4. **Permanent deletion**: If the file remains in trash for 30 days, or if
- * the user explictly permanently deletes it from the trash, or if the user
- * explicitly clears the trash, then it will get permanently deleted.
- *
- * The trash entry for such permanently deleted files will have
- * {@link isDeleted} set to `true` to notify clients that an item that was
- * previously in trash is no longer there because it has been permanently
- * deleted.
- *
- * When a file is permanently deleted, remote will scrub off data from its
- * fields. See: [Note: Optionality of remote file fields].
- */
export interface EnteFile
extends Omit<
EncryptedEnteFile,
@@ -360,6 +457,28 @@ export const RemoteEnteFile = z.looseObject({
export type RemoteEnteFile = z.infer<typeof RemoteEnteFile>;
+/**
+ * Zod schema for a response for various "/diff" APIs that return changes since
+ * a provided timestamp.
+ *
+ * - "/cast/diff"
+ *
+ */
+export const FileDiffResponse = z.object({
+ /**
+ * A batch of changes (upserts or deletions).
+ *
+ * See also: [Note: Diff response will have at most one entry for an id].
+ */
+ diff: RemoteEnteFile.array(),
+ /**
+ * If `true`, then there are more changes that can be fetched by doing
+ * another of the same API call, this time passing it the latest from
+ * amongst the timestamps of entries in {@link diff}.
+ */
+ hasMore: z.boolean(),
+});
+
export interface FileWithUpdatedMagicMetadata {
file: EnteFile;
updatedMagicMetadata: FileMagicMetadata;
@@ -546,6 +665,82 @@ export async function decryptFile(
}
}
+/**
+ * Decrypt a remote file using the provided {@link collectionKey}.
+ *
+ * @param file The remote file to decrypt.
+ *
+ * @param collectionKey The base64 encoded key of the collection to which this
+ * file belongs. It is needed to decrypt the file's key (which in turn is needed
+ * to decrypt the file's contents and other encrypted fields).
+ *
+ * @returns A decrypted {@link EnteFile}.
+ */
+export const decryptRemoteFile = async (
+ remoteFile: RemoteEnteFile,
+ collectionKey: string,
+): Promise<EnteFile2> => {
+ // RemoteEnteFile is a looseObject, and we want to retain that semantic for
+ // the parsed EnteFile. Mention all fields that we want to explicitly drop
+ // or transform, passthrough the rest unchanged in the return value.
+ //
+ // See: [Note: Use looseObject when parsing JSON that will get persisted].
+ const {
+ encryptedKey,
+ keyDecryptionNonce,
+ isDeleted,
+ metadata: encryptedMetadata,
+ magicMetadata: encryptedMagicMetadata,
+ pubMagicMetadata: encryptedPubMagicMetadata,
+ ...rest
+ } = remoteFile;
+
+ // This flag is relevant only in the diff response, where it indicates that
+ // the file should be removed from the corresponding collection. We would've
+ // already acted on that information, so drop it from our local state.
+ ignore(isDeleted);
+
+ const key = await decryptBox(
+ { encryptedData: encryptedKey, nonce: keyDecryptionNonce },
+ collectionKey,
+ );
+
+ const metadata = await decryptMetadataJSON(encryptedMetadata, key);
+
+ let magicMetadata: EnteFile2["magicMetadata"];
+ if (encryptedMagicMetadata) {
+ const genericMM = await decryptMagicMetadata(
+ encryptedMagicMetadata,
+ key,
+ );
+ // TODO(RE):
+ const data = genericMM.data as FileMagicMetadataProps;
+ // TODO(RE):
+ magicMetadata = { ...genericMM, header: "", data };
+ }
+
+ let pubMagicMetadata: EnteFile2["pubMagicMetadata"];
+ if (encryptedPubMagicMetadata) {
+ const genericMM = await decryptMagicMetadata(
+ encryptedPubMagicMetadata,
+ key,
+ );
+ // TODO(RE):
+ const data = genericMM.data as FilePublicMagicMetadataProps;
+ // TODO(RE):
+ pubMagicMetadata = { ...genericMM, header: "", data };
+ }
+
+ return {
+ ...rest,
+ key,
+ // TODO(RE):n
+ metadata: metadata as Metadata,
+ magicMetadata,
+ pubMagicMetadata,
+ };
+};
+
/**
* Update the immutable fields of an (in-memory) {@link EnteFile} with any edits
* that the user has made to their corresponding mutable metadata fields.
@@ -601,3 +796,12 @@ export const mergeMetadata1 = (file: EnteFile): EnteFile => {
*/
export const mergeMetadata = (files: EnteFile[]) =>
files.map((file) => mergeMetadata1(file));
+
+export interface MagicMetadataCore<T> {
+ version: number;
+ count: number;
+ header: string;
+ data: T;
+}
+
+export type EncryptedMagicMetadata = MagicMetadataCore<string>;