This commit is contained in:
Manav Rathi
2024-05-31 10:56:42 +05:30
parent 5049b5cc4e
commit 27a5aa99c0
2 changed files with 37 additions and 13 deletions

View File

@@ -221,9 +221,12 @@ export const setIsFaceIndexingEnabled = async (enabled: boolean) => {
else localStorage.removeItem("faceIndexingEnabled");
};
export type IndexableEnteFile = { enteFile: EnteFile; isHidden: boolean };
/**
* Sync face DB with the local indexable files that we know about. Then return
* the next {@link count} files that still need to be indexed.
* the next {@link count} files that still need to be indexed alongwith a flag
* for whether they are currently hidden.
*
* For more specifics of what a "sync" entails, see
* {@link syncWithLocalFiles}.
@@ -232,7 +235,10 @@ export const setIsFaceIndexingEnabled = async (enabled: boolean) => {
*
* @param count Limit the resulting list of files to {@link count}.
*/
export const syncAndGetFilesToIndex = async (userID: number, count: number) => {
export const syncAndGetFilesToIndex = async (
userID: number,
count: number,
): Promise<IndexableEnteFile[]> => {
const indexableTypes = [FILE_TYPE.IMAGE, FILE_TYPE.LIVE_PHOTO];
const isIndexable = (f: EnteFile) =>
f.ownerID == userID && indexableTypes.includes(f.metadata.fileType);
@@ -253,10 +259,12 @@ export const syncAndGetFilesToIndex = async (userID: number, count: number) => {
);
const fileIDsToIndex = await indexableFileIDs(count);
return fileIDsToIndex.map((id) =>
ensure(
indexableNormalFilesByID.get(id) ??
indexableHiddenFilesByID.get(id),
),
);
return fileIDsToIndex.map((id) => {
const f = indexableNormalFilesByID.get(id);
if (f) return { enteFile: f, isHidden: false };
return {
enteFile: ensure(indexableHiddenFilesByID.get(id)),
isHidden: true,
};
});
};

View File

@@ -1,7 +1,10 @@
import log from "@/next/log";
import { CustomError, parseUploadErrorCodes } from "@ente/shared/error";
import PQueue from "p-queue";
import { syncAndGetFilesToIndex } from "services/face/indexer";
import {
syncAndGetFilesToIndex,
type IndexableEnteFile,
} from "services/face/indexer";
import { FaceIndexerWorker } from "services/face/indexer.worker";
import { EnteFile } from "types/file";
@@ -13,7 +16,7 @@ class MLSyncContext {
public userAgent: string;
public localFilesMap: Map<number, EnteFile>;
public outOfSyncFiles: EnteFile[];
public outOfSyncFiles: IndexableEnteFile[];
public nSyncedFiles: number;
public error?: Error;
@@ -138,12 +141,18 @@ class MachineLearningService {
}
public async syncLocalFile(
// eslint-disable-next-line @typescript-eslint/no-unused-vars
token: string,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
userID: number,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
userAgent: string,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
enteFile: EnteFile,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
localFile?: globalThis.File,
) {
/* TODO-ML(MR): Currently not used
const syncContext = await this.getLocalSyncContext(
token,
userID,
@@ -164,15 +173,21 @@ class MachineLearningService {
} catch (e) {
console.error("Error while syncing local file: ", enteFile.id, e);
}
*/
}
private async syncFileWithErrorHandler(
syncContext: MLSyncContext,
enteFile: EnteFile,
{ enteFile, isHidden }: IndexableEnteFile,
localFile?: globalThis.File,
) {
try {
await this.syncFile(enteFile, localFile, syncContext.userAgent);
await this.syncFile(
enteFile,
localFile,
isHidden,
syncContext.userAgent,
);
syncContext.nSyncedFiles += 1;
} catch (e) {
let error = e;
@@ -197,11 +212,12 @@ class MachineLearningService {
private async syncFile(
enteFile: EnteFile,
file: File | undefined,
isHidden: boolean,
userAgent: string,
) {
const worker = new FaceIndexerWorker();
await worker.index(enteFile, file, userAgent);
await worker.index(enteFile, file, isHidden, userAgent);
}
}