Skip to content

armgit / ente

Public Imported from GitHub
0

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

Convert reverse

Manav Rathiad47dda61455
diff --git a/desktop/src/main/ipc.ts b/desktop/src/main/ipc.ts
index fc70be1a92..ae27824119 100644
--- a/desktop/src/main/ipc.ts
+++ b/desktop/src/main/ipc.ts
@@ -42,6 +42,7 @@ import {
fsRm,
fsRmdir,
fsWriteFile,
+ fsStatMtime,
fsWriteFileViaBackup,
} from "./services/fs";
import { convertToJPEG, generateImageThumbnail } from "./services/image";
@@ -164,6 +165,8 @@ export const attachIPCHandlers = () => {
ipcMain.handle("fsIsDir", (_, dirPath: string) => fsIsDir(dirPath));
+ ipcMain.handle("fsStatMtime", (_, path: string) => fsStatMtime(path));
+
ipcMain.handle("fsFindFiles", (_, folderPath: string) =>
fsFindFiles(folderPath),
);
diff --git a/desktop/src/main/services/fs.ts b/desktop/src/main/services/fs.ts
index 6cfe101ebf..cba66a46d9 100644
--- a/desktop/src/main/services/fs.ts
+++ b/desktop/src/main/services/fs.ts
@@ -36,6 +36,17 @@ export const fsIsDir = async (dirPath: string) => {
return stat.isDirectory();
};
+export const fsStatMtime = (path: string) =>
+ // [Note: Integral last modified time]
+ //
+ // Whenever we need to find the modified time of a file, use the
+ // `mtime.getTime()` instead of `mtimeMs` of the stat; this way, it is
+ // guaranteed that the times are integral (we persist these values to remote
+ // in some cases, and the contract is for them to be integral; mtimeMs is a
+ // float with sub-millisecond precision), and that all places use the same
+ // value so that they're comparable.
+ fs.stat(path).then((st) => st.mtime.getTime());
+
export const fsFindFiles = async (dirPath: string) => {
const items = await fs.readdir(dirPath, { withFileTypes: true });
let paths: string[] = [];
diff --git a/desktop/src/main/services/upload.ts b/desktop/src/main/services/upload.ts
index 7786deb45d..f62a181a48 100644
--- a/desktop/src/main/services/upload.ts
+++ b/desktop/src/main/services/upload.ts
@@ -142,6 +142,7 @@ export const markUploadedFile = (
const existing = uploadStatusStore.get("filePaths") ?? [];
const updated = existing.filter((p) => p != path && p != associatedPath);
uploadStatusStore.set("filePaths", updated);
+ // See: [Note: Integral last modified time]
return fs.stat(path).then((st) => st.mtime.getTime());
};
diff --git a/desktop/src/main/stream.ts b/desktop/src/main/stream.ts
index 03798e4bad..7007debe1d 100644
--- a/desktop/src/main/stream.ts
+++ b/desktop/src/main/stream.ts
@@ -125,6 +125,7 @@ const handleRead = async (path: string) => {
res.headers.set("Content-Length", `${fileSize}`);
// Add the file's last modified time (as epoch milliseconds).
+ // See: [Note: Integral last modified time]
const mtimeMs = stat.mtime.getTime();
res.headers.set("X-Last-Modified-Ms", `${mtimeMs}`);
}
diff --git a/desktop/src/preload.ts b/desktop/src/preload.ts
index 0780cb5e54..ef586210dd 100644
--- a/desktop/src/preload.ts
+++ b/desktop/src/preload.ts
@@ -184,6 +184,8 @@ const fsWriteFileViaBackup = (path: string, contents: string) =>
const fsIsDir = (dirPath: string) => ipcRenderer.invoke("fsIsDir", dirPath);
+const fsStatMtime = (path: string) => ipcRenderer.invoke("fsStatMtime", path);
+
// - Conversion
const convertToJPEG = (imageData: Uint8Array) =>
@@ -379,6 +381,7 @@ contextBridge.exposeInMainWorld("electron", {
writeFile: fsWriteFile,
writeFileViaBackup: fsWriteFileViaBackup,
isDir: fsIsDir,
+ statMtime: fsStatMtime,
findFiles: fsFindFiles,
},
diff --git a/web/packages/base/types/ipc.ts b/web/packages/base/types/ipc.ts
index f13888ceb8..6325aaa5ca 100644
--- a/web/packages/base/types/ipc.ts
+++ b/web/packages/base/types/ipc.ts
@@ -277,6 +277,12 @@ export interface Electron {
*/
isDir: (dirPath: string) => Promise<boolean>;
+ /**
+ * Return the last modified time (integral epoch milliseconds) of the
+ * file system entry at {@link path}.
+ */
+ statMtime: (path: string) => Promise<number>;
+
/**
* Return the paths of all the files under the given folder.
*
diff --git a/web/packages/gallery/services/upload.ts b/web/packages/gallery/services/upload.ts
index 01d56c6611..83eeb0ca33 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 { ZipItem } from "ente-base/types/ipc";
+import type { Electron, ZipItem } from "ente-base/types/ipc";
import { nullToUndefined } from "ente-utils/transform";
import { z } from "zod";
@@ -217,6 +217,57 @@ export const markUploadedAndObtainProcessableItem = async (
}
};
+/**
+ * Convert an {@link TimestampedFileSystemUploadItem} item back into a
+ * {@link FileSystemUploadItem} if the underlying file hasn't changed.
+ *
+ * While this functionality is not directly related to upload, this function is
+ * 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}
+ *
+ * @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`.
+ *
+ * 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,
+): Promise<FileSystemUploadItem | undefined> => {
+ let path: string;
+ if (typeof fsUploadItem == "string") {
+ path = fsUploadItem;
+ } else if (Array.isArray(fsUploadItem)) {
+ // The last modified time we recorded was of the zip file that contains
+ // the entry.
+ path = fsUploadItem[0];
+ } else {
+ path = fsUploadItem.path;
+ }
+
+ try {
+ const mtimeMs = await electron.fs.statMtime(path);
+ if (mtimeMs != lastModifiedMs) {
+ log.info(
+ `Not using upload item for path '${path}' since modified times have changed`,
+ );
+ return undefined;
+ }
+ return fsUploadItem;
+ } catch (e) {
+ log.warn(`Could not determine modified time for path '${path}'`, e);
+ return undefined;
+ }
+};
+
/**
* For each of cases of {@link UploadItem} that apply when we're running in the
* context of our desktop app, return a value that can be passed to