Skip to content

armgit / ente

Public Imported from GitHub
0

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

Use 1

Manav Rathi83373c442424
diff --git a/desktop/src/main/services/ml-worker.ts b/desktop/src/main/services/ml-worker.ts
index 5f465f6c8c..b2c1eb556f 100644
--- a/desktop/src/main/services/ml-worker.ts
+++ b/desktop/src/main/services/ml-worker.ts
@@ -18,6 +18,7 @@ import * as ort from "onnxruntime-node";
import { messagePortMainEndpoint } from "../utils/comlink";
import { wait } from "../utils/common";
import { writeStream } from "../utils/stream";
+import { fsStatMtime } from "./fs";
/**
* We cannot do
@@ -63,6 +64,7 @@ process.parentPort.once("message", (e) => {
// parent.
expose(
{
+ fsStatMtime,
computeCLIPImageEmbedding,
computeCLIPTextEmbeddingIfAvailable,
detectFaces,
@@ -72,6 +74,12 @@ process.parentPort.once("message", (e) => {
);
});
+// Duplicate of `fsStatMtime` from `fs.ts`.
+//
+// Since it is a trivial function, we duplicate its definition in the worker.
+const fsStatMtime = (path: string) =>
+ fs.stat(path).then((st) => st.mtime.getTime());
+
/**
* We cannot access Electron's {@link app} object within a utility process, so
* we pass the value of `app.getPath("userData")` during initialization, and it
diff --git a/web/packages/base/types/ipc.ts b/web/packages/base/types/ipc.ts
index 6325aaa5ca..22cbf7ffdb 100644
--- a/web/packages/base/types/ipc.ts
+++ b/web/packages/base/types/ipc.ts
@@ -595,6 +595,12 @@ export interface Electron {
* message port that the web layer obtains by doing {@link createMLWorker}.
*/
export interface ElectronMLWorker {
+ /**
+ * Return the last modified time (epoch milliseconds) for the file at the
+ * given {@link path} on the user's file system.
+ */
+ fsStatMtime: (path: string) => Promise<number>;
+
/**
* Return a CLIP embedding of the given image.
*
diff --git a/web/packages/gallery/services/upload.ts b/web/packages/gallery/services/upload.ts
index 83eeb0ca33..76a79c8be3 100644
--- a/web/packages/gallery/services/upload.ts
+++ b/web/packages/gallery/services/upload.ts
@@ -1,6 +1,6 @@
import log from "ente-base/log";
import { customAPIOrigin } from "ente-base/origins";
-import type { Electron, ZipItem } from "ente-base/types/ipc";
+import type { ZipItem } from "ente-base/types/ipc";
import { nullToUndefined } from "ente-utils/transform";
import { z } from "zod";
@@ -225,22 +225,23 @@ export const markUploadedAndObtainProcessableItem = async (
* kept here since it is in some sense the inverse of
* {@link markUploadedAndObtainProcessableItem}.
*
- * @param electron The {@link Electron} instance to use for IPC.
- *
* @param item A {@link TimestampedFileSystemUploadItem}
*
+ * @param fsStatMtime A function that can be used to perform IPC and obtain the
+ * last modified time from the node side.
+ *
* @returns If the last modified time of the file system file pointed to by the
- * given {@link item} is the same as what is recorded within
- * the structure, then return then wrapped {@link FileSystemUploadItem},
- * otherwise return `undefined`.
+ * given {@link item} is the same as what is recorded within the structure, then
+ * return then wrapped {@link FileSystemUploadItem}, otherwise return
+ * `undefined`.
*
* In case of any errors, also return `undefined`. This is because errors are
* expected if the underlying file system file was, e.g. renamed or removed
* between the time the file was uploaded and we got around to processing it.
*/
export const fileSystemUploadItemIfUnchanged = async (
- electron: Electron,
{ fsUploadItem, lastModifiedMs }: TimestampedFileSystemUploadItem,
+ fsStatMtime: (path: string) => Promise<number>,
): Promise<FileSystemUploadItem | undefined> => {
let path: string;
if (typeof fsUploadItem == "string") {
@@ -254,7 +255,7 @@ export const fileSystemUploadItemIfUnchanged = async (
}
try {
- const mtimeMs = await electron.fs.statMtime(path);
+ const mtimeMs = await fsStatMtime(path);
if (mtimeMs != lastModifiedMs) {
log.info(
`Not using upload item for path '${path}' since modified times have changed`,
diff --git a/web/packages/new/photos/services/ml/blob.ts b/web/packages/new/photos/services/ml/blob.ts
index bf3c825a70..9752418dd6 100644
--- a/web/packages/new/photos/services/ml/blob.ts
+++ b/web/packages/new/photos/services/ml/blob.ts
@@ -2,9 +2,10 @@ import { basename } from "ente-base/file-name";
import type { ElectronMLWorker } from "ente-base/types/ipc";
import { renderableImageBlob } from "ente-gallery/services/convert";
import { downloadManager } from "ente-gallery/services/download";
-import type {
- ProcessableUploadItem,
- UploadItem,
+import {
+ fileSystemUploadItemIfUnchanged,
+ type ProcessableUploadItem,
+ type UploadItem,
} from "ente-gallery/services/upload";
import { readStream } from "ente-gallery/utils/native-stream";
import type { EnteFile } from "ente-media/file";
@@ -74,18 +75,17 @@ export const createImageBitmapAndData = async (
* be set to the {@link FilesystemUploadItem} that was uploaded so that we can
* directly use the on-disk file instead of needing to download the original.
*
- * @param electron The {@link ElectronMLWorker} instance that stands as a
- * witness that we're actually running in our desktop app (and thus can safely
- * call our Node.js layer for various functionality).
+ * @param electron The {@link ElectronMLWorker} instance that we can use to IPC
+ * with the Node.js layer.
*/
export const fetchRenderableBlob = async (
file: EnteFile,
puItem: ProcessableUploadItem | undefined,
electron: ElectronMLWorker,
): Promise<Blob> =>
- puItem
+ (puItem
? await fetchRenderableUploadItemBlob(file, puItem, electron)
- : await fetchRenderableEnteFileBlob(file);
+ : undefined) ?? (await fetchRenderableEnteFileBlob(file));
const fetchRenderableUploadItemBlob = async (
file: EnteFile,
@@ -98,7 +98,16 @@ const fetchRenderableUploadItemBlob = async (
return new Blob([thumbnailData!]);
} else {
const uploadItem =
- puItem instanceof File ? puItem : puItem.fsUploadItem;
+ puItem instanceof File
+ ? puItem
+ : await fileSystemUploadItemIfUnchanged(
+ puItem,
+ electron.fsStatMtime,
+ );
+ if (!uploadItem) {
+ // The file on disk has changed. Fetch it from remote.
+ return undefined;
+ }
const blob = await readNonVideoUploadItem(uploadItem, electron);
return renderableImageBlob(blob, file.metadata.title);
}