From 4ce02fba93bb73e1b091d342b575e25be227c59a Mon Sep 17 00:00:00 2001 From: Manav Rathi Date: Wed, 29 May 2024 20:14:53 +0530 Subject: [PATCH] ll --- web/apps/photos/src/services/face/f-index.ts | 3 +- .../src/services/face/indexer.worker.ts | 29 ++++++++++++++++--- web/apps/photos/src/utils/file/index.ts | 8 +++++ 3 files changed, 35 insertions(+), 5 deletions(-) diff --git a/web/apps/photos/src/services/face/f-index.ts b/web/apps/photos/src/services/face/f-index.ts index 5e93f60bd6..a1a614f138 100644 --- a/web/apps/photos/src/services/face/f-index.ts +++ b/web/apps/photos/src/services/face/f-index.ts @@ -12,6 +12,7 @@ import { translate, } from "transformation-matrix"; import type { EnteFile } from "types/file"; +import { logIdentifier } from "utils/file"; import { saveFaceCrop } from "./crop"; import { fetchImageBitmap, getLocalFileImageBitmap } from "./file"; import { @@ -58,7 +59,7 @@ export const indexFaces = async (enteFile: EnteFile, localFile?: File) => { log.debug(() => { const nf = mlFile.faces?.length ?? 0; const ms = Date.now() - startTime; - return `Indexed ${nf} faces in file ${enteFile.id} (${ms} ms)`; + return `Indexed ${nf} faces in file ${logIdentifier(enteFile)} (${ms} ms)`; }); return mlFile; }; diff --git a/web/apps/photos/src/services/face/indexer.worker.ts b/web/apps/photos/src/services/face/indexer.worker.ts index 61c62b4029..84250dd703 100644 --- a/web/apps/photos/src/services/face/indexer.worker.ts +++ b/web/apps/photos/src/services/face/indexer.worker.ts @@ -1,6 +1,7 @@ import log from "@/next/log"; import type { EnteFile } from "types/file"; -import { markIndexingFailed } from "./db"; +import { logIdentifier } from "utils/file"; +import { closeFaceDBConnectionsIfNeeded, markIndexingFailed } from "./db"; import { indexFaces } from "./f-index"; /** @@ -13,14 +14,34 @@ import { indexFaces } from "./f-index"; * comlink workers are structured. */ export class FaceIndexerWorker { + /* + * Index faces in a file, save the persist the results locally, and put them + * on remote. + * + * @param enteFile The {@link EnteFile} to index. + * + * @param file If the file is one which is being uploaded from the current + * client, then we will also have access to the file's content. In such + * cases, pass a web {@link File} object to use that its data directly for + * face indexing. If this is not provided, then the file's contents will be + * downloaded and decrypted from remote. + */ async index(enteFile: EnteFile, file: File | undefined) { - const fileID = enteFile.id; + const f = logIdentifier(enteFile); try { const faceIndex = await indexFaces(enteFile, file); - log.info(`faces in file ${fileID}`, faceIndex); + log.info(`faces in file ${f}`, faceIndex); } catch (e) { - log.error(`Failed to index faces in file ${fileID}`, e); + log.error(`Failed to index faces in file ${f}`, e); markIndexingFailed(enteFile.id); } } + + /** + * Calls {@link closeFaceDBConnectionsIfNeeded} to close any open + * connections to the face DB from the web worker's context. + */ + closeFaceDB() { + closeFaceDBConnectionsIfNeeded(); + } } diff --git a/web/apps/photos/src/utils/file/index.ts b/web/apps/photos/src/utils/file/index.ts index 3a349abea7..c15cca63c0 100644 --- a/web/apps/photos/src/utils/file/index.ts +++ b/web/apps/photos/src/utils/file/index.ts @@ -81,6 +81,14 @@ class ModuleState { const moduleState = new ModuleState(); +/** + * @returns a string to use as an identifier when logging information about the + * given {@link enteFile}. The returned string contains the file name (for ease + * of debugging) and the file ID (for exactness). + */ +export const logIdentifier = (enteFile: EnteFile) => + `${enteFile.metadata.title ?? "-"} (${enteFile.id})`; + export async function getUpdatedEXIFFileForDownload( fileReader: FileReader, file: EnteFile,