This commit is contained in:
Manav Rathi
2024-05-29 20:14:53 +05:30
parent 72851397b1
commit 4ce02fba93
3 changed files with 35 additions and 5 deletions

View File

@@ -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;
};

View File

@@ -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();
}
}

View File

@@ -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,