Skip to content

armgit / ente

Public Imported from GitHub
0

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

Keep both concepts

Manav Rathi10a7c1172b3a
diff --git a/web/apps/photos/src/services/upload/uploadManager.ts b/web/apps/photos/src/services/upload/uploadManager.ts
index 9c5234fea7..5a1d3e954d 100644
--- a/web/apps/photos/src/services/upload/uploadManager.ts
+++ b/web/apps/photos/src/services/upload/uploadManager.ts
@@ -11,6 +11,7 @@ import type { UploadItem } from "ente-gallery/services/upload";
import {
RANDOM_PERCENTAGE_PROGRESS_FOR_PUT,
shouldDisableCFUploadProxy,
+ toTimestampedDesktopUploadItem,
type UploadPhase,
type UploadResult,
} from "ente-gallery/services/upload";
@@ -647,8 +648,16 @@ class UploadManager {
(uploadResult == "uploaded" ||
uploadResult == "uploadedWithStaticThumbnail")
) {
- indexNewUpload(decryptedFile, uploadItem);
- processVideoNewUpload(decryptedFile, uploadItem);
+ if (electron) {
+ // Run the various desktop app specific indexing /
+ // processing steps on the uploaded file.
+ const tsUploadItem = toTimestampedDesktopUploadItem(
+ electron,
+ uploadItem,
+ );
+ indexNewUpload(decryptedFile, tsUploadItem);
+ processVideoNewUpload(decryptedFile, tsUploadItem);
+ }
}
this.updateExistingFiles(decryptedFile);
}
diff --git a/web/packages/gallery/services/upload.ts b/web/packages/gallery/services/upload.ts
index c292f35d13..3d6623e156 100644
--- a/web/packages/gallery/services/upload.ts
+++ b/web/packages/gallery/services/upload.ts
@@ -74,37 +74,59 @@ export interface FileAndPath {
}
/**
- * The of cases of {@link UploadItem} that apply when we're running in the
- * context of our desktop app.
+ * The cases of {@link UploadItem} that apply when we're running in the context
+ * of our desktop app.
*
- * If we know that we're running in the context of the desktop app then
- * {@link toDesktopUploadItem} can be used to convert from an
- * {@link UploadItem}.
+ * See also {@link TimestampedDesktopUploadItem}.
*/
export type DesktopUploadItem = Exclude<UploadItem, File>;
/**
- * Assert that the given {@link UploadItem} is, in fact, an {@link DesktopUploadItem}.
+ * A {@link DesktopUploadItem} augmented with the last modified time of the
+ * corresponding file on disk.
+ *
+ * By keeping the last modified time, we can use it as a test of whether or not
+ * the file on disk was modified since it was uploaded. Such an ability is
+ * useful for tasks where there can be a arbitrary delay between when the file
+ * was uploaded and when the file gets processed.
+ *
+ * See {@link toTimestampedDesktopUploadItem} for converting a
+ * {@link UploadItem} into a {@link TimestampedDesktopUploadItem} when running
+ * in the context of the desktop app.
+ */
+export interface TimestampedDesktopUploadItem {
+ uploadItem: DesktopUploadItem;
+ lastModifiedTime: number;
+}
+
+/**
+ * Assert that the given {@link UploadItem} is, in fact, one of the cases that
+ * can happen when we're running in the context of the desktop app, and attach
+ * the last modified time of the corresponding file system file to it.
*
* @param electron A witness to the fact that we're running in the context of
- * the desktop app. The electron instance is not actually used by this function.
+ * the desktop app. We also use it to find the last modified time of the file.
*
- * @param uploadItem The upload item we obtained from an arbitrary place in the app.
+ * @param uploadItem The upload item we obtained from an arbitrary place in the
+ * app.
*
* @returns The same {@link uploadItem}, but after excluding the cases that can
- * only happen when running in the web app.
+ * only happen when running in the web app, and with the last modified time of
+ * the file attached to it.
*/
-export const toDesktopUploadItem = (
+export const toTimestampedDesktopUploadItem = (
electron: Electron,
uploadItem: UploadItem,
-): DesktopUploadItem => {
+): TimestampedDesktopUploadItem => {
if (uploadItem instanceof File) {
log.info(`Invalid upload item (electron: ${!!electron})`, uploadItem);
throw new Error(
"Found a File upload item even though we're running in the desktop app",
);
}
- return uploadItem;
+ // TODO(HLS):
+ const lastModifiedTime = 0;
+ return { uploadItem, lastModifiedTime };
};
/**
diff --git a/web/packages/gallery/services/video.ts b/web/packages/gallery/services/video.ts
index 880067020d..b4361a43aa 100644
--- a/web/packages/gallery/services/video.ts
+++ b/web/packages/gallery/services/video.ts
@@ -1,8 +1,8 @@
import { decryptBlob } from "ente-base/crypto";
import type { EncryptedBlob } from "ente-base/crypto/types";
+import { ensureElectron } from "ente-base/electron";
import { type PublicAlbumsCredentials } from "ente-base/http";
import log from "ente-base/log";
-import type { Electron } from "ente-base/types/ipc";
import { fileLogID, type EnteFile } from "ente-media/file";
import { FileType } from "ente-media/file-type";
import { settingsSnapshot } from "ente-new/photos/services/settings";
@@ -21,11 +21,7 @@ import {
getFilePreviewDataUploadURL,
putVideoData,
} from "./file-data";
-import {
- toDesktopUploadItem,
- type DesktopUploadItem,
- type UploadItem,
-} from "./upload";
+import { type TimestampedDesktopUploadItem } from "./upload";
interface VideoProcessingQueueItem {
/**
@@ -34,13 +30,14 @@ interface VideoProcessingQueueItem {
*/
file: EnteFile;
/**
- * The {@link DesktopUploadItem} if available for the newly uploaded
- * {@link file}.
+ * The {@link TimestampedDesktopUploadItem} when available for the newly
+ * uploaded {@link file}.
*
- * If present, this serves as an optimization allowing us to directly read
- * the file off the user's filesystem.
+ * It will be present when this queue item was enqueued during a upload from
+ * the current client. If present, this serves as an optimization allowing
+ * us to directly read the file off the user's filesystem.
*/
- uploadItem: DesktopUploadItem | undefined;
+ timestampedUploadItem: TimestampedDesktopUploadItem | undefined;
}
/**
@@ -315,36 +312,29 @@ const blobToDataURL = (blob: Blob) =>
* redownload the video.
*
* It only does the processing if we're running in the context of the desktop
- * app as the video processing is resource intensive.
+ * app as the video processing is resource intensive. In particular, processing
+ * large videos with the Wasm ffmpeg implementation can cause the app to crash,
+ * on mobile devices (see https://github.com/ffmpegwasm/ffmpeg.wasm/issues/851).
+ * In contrast, the desktop app can us the efficient native FFmpeg integration.
*
- * Note that this is an optimization. Even if we don't process the video at this
- * time (e.g. if the video processor can't keep up with the uploads), we will
- * eventually process it later as part of a backfill.
+ * Note that this function is an optimization. Even if we don't process the
+ * video at this time (e.g. if the video processor can't keep up with the
+ * uploads), we will eventually process it later as part of a backfill.
*
* @param file The {@link EnteFile} that got uploaded (video or otherwise).
*
- * @param uploadItem The item that was uploaded. This can be used to get at the
- * contents of the file that got uploaded.
+ * @param timestampedUploadItem The item that was uploaded. This can be used to
+ * read the contents of the file that got uploaded directly from disk instead of
+ * needing to download it again.
*/
export const processVideoNewUpload = (
file: EnteFile,
- uploadItem: UploadItem,
+ timestampedUploadItem: TimestampedDesktopUploadItem,
) => {
// TODO(HLS):
if (!isVideoProcessingEnabled()) return;
if (file.metadata.fileType !== FileType.video) return;
- const electron = globalThis.electron;
- if (!electron) {
- // Processing very large videos with the current ffmpeg Wasm
- // implementation can cause the app to crash, esp. on mobile devices
- // (e.g. https://github.com/ffmpegwasm/ffmpeg.wasm/issues/851).
- //
- // So the video processing only happpens in the desktop app, which uses
- // the much more efficient native FFmpeg integration.
- return;
- }
-
if (_state.videoProcessingQueue.length > 50) {
// Drop new requests if the queue can't keep up to avoid the app running
// out of memory by keeping hold of too many (potentially huge) video
@@ -354,26 +344,20 @@ export const processVideoNewUpload = (
}
// Enqueue the item.
- _state.videoProcessingQueue.push({
- file,
- uploadItem: toDesktopUploadItem(electron, uploadItem),
- });
+ _state.videoProcessingQueue.push({ file, timestampedUploadItem });
// Tickle the processor if it isn't already running.
- _state.queueProcessor ??= processQueue(electron);
+ _state.queueProcessor ??= processQueue();
};
export const isVideoProcessingEnabled = () =>
process.env.NEXT_PUBLIC_ENTE_WIP_VIDEO_STREAMING &&
settingsSnapshot().isInternalUser;
-const processQueue = async (electron: Electron) => {
+const processQueue = async () => {
while (_state.videoProcessingQueue.length) {
try {
- await processQueueItem(
- _state.videoProcessingQueue.shift()!,
- electron,
- );
+ await processQueueItem(_state.videoProcessingQueue.shift()!);
} catch (e) {
log.error("Video processing failed", e);
// Ignore this unprocessable item. Currently this function only runs
@@ -406,10 +390,15 @@ const processQueue = async (electron: Electron) => {
*
* 3. Both the generated video and the HLS playlist are then uploaded, E2EE.
*/
-const processQueueItem = async (
- { file, uploadItem }: VideoProcessingQueueItem,
- electron: Electron,
-) => {
+const processQueueItem = async ({
+ file,
+ timestampedUploadItem,
+}: VideoProcessingQueueItem) => {
+ const electron = ensureElectron();
+
+ // TODO(HLS):
+ const uploadItem = timestampedUploadItem?.uploadItem;
+
log.debug(() => ["gen-hls", { file, uploadItem }]);
const sourceVideo = uploadItem ?? (await downloadManager.fileStream(file));
diff --git a/web/packages/new/photos/services/ml/blob.ts b/web/packages/new/photos/services/ml/blob.ts
index 206d0a749b..77a2cd1b45 100644
--- a/web/packages/new/photos/services/ml/blob.ts
+++ b/web/packages/new/photos/services/ml/blob.ts
@@ -2,7 +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 { DesktopUploadItem } from "ente-gallery/services/upload";
+import type {
+ DesktopUploadItem,
+ TimestampedDesktopUploadItem,
+} from "ente-gallery/services/upload";
import { readStream } from "ente-gallery/utils/native-stream";
import type { EnteFile } from "ente-media/file";
import { FileType } from "ente-media/file-type";
@@ -77,16 +80,16 @@ export const createImageBitmapAndData = async (
*/
export const fetchRenderableBlob = async (
file: EnteFile,
- uploadItem: DesktopUploadItem | undefined,
+ tsUploadItem: TimestampedDesktopUploadItem | undefined,
electron: ElectronMLWorker,
): Promise<Blob> =>
- uploadItem
- ? await fetchRenderableUploadItemBlob(file, uploadItem, electron)
+ tsUploadItem
+ ? await fetchRenderableUploadItemBlob(file, tsUploadItem, electron)
: await fetchRenderableEnteFileBlob(file);
const fetchRenderableUploadItemBlob = async (
file: EnteFile,
- uploadItem: DesktopUploadItem,
+ tsUploadItem: TimestampedDesktopUploadItem,
electron: ElectronMLWorker,
) => {
const fileType = file.metadata.fileType;
@@ -94,6 +97,9 @@ const fetchRenderableUploadItemBlob = async (
const thumbnailData = await downloadManager.thumbnailData(file);
return new Blob([thumbnailData!]);
} else {
+ // TODO(HLS):
+ const uploadItem = tsUploadItem.uploadItem;
+
const blob = await readNonVideoUploadItem(uploadItem, electron);
return renderableImageBlob(blob, file.metadata.title);
}
diff --git a/web/packages/new/photos/services/ml/index.ts b/web/packages/new/photos/services/ml/index.ts
index 19170b02da..c4e85da1c6 100644
--- a/web/packages/new/photos/services/ml/index.ts
+++ b/web/packages/new/photos/services/ml/index.ts
@@ -10,10 +10,7 @@ import log from "ente-base/log";
import { masterKeyFromSession } from "ente-base/session";
import type { Electron } from "ente-base/types/ipc";
import { ComlinkWorker } from "ente-base/worker/comlink-worker";
-import {
- toDesktopUploadItem,
- type UploadItem,
-} from "ente-gallery/services/upload";
+import { type TimestampedDesktopUploadItem } from "ente-gallery/services/upload";
import type { EnteFile } from "ente-media/file";
import { FileType } from "ente-media/file-type";
import { throttled } from "ente-utils/promise";
@@ -438,20 +435,18 @@ const workerDidUnawaitedIndex = () => void debounceUpdateClustersAndPeople();
*
* @param file The {@link EnteFile} that got uploaded.
*
- * @param uploadItem The item that was uploaded. This can be used to get at the
- * contents of the file that got uploaded. In case of live photos, this is the
- * image part of the live photo that was uploaded.
+ * @param timestampedUploadItem The item that was uploaded. This can be used to
+ * get at the contents of the file that got uploaded. In case of live photos,
+ * this is the image part of the live photo that was uploaded.
*/
-export const indexNewUpload = (file: EnteFile, uploadItem: UploadItem) => {
+export const indexNewUpload = (
+ file: EnteFile,
+ timestampedUploadItem: TimestampedDesktopUploadItem,
+) => {
if (!isMLEnabled()) return;
-
- const electron = globalThis.electron;
- if (!electron) return;
-
- const desktopUploadItem = toDesktopUploadItem(electron, uploadItem);
if (file.metadata.fileType !== FileType.image) return;
- log.debug(() => ["ml/liveq", { file, uploadItem: desktopUploadItem }]);
- void worker().then((w) => w.onUpload(file, desktopUploadItem));
+ log.debug(() => ["ml/liveq", { file, timestampedUploadItem }]);
+ void worker().then((w) => w.onUpload(file, timestampedUploadItem));
};
export type MLStatus =
diff --git a/web/packages/new/photos/services/ml/worker.ts b/web/packages/new/photos/services/ml/worker.ts
index 5caef83919..aec2ec62d2 100644
--- a/web/packages/new/photos/services/ml/worker.ts
+++ b/web/packages/new/photos/services/ml/worker.ts
@@ -6,7 +6,7 @@ import log from "ente-base/log";
import { logUnhandledErrorsAndRejectionsInWorker } from "ente-base/log-web";
import type { ElectronMLWorker } from "ente-base/types/ipc";
import { isNetworkDownloadError } from "ente-gallery/services/download";
-import type { DesktopUploadItem } from "ente-gallery/services/upload";
+import type { TimestampedDesktopUploadItem } from "ente-gallery/services/upload";
import { fileLogID, type EnteFile } from "ente-media/file";
import { wait } from "ente-utils/promise";
import { getAllLocalFiles, getLocalTrashedFiles } from "../files";
@@ -64,16 +64,17 @@ const idleDurationStart = 5; /* 5 seconds */
const idleDurationMax = 16 * 60; /* 16 minutes */
interface IndexableItem {
- /** The {@link EnteFile} to (potentially) index. */
+ /**
+ * The {@link EnteFile} to (potentially) index.
+ */
file: EnteFile;
/**
* If the file was uploaded from the current client, then its contents.
- *
- * Since indexing only happens in the desktop app, this is the more specific
- * type {@link DesktopUploadItem}.
*/
- uploadItem: DesktopUploadItem | undefined;
- /** The existing ML data on remote corresponding to this file. */
+ timestampedUploadItem: TimestampedDesktopUploadItem | undefined;
+ /**
+ * The existing ML data (if any) on remote corresponding to this file.
+ */
remoteMLData: RemoteMLData | undefined;
}
@@ -178,11 +179,13 @@ export class MLWorker {
/**
* Called when a file is uploaded from the current client.
*
- * This is a great opportunity to index since we already have the in-memory
- * representation of the file's contents with us and won't need to download
- * the file from remote.
+ * This is a great opportunity to index since we already have the file with
+ * us and won't need to download the file from remote.
*/
- onUpload(file: EnteFile, uploadItem: DesktopUploadItem) {
+ onUpload(
+ file: EnteFile,
+ timestampedUploadItem: TimestampedDesktopUploadItem,
+ ) {
// Add the recently uploaded file to the live indexing queue.
//
// Limit the queue to some maximum so that we don't keep growing
@@ -196,7 +199,11 @@ export class MLWorker {
if (this.liveQ.length < 200) {
// The file is just being uploaded, and so will not have any
// pre-existing ML data on remote.
- this.liveQ.push({ file, uploadItem, remoteMLData: undefined });
+ this.liveQ.push({
+ file,
+ timestampedUploadItem,
+ remoteMLData: undefined,
+ });
this.wakeUp();
} else {
log.debug(() => "Ignoring upload item since liveQ is full");
@@ -312,7 +319,7 @@ export class MLWorker {
// Return files after annotating them with their existing ML data.
return Array.from(fileByID, ([id, file]) => ({
file,
- uploadItem: undefined,
+ timestampedUploadItem: undefined,
remoteMLData: mlDataByID.get(id),
}));
}
@@ -499,7 +506,7 @@ const syncWithLocalFilesAndGetFilesToIndex = async (
* then remote will return a 413 Request Entity Too Large).
*/
const index = async (
- { file, uploadItem, remoteMLData }: IndexableItem,
+ { file, timestampedUploadItem, remoteMLData }: IndexableItem,
electron: ElectronMLWorker,
) => {
const f = fileLogID(file);
@@ -549,7 +556,11 @@ const index = async (
let renderableBlob: Blob;
try {
- renderableBlob = await fetchRenderableBlob(file, uploadItem, electron);
+ renderableBlob = await fetchRenderableBlob(
+ file,
+ timestampedUploadItem,
+ electron,
+ );
} catch (e) {
// Network errors are transient and shouldn't be marked.
//