Skip to content

armgit / ente

Public Imported from GitHub
0

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

add putEmbedding API and return getLatestEmbeddings and runEmbeddingsSync

Abhinavfbbd4c9c3a74
diff --git a/apps/photos/src/pages/gallery/index.tsx b/apps/photos/src/pages/gallery/index.tsx
index 81d171c312..2e4fd13d41 100644
--- a/apps/photos/src/pages/gallery/index.tsx
+++ b/apps/photos/src/pages/gallery/index.tsx
@@ -121,7 +121,7 @@ import { syncEntities } from 'services/entityService';
import { constructUserIDToEmailMap } from 'services/collectionService';
import { getLocalFamilyData } from 'utils/user/family';
import InMemoryStore, { MS_KEYS } from 'services/InMemoryStore';
-import { runEmbeddingsSync } from 'services/embeddingService';
+import { syncEmbeddings } from 'services/embeddingService';
export const DeadCenter = styled('div')`
flex: 1;
@@ -693,7 +693,7 @@ export default function Gallery() {
await syncTrash(collections, setTrashedFiles);
await syncEntities();
await syncMapEnabled();
- await runEmbeddingsSync();
+ await syncEmbeddings();
} catch (e) {
switch (e.message) {
case ServerErrorCodes.SESSION_EXPIRED:
diff --git a/apps/photos/src/services/embeddingService.ts b/apps/photos/src/services/embeddingService.ts
index b1ecb2018a..43fe5f759c 100644
--- a/apps/photos/src/services/embeddingService.ts
+++ b/apps/photos/src/services/embeddingService.ts
@@ -1,4 +1,8 @@
-import { Embedding, GetEmbeddingDiffResponse } from 'types/embedding';
+import {
+ Embedding,
+ GetEmbeddingDiffResponse,
+ PutEmbeddingRequest,
+} from 'types/embedding';
import ComlinkCryptoWorker from 'utils/comlink/ComlinkCryptoWorker';
import { getEndpoint } from 'utils/common/apiUtil';
import { addLogLine } from 'utils/logging';
@@ -15,7 +19,7 @@ const DIFF_LIMIT = 500;
const EMBEDDINGS_TABLE = 'embeddings';
const EMBEDDING_SYNC_TIME_TABLE = 'embedding_sync_time';
-const getLocalEmbeddings = async () => {
+export const getLocalEmbeddings = async () => {
const entities: Array<Embedding> =
(await localForage.getItem<Embedding[]>(EMBEDDINGS_TABLE)) || [];
return entities;
@@ -25,31 +29,8 @@ const getEmbeddingSyncTime = async () => {
return (await localForage.getItem<number>(EMBEDDING_SYNC_TIME_TABLE)) ?? 0;
};
-export const getLatestEmbeddings = async () => {
+export const syncEmbeddings = async () => {
try {
- await runEmbeddingsSync();
- return await getLocalEmbeddings();
- } catch (e) {
- logError(e, 'failed to get latest embeddings');
- throw e;
- }
-};
-
-let syncInProgress: Promise<void> = null;
-
-export const runEmbeddingsSync = async () => {
- if (syncInProgress) {
- return syncInProgress;
- }
- syncInProgress = syncEmbeddings();
- return syncInProgress;
-};
-
-const syncEmbeddings = async () => {
- try {
- if (syncInProgress) {
- return syncInProgress;
- }
let embeddings = await getLocalEmbeddings();
const localFiles = await getLocalFiles();
const fileIdToKeyMap = new Map<number, string>();
@@ -94,8 +75,6 @@ const syncEmbeddings = async () => {
} while (response.diff.length === DIFF_LIMIT);
} catch (e) {
logError(e, 'Sync embeddings failed');
- } finally {
- syncInProgress = null;
}
};
@@ -122,3 +101,18 @@ export const getEmbeddingsDiff = async (
throw e;
}
};
+
+export const putEmbedding = async (putEmbeddingReq: PutEmbeddingRequest) => {
+ try {
+ const token = getToken();
+ if (!token) {
+ return;
+ }
+ await HTTPService.put(`${ENDPOINT}/embeddings`, putEmbeddingReq, null, {
+ 'X-Auth-Token': token,
+ });
+ } catch (e) {
+ logError(e, 'put embedding failed');
+ throw e;
+ }
+};
diff --git a/apps/photos/src/types/embedding.tsx b/apps/photos/src/types/embedding.tsx
index a81f156f8e..c1c2ea5d6d 100644
--- a/apps/photos/src/types/embedding.tsx
+++ b/apps/photos/src/types/embedding.tsx
@@ -21,3 +21,10 @@ export interface Embedding
export interface GetEmbeddingDiffResponse {
diff: EncryptedEmbedding[];
}
+
+export interface PutEmbeddingRequest {
+ fileID: number;
+ model: Model;
+ encryptedEmbedding: string;
+ decryptionHeader: string;
+}