π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Fin2
Manav Rathi3b63089b42b0
diff --git a/web/packages/new/photos/services/ml/embedding.ts b/web/packages/new/photos/services/ml/ml-data.tssimilarity index 59%rename from web/packages/new/photos/services/ml/embedding.tsrename to web/packages/new/photos/services/ml/ml-data.tsindex 0008e1b79e..154e78630c 100644--- a/web/packages/new/photos/services/ml/embedding.ts+++ b/web/packages/new/photos/services/ml/ml-data.ts@@ -8,40 +8,63 @@ import { gunzip, gzip } from "../gzip";import { type RemoteCLIPIndex } from "./clip";import { type RemoteFaceIndex } from "./face";-export type RawRemoteMLData = Record<string, unknown>;--export type ParsedRemoteMLData = Partial<{- face: RemoteFaceIndex;- clip: RemoteCLIPIndex;-}>;-/**- * The decrypted payload of a {@link RemoteFileData} for the "derived"+ * The decrypted payload of a {@link RemoteFileData} for the "mldata"* {@link FileDataType}.*+ * [Note: "mldata" format]+ *+ * The file data of type "mldata" contains various embeddings and other metadata+ * generated by the ML indexing for a given file.+ *+ * For example, the face indexing process generates multiple face embeddings per+ * file (one for each face), each with an associated detection box. So instead+ * of storing just a singular embedding, the data is an entire face index+ * structure containing multiple embeddings and associated data.+ *+ * Similarly, the CLIP indexing process generates a CLIP embedding with some+ * associated data.+ *+ * All such indexes are merge into a single object of the form:+ *+ * {+ * "face": ... the face indexing result ...+ * "clip": ... the CLIP indexing result ...+ * ... more in the future ...+ * }+ *+ * which is then gzipped to get the plaintext data to upload.+ ** [Note: Preserve unknown derived data fields]*- * The remote derived data can contain arbitrary key at the top level apart from- * the ones that the current client knows about. We need to preserve these- * verbatim when we use {@link putMLData}.+ * The (unzipped) remote mldata can contain arbitrary keys at the top level+ * apart from the ones that the current client knows about. We need to preserve+ * these verbatim when we use {@link putMLData}.** Thus we return two separate results from {@link fetchMLData}:*- * - {@link RawRemoteDerivedData}: The original, unmodified JSON.+ * - {@link RawRemoteMLData}: The original, unmodified JSON.*- * - {@link ParsedRemoteDerivedData}: The particular fields that the current- * client knows about, parsed according to their expected structure.+ * - {@link ParsedRemoteMLData}: The particular fields that the current client+ * knows about, parsed according to their expected structure.** When integrating this information into our local state, we use the parsed* version. And if we need to update the state on remote (e.g. if the current- * client notices an embedding type that was missing), then we use the original- * JSON as the base.+ * client notices an particular type of index was missing), then we use the+ * original JSON as the base.*/export interface RemoteMLData {raw: RawRemoteMLData;parsed: ParsedRemoteMLData | undefined;}+export type RawRemoteMLData = Record<string, unknown>;++export type ParsedRemoteMLData = Partial<{+ face: RemoteFaceIndex;+ clip: RemoteCLIPIndex;+}>;+/*** Zod schema for the {@link RemoteFaceIndex} type.*@@ -107,68 +130,69 @@ const RemoteCLIPIndex = z.object({});/**- * Zod schema for the {@link RawRemoteDerivedData} type.+ * Zod schema for the {@link RawRemoteMLData} type.*/-const RawRemoteDerivedData = z.object({}).passthrough();+const RawRemoteMLData = z.object({}).passthrough();/**- * Zod schema for the {@link ParsedRemoteDerivedData} type.+ * Zod schema for the {@link ParsedRemoteMLData} type.*/-const ParsedRemoteDerivedData = z.object({+const ParsedRemoteMLData = z.object({face: RemoteFaceIndex.nullish().transform(nullToUndefined),clip: RemoteCLIPIndex.nullish().transform(nullToUndefined),});/**- * Fetch derived data for the given files from remote.+ * Fetch ML data for the given files from remote.+ *+ * @param filesByID A map containing the files whose ML data we want to fetch.+ * Each entry is keyed the the file's ID, and the value is the file.*- * @param filesByID A map containing the files whose derived data we want to- * fetch. Each entry is keyed the the file's ID, and the value is the file.+ * @returns a map containing the (decrypted) ML data for each file for which+ * remote returned the corresponding ML data.*- * @returns a map containing the (decrypted) derived data for each file for- * which remote returned the corresponding embedding. Each entry in the map is- * keyed by file's ID, and each value is a {@link RemoteMLData} that- * contains both the original JSON, and parsed representation of embeddings that- * we know about.+ * Each entry in the map is keyed by file's ID, and each value is a+ * {@link RemoteMLData} that contains both the original JSON, and parsed+ * representation of ML data keys that we know about.*/export const fetchMLData = async (filesByID: Map<number, EnteFile>,): Promise<Map<number, RemoteMLData>> => {- const remoteEmbeddings = await fetchFileData("mldata", [+ const remoteFileDatas = await fetchFileData("mldata", [...filesByID.keys(),]);const result = new Map<number, RemoteMLData>();- for (const remoteEmbedding of remoteEmbeddings) {- const { fileID } = remoteEmbedding;+ for (const remoteFileData of remoteFileDatas) {+ const { fileID } = remoteFileData;const file = filesByID.get(fileID);if (!file) {- log.warn(`Ignoring derived data for unknown file id ${fileID}`);+ log.warn(`Ignoring ML data for unknown file id ${fileID}`);continue;}try {const decryptedBytes = await decryptFileEmbedding({- encryptedDataB64: remoteEmbedding.encryptedData,- decryptionHeaderB64: remoteEmbedding.decryptionHeader,+ encryptedDataB64: remoteFileData.encryptedData,+ decryptionHeaderB64: remoteFileData.decryptionHeader,keyB64: file.key,});const jsonString = await gunzip(decryptedBytes);result.set(fileID, remoteMLDataFromJSONString(jsonString));} catch (e) {// This shouldn't happen. Best guess is that some client has- // uploaded a corrupted embedding. Ignore it so that it gets+ // uploaded a corrupted ML index. Ignore it so that it gets// reindexed and uploaded correctly again.- log.warn(`Ignoring unparseable embedding for file id ${fileID}`, e);+ log.warn(`Ignoring unparseable ML data for file id ${fileID}`, e);}}- log.debug(() => `Fetched derived data for ${result.size} files`);+ log.debug(() => `Fetched ML data for ${result.size} files`);return result;};const remoteMLDataFromJSONString = (jsonString: string) => {- const raw = RawRemoteDerivedData.parse(JSON.parse(jsonString));- const parseResult = ParsedRemoteDerivedData.safeParse(raw);+ const raw = RawRemoteMLData.parse(JSON.parse(jsonString));+ const parseResult = ParsedRemoteMLData.safeParse(raw);// This code is included in apps/photos, which currently does not have the// TypeScript strict mode enabled, which causes a spurious tsc failure.//@@ -181,18 +205,16 @@ const remoteMLDataFromJSONString = (jsonString: string) => {};/**- * Update the derived data stored for given {@link enteFile} on remote.+ * Update the ML data stored for given {@link enteFile} on remote.*- * This allows other clients to directly pull the derived data instead of- * needing to re-index.+ * This allows other clients to directly pull the ML data instead of needing to+ * re-index.** The data on remote will be replaced unconditionally, and it is up to the- * client (us) to ensure that we preserve the parts of the pre-existing derived- * data (if any) that we did not understand or touch.+ * client (us) to ensure that we preserve the parts of the pre-existing ML data+ * (if any) that we did not understand or touch.** See: [Note: Preserve unknown derived data fields].*/-export const putMLData = async (- enteFile: EnteFile,- derivedData: RawRemoteMLData,-) => putFileData(enteFile, "mldata", await gzip(JSON.stringify(derivedData)));+export const putMLData = async (enteFile: EnteFile, mlData: RawRemoteMLData) =>+ putFileData(enteFile, "mldata", await gzip(JSON.stringify(mlData)));diff --git a/web/packages/new/photos/services/ml/worker.ts b/web/packages/new/photos/services/ml/worker.tsindex 28e327e713..9d0d6c6bba 100644--- a/web/packages/new/photos/services/ml/worker.ts+++ b/web/packages/new/photos/services/ml/worker.ts@@ -35,7 +35,7 @@ import {putMLData,type RawRemoteMLData,type RemoteMLData,-} from "./embedding";+} from "./ml-data";import { faceIndexingVersion, indexFaces, type FaceIndex } from "./face";import type { CLIPMatches, MLWorkerDelegate } from "./worker-types";