Skip to content

armgit / ente

Public Imported from GitHub
0

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

Tie it together

Manav Rathi9c04c9075e31
diff --git a/web/packages/new/photos/services/exif.ts b/web/packages/new/photos/services/exif.ts
index 79e398a6a1..2a8cd6a189 100644
--- a/web/packages/new/photos/services/exif.ts
+++ b/web/packages/new/photos/services/exif.ts
@@ -1,5 +1,5 @@
-import type { EnteFile } from "../types/file";
import ExifReader from "exifreader";
+import type { EnteFile } from "../types/file";
/**
* Index Exif in the given file.
@@ -12,22 +12,22 @@ import ExifReader from "exifreader";
* more readily use it), and also backfill any fields that old clients might've
* not extracted during their file uploads.
*
- * @param enteFile The {@link EnteFile} which we're indexing.
+ * @param enteFile The {@link EnteFile} which we're indexing. This is the file
+ * whose metadata we update if needed (for backfilling).
*
- * @param
+ * @param blob A {@link Blob} containing the {@link enteFile}'s data. This is
+ * where we extract the Exif from.
*
* [Note: Exif not EXIF]
*
* The standard uses "Exif" (not "EXIF") to refer to itself. We do the same.
- *
- * [Note: Indexing Exif]
- *
- * We don't really index Exif, we
*/
-export const indexExif = async (enteFile: EnteFile) => {
- const tags = await ExifReader.load(fileBuffer, { async: true })
+export const indexExif = async (enteFile: EnteFile, blob: Blob) => {
+ const tags = await ExifReader.load(await blob.arrayBuffer(), {
+ async: true,
+ });
return {
title: enteFile.title ?? "",
- tags
+ tags,
};
};
diff --git a/web/packages/new/photos/services/ml/blob.ts b/web/packages/new/photos/services/ml/blob.ts
index aaa59aa5b3..dac3ae1ed4 100644
--- a/web/packages/new/photos/services/ml/blob.ts
+++ b/web/packages/new/photos/services/ml/blob.ts
@@ -9,32 +9,6 @@ import DownloadManager from "../download";
import type { UploadItem } from "../upload/types";
import type { MLWorkerElectron } from "./worker-types";
-/**
- * A data structure containing data about an image in all formats that the
- * various indexing steps need. Consolidating all the data here and parsing them
- * in one go obviates the need for each indexing step to roll their own parsing.
- */
-export interface IndexableImage {
- /**
- * The original file's data, and a renderable representation of it (both as
- * {@link Blob}s).
- */
- blobs: IndexableBlobs;
- /**
- * An {@link ImageBitmap} from the original or converted image.
- *
- * This bitmap is constructed from the original file's data if the
- * browser knows how to handle it; otherwise we first convert it to a JPEG
- * and then create the bitmap from that.
- */
- bitmap: ImageBitmap;
- /**
- * The RGBA {@link ImageData} of the {@link bitmap}, obtained by rendering
- * it to an offscreen canvas.
- */
- data: ImageData;
-}
-
/**
* A pair of blobs - the original, and a possibly converted "renderable" one -
* for a file that we're trying to index.
@@ -65,13 +39,36 @@ export interface IndexableBlobs {
renderableBlob: Blob;
}
+/**
+ * Indexable blobs augmented with the image bitmap and RGBA data.
+ *
+ * This is data structure containing data about an image in all formats that the
+ * various indexing steps need. Consolidating all the data here and parsing them
+ * in one go obviates the need for each indexing step to roll their own parsing.
+ */
+export interface ImageBitmapAndData {
+ /**
+ * An {@link ImageBitmap} from the original or converted image.
+ *
+ * This bitmap is constructed from the original file's data if the
+ * browser knows how to handle it; otherwise we first convert it to a JPEG
+ * and then create the bitmap from that.
+ */
+ bitmap: ImageBitmap;
+ /**
+ * The RGBA {@link ImageData} of the {@link bitmap}, obtained by rendering
+ * it to an offscreen canvas.
+ */
+ data: ImageData;
+}
+
/**
* Create an {@link ImageBitmap} from the given {@link imageBlob}, and return
* both the image bitmap and its {@link ImageData}.
*/
export const imageBitmapAndData = async (
imageBlob: Blob,
-): Promise<IndexableImage> => {
+): Promise<ImageBitmapAndData> => {
const imageBitmap = await createImageBitmap(imageBlob);
const { width, height } = imageBitmap;
@@ -86,7 +83,8 @@ export const imageBitmapAndData = async (
};
/**
- * Return a {@link Blob} that can be used to create an {@link ImageBitmap}.
+ * Return a pair of blobs for the given data - the original, and a renderable
+ * one (possibly involving a JPEG conversion).
*
* The blob from the relevant image component is either constructed using the
* given {@link uploadItem} if present, otherwise it is downloaded from remote.
@@ -95,6 +93,10 @@ export const imageBitmapAndData = async (
* - For live photos the original image component is used.
* - For videos the thumbnail is used.
*
+ * Then, if the image blob we have seems to be something that the browser cannot
+ * handle, we convert it into a JPEG blob so that it can subsequently be used to
+ * create an {@link ImageBitmap}.
+ *
* @param enteFile The {@link EnteFile} to index.
*
* @param uploadItem If we're called during the upload process, then this will
@@ -104,30 +106,34 @@ export const imageBitmapAndData = async (
* @param electron The {@link MLWorkerElectron} instance that allows us to call
* our Node.js layer for various functionality.
*/
-export const renderableBlob = async (
+export const indexableBlobs = async (
enteFile: EnteFile,
uploadItem: UploadItem | undefined,
electron: MLWorkerElectron,
-): Promise<Blob> =>
+): Promise<IndexableBlobs> =>
uploadItem
- ? await renderableUploadItemBlob(enteFile, uploadItem, electron)
- : await indexableBlobs(enteFile);
+ ? await indexableUploadItemBlobs(enteFile, uploadItem, electron)
+ : await indexableEnteFileBlobs(enteFile);
-const renderableUploadItemBlob = async (
+const indexableUploadItemBlobs = async (
enteFile: EnteFile,
uploadItem: UploadItem,
electron: MLWorkerElectron,
) => {
const fileType = enteFile.metadata.fileType;
- let blob: Blob | undefined;
+ let originalBlob: Blob | undefined;
+ let renderableBlob: Blob;
if (fileType == FILE_TYPE.VIDEO) {
const thumbnailData = await DownloadManager.getThumbnail(enteFile);
- blob = new Blob([ensure(thumbnailData)]);
+ renderableBlob = new Blob([ensure(thumbnailData)]);
} else {
- const file = await readNonVideoUploadItem(uploadItem, electron);
- blob = await renderableImageBlob(enteFile.metadata.title, file);
+ originalBlob = await readNonVideoUploadItem(uploadItem, electron);
+ renderableBlob = await renderableImageBlob(
+ enteFile.metadata.title,
+ originalBlob,
+ );
}
- return ensure(blob);
+ return { originalBlob, renderableBlob };
};
/**
@@ -168,8 +174,11 @@ const readNonVideoUploadItem = async (
/**
* Return a pair of blobs for the given file - the original, and a renderable
* one (possibly involving a JPEG conversion).
+ *
+ * - The original will be downloaded if needed
+ * - The original will be converted to JPEG if needed
*/
-export const indexableBlobs = async (
+export const indexableEnteFileBlobs = async (
enteFile: EnteFile,
): Promise<IndexableBlobs> => {
const fileType = enteFile.metadata.fileType;
diff --git a/web/packages/new/photos/services/ml/crop.ts b/web/packages/new/photos/services/ml/crop.ts
index de308f370e..d42d7d48fc 100644
--- a/web/packages/new/photos/services/ml/crop.ts
+++ b/web/packages/new/photos/services/ml/crop.ts
@@ -1,7 +1,7 @@
import { blobCache } from "@/base/blob-cache";
import { ensure } from "@/utils/ensure";
import type { EnteFile } from "../../types/file";
-import { indexableBlobs } from "./blob";
+import { indexableEnteFileBlobs } from "./blob";
import { type Box, type FaceIndex } from "./face";
import { clamp } from "./math";
@@ -26,7 +26,7 @@ export const regenerateFaceCrops = async (
enteFile: EnteFile,
faceIndex: FaceIndex,
) => {
- const { renderableBlob } = await indexableBlobs(enteFile);
+ const { renderableBlob } = await indexableEnteFileBlobs(enteFile);
const imageBitmap = await createImageBitmap(renderableBlob);
try {
diff --git a/web/packages/new/photos/services/ml/worker.ts b/web/packages/new/photos/services/ml/worker.ts
index 1d07f39f83..48192ef9cc 100644
--- a/web/packages/new/photos/services/ml/worker.ts
+++ b/web/packages/new/photos/services/ml/worker.ts
@@ -14,8 +14,8 @@ import { getAllLocalFiles, getLocalTrashedFiles } from "../files";
import type { UploadItem } from "../upload/types";
import {
imageBitmapAndData,
- renderableBlob,
- type IndexableImage,
+ indexableBlobs,
+ type ImageBitmapAndData,
} from "./blob";
import { clipIndexingVersion, indexCLIP, type CLIPIndex } from "./clip";
import { saveFaceCrops } from "./crop";
@@ -408,11 +408,15 @@ const index = async (
// There is at least one derived data type that still needs to be indexed.
- const imageBlob = await renderableBlob(enteFile, uploadItem, electron);
+ const { originalBlob, renderableBlob } = await indexableBlobs(
+ enteFile,
+ uploadItem,
+ electron,
+ );
- let image: IndexableImage;
+ let image: ImageBitmapAndData;
try {
- image = await imageBitmapAndData(imageBlob);
+ image = await imageBitmapAndData(renderableBlob);
} catch (e) {
// If we cannot get the raw image data for the file, then retrying again
// won't help. It'd only make sense to retry later if modify
@@ -435,7 +439,7 @@ const index = async (
[faceIndex, clipIndex, exif] = await Promise.all([
existingFaceIndex ?? indexFaces(enteFile, image, electron),
existingCLIPIndex ?? indexCLIP(image, electron),
- indexExif(enteFile),
+ originalBlob ? indexExif(enteFile, originalBlob) : undefined,
]);
} catch (e) {
// See: [Note: Transient and permanent indexing failures]