Skip to content

armgit / ente

Public Imported from GitHub
0

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

Move

Manav Rathi157f3696e43e
diff --git a/web/apps/photos/src/components/PhotoViewer/index.tsx b/web/apps/photos/src/components/PhotoViewer/index.tsx
index db3d11b5c2..d5ecb003b4 100644
--- a/web/apps/photos/src/components/PhotoViewer/index.tsx
+++ b/web/apps/photos/src/components/PhotoViewer/index.tsx
@@ -17,6 +17,7 @@ import {
import { FILE_TYPE } from "@/media/file-type";
import { isNonWebImageFileExtension } from "@/media/formats";
import type { LoadedLivePhotoSourceURL } from "@/new/photos/types/file";
+import { detectFileTypeInfo } from "@/new/photos/utils/detect-type";
import { lowercaseExtension } from "@/next/file";
import { FlexWrapper } from "@ente/shared/components/Container";
import EnteSpinner from "@ente/shared/components/EnteSpinner";
@@ -44,7 +45,6 @@ import { t } from "i18next";
import isElectron from "is-electron";
import { AppContext } from "pages/_app";
import { GalleryContext } from "pages/gallery";
-import { detectFileTypeInfo } from "services/detect-type";
import downloadManager from "services/download";
import { getParsedExifData } from "services/exif";
import { trashFiles } from "services/fileService";
diff --git a/web/apps/photos/src/services/download/index.ts b/web/apps/photos/src/services/download/index.ts
index 179aa7cbc8..a0f99cac2e 100644
--- a/web/apps/photos/src/services/download/index.ts
+++ b/web/apps/photos/src/services/download/index.ts
@@ -5,6 +5,7 @@ import {
type LivePhotoSourceURL,
type SourceURLs,
} from "@/new/photos/types/file";
+import { getRenderableImage } from "@/new/photos/utils/file";
import { blobCache, type BlobCache } from "@/next/blob-cache";
import log from "@/next/log";
import ComlinkCryptoWorker from "@ente/shared/crypto";
@@ -14,7 +15,6 @@ import { isPlaybackPossible } from "@ente/shared/media/video-playback";
import type { Remote } from "comlink";
import isElectron from "is-electron";
import * as ffmpeg from "services/ffmpeg";
-import { getRenderableImage } from "utils/file";
import { PhotosDownloadClient } from "./clients/photos";
import { PublicAlbumsDownloadClient } from "./clients/publicAlbums";
diff --git a/web/apps/photos/src/services/fix-exif.ts b/web/apps/photos/src/services/fix-exif.ts
index a695f8d3a0..d212b31f11 100644
--- a/web/apps/photos/src/services/fix-exif.ts
+++ b/web/apps/photos/src/services/fix-exif.ts
@@ -1,9 +1,9 @@
import { FILE_TYPE } from "@/media/file-type";
import { EnteFile } from "@/new/photos/types/file";
+import { detectFileTypeInfo } from "@/new/photos/utils/detect-type";
import log from "@/next/log";
import { validateAndGetCreationUnixTimeInMicroSeconds } from "@ente/shared/time";
import type { FixOption } from "components/FixCreationTime";
-import { detectFileTypeInfo } from "services/detect-type";
import {
changeFileCreationTime,
updateExistingFilePubMetadata,
diff --git a/web/apps/photos/src/services/upload/uploadService.ts b/web/apps/photos/src/services/upload/uploadService.ts
index c50407eb94..89a4d96f50 100644
--- a/web/apps/photos/src/services/upload/uploadService.ts
+++ b/web/apps/photos/src/services/upload/uploadService.ts
@@ -2,6 +2,7 @@ import { hasFileHash } from "@/media/file";
import { FILE_TYPE, type FileTypeInfo } from "@/media/file-type";
import { encodeLivePhoto } from "@/media/live-photo";
import type { Metadata } from "@/media/types/file";
+import { detectFileTypeInfoFromChunk } from "@/new/photos/services/detect-type";
import {
EnteFile,
MetadataFileAttributes,
@@ -40,7 +41,6 @@ import {
} from "utils/magicMetadata";
import { readStream } from "utils/native-stream";
import * as convert from "xml-js";
-import { detectFileTypeInfoFromChunk } from "../detect-type";
import { tryParseEpochMicrosecondsFromFileName } from "./date";
import publicUploadHttpClient from "./publicUploadHttpClient";
import type { ParsedMetadataJSON } from "./takeout";
diff --git a/web/apps/photos/src/utils/file/index.ts b/web/apps/photos/src/utils/file/index.ts
index f9bb04b94c..759117390c 100644
--- a/web/apps/photos/src/utils/file/index.ts
+++ b/web/apps/photos/src/utils/file/index.ts
@@ -1,6 +1,4 @@
import { FILE_TYPE } from "@/media/file-type";
-import { isNonWebImageFileExtension } from "@/media/formats";
-import { heicToJPEG } from "@/media/heic-convert";
import { decodeLivePhoto } from "@/media/live-photo";
import {
EncryptedEnteFile,
@@ -12,20 +10,18 @@ import {
FileWithUpdatedMagicMetadata,
} from "@/new/photos/types/file";
import { VISIBILITY_STATE } from "@/new/photos/types/magicMetadata";
+import { detectFileTypeInfo } from "@/new/photos/utils/detect-type";
import { mergeMetadata } from "@/new/photos/utils/file";
import { lowercaseExtension } from "@/next/file";
import log from "@/next/log";
-import { CustomErrorMessage, type Electron } from "@/next/types/ipc";
-import { workerBridge } from "@/next/worker/worker-bridge";
+import { type Electron } from "@/next/types/ipc";
import { withTimeout } from "@/utils/promise";
import ComlinkCryptoWorker from "@ente/shared/crypto";
import { LS_KEYS, getData } from "@ente/shared/storage/localStorage";
import type { User } from "@ente/shared/user/types";
import { downloadUsingAnchor } from "@ente/shared/utils";
import { t } from "i18next";
-import isElectron from "is-electron";
import { moveToHiddenCollection } from "services/collectionService";
-import { detectFileTypeInfo } from "services/detect-type";
import DownloadManager from "services/download";
import { updateFileCreationDateInEXIF } from "services/exif";
import {
@@ -43,19 +39,6 @@ import { isArchivedFile, updateMagicMetadata } from "utils/magicMetadata";
import { safeFileName } from "utils/native-fs";
import { writeStream } from "utils/native-stream";
-const SUPPORTED_RAW_FORMATS = [
- "heic",
- "rw2",
- "tiff",
- "arw",
- "cr3",
- "cr2",
- "nef",
- "psd",
- "dng",
- "tif",
-];
-
export enum FILE_OPS_TYPE {
DOWNLOAD,
FIX_TIME,
@@ -66,22 +49,6 @@ export enum FILE_OPS_TYPE {
DELETE_PERMANENTLY,
}
-class ModuleState {
- /**
- * This will be set to true if we get an error from the Node.js side of our
- * desktop app telling us that native JPEG conversion is not available for
- * the current OS/arch combination.
- *
- * That way, we can stop pestering it again and again (saving an IPC
- * round-trip).
- *
- * Note the double negative when it is used.
- */
- isNativeJPEGConversionNotAvailable = false;
-}
-
-const moduleState = new ModuleState();
-
/**
* @returns a string to use as an identifier when logging information about the
* given {@link enteFile}. The returned string contains the file name (for ease
@@ -257,80 +224,6 @@ export async function decryptFile(
}
}
-/**
- * The returned blob.type is filled in, whenever possible, with the MIME type of
- * the data that we're dealing with.
- */
-export const getRenderableImage = async (fileName: string, imageBlob: Blob) => {
- try {
- const tempFile = new File([imageBlob], fileName);
- const fileTypeInfo = await detectFileTypeInfo(tempFile);
- log.debug(
- () =>
- `Need renderable image for ${JSON.stringify({ fileName, ...fileTypeInfo })}`,
- );
- const { extension } = fileTypeInfo;
-
- if (!isNonWebImageFileExtension(extension)) {
- // Either it is something that the browser already knows how to
- // render, or something we don't even about yet.
- const mimeType = fileTypeInfo.mimeType;
- if (!mimeType) {
- log.info(
- "Trying to render a file without a MIME type",
- fileName,
- );
- return imageBlob;
- } else {
- return new Blob([imageBlob], { type: mimeType });
- }
- }
-
- const available = !moduleState.isNativeJPEGConversionNotAvailable;
- if (isElectron() && available && isSupportedRawFormat(extension)) {
- // If we're running in our desktop app, see if our Node.js layer can
- // convert this into a JPEG using native tools for us.
- try {
- return await nativeConvertToJPEG(imageBlob);
- } catch (e) {
- if (e.message.endsWith(CustomErrorMessage.NotAvailable)) {
- moduleState.isNativeJPEGConversionNotAvailable = true;
- } else {
- log.error("Native conversion to JPEG failed", e);
- }
- }
- }
-
- if (extension == "heic" || extension == "heif") {
- // For HEIC/HEIF files we can use our web HEIC converter.
- return await heicToJPEG(imageBlob);
- }
-
- return undefined;
- } catch (e) {
- log.error(`Failed to get renderable image for ${fileName}`, e);
- return undefined;
- }
-};
-
-const nativeConvertToJPEG = async (imageBlob: Blob) => {
- const startTime = Date.now();
- const imageData = new Uint8Array(await imageBlob.arrayBuffer());
- const electron = globalThis.electron;
- // If we're running in a worker, we need to reroute the request back to
- // the main thread since workers don't have access to the `window` (and
- // thus, to the `window.electron`) object.
- const jpegData = electron
- ? await electron.convertToJPEG(imageData)
- : await workerBridge.convertToJPEG(imageData);
- log.debug(() => `Native JPEG conversion took ${Date.now() - startTime} ms`);
- return new Blob([jpegData], { type: "image/jpeg" });
-};
-
-export function isSupportedRawFormat(exactType: string) {
- return SUPPORTED_RAW_FORMATS.includes(exactType.toLowerCase());
-}
-
export async function changeFilesVisibility(
files: EnteFile[],
visibility: VISIBILITY_STATE,
diff --git a/web/apps/photos/src/services/detect-type.ts b/web/packages/new/photos/utils/detect-type.ts
similarity index 97%
rename from web/apps/photos/src/services/detect-type.ts
rename to web/packages/new/photos/utils/detect-type.ts
index e92e10bf82..e7447587d6 100644
--- a/web/apps/photos/src/services/detect-type.ts
+++ b/web/packages/new/photos/utils/detect-type.ts
@@ -78,7 +78,7 @@ export const detectFileTypeInfoFromChunk = async (
const known = KnownFileTypeInfos.find((f) => f.extension == extension);
if (known) return known;
- if (KnownNonMediaFileExtensions.includes(extension))
+ if (extension && KnownNonMediaFileExtensions.includes(extension))
throw Error(CustomError.UNSUPPORTED_FILE_FORMAT);
throw e;
diff --git a/web/packages/new/photos/utils/file.ts b/web/packages/new/photos/utils/file.ts
index d5d2892254..9674b2f504 100644
--- a/web/packages/new/photos/utils/file.ts
+++ b/web/packages/new/photos/utils/file.ts
@@ -1,4 +1,40 @@
+import { isNonWebImageFileExtension } from "@/media/formats";
+import { heicToJPEG } from "@/media/heic-convert";
+import log from "@/next/log";
+import { CustomErrorMessage } from "@/next/types/ipc";
+import { workerBridge } from "@/next/worker/worker-bridge";
+import isElectron from "is-electron";
import type { EnteFile } from "../types/file";
+import { detectFileTypeInfo } from "./detect-type";
+
+const SUPPORTED_RAW_FORMATS = [
+ "heic",
+ "rw2",
+ "tiff",
+ "arw",
+ "cr3",
+ "cr2",
+ "nef",
+ "psd",
+ "dng",
+ "tif",
+];
+
+class ModuleState {
+ /**
+ * This will be set to true if we get an error from the Node.js side of our
+ * desktop app telling us that native JPEG conversion is not available for
+ * the current OS/arch combination.
+ *
+ * That way, we can stop pestering it again and again (saving an IPC
+ * round-trip).
+ *
+ * Note the double negative when it is used.
+ */
+ isNativeJPEGConversionNotAvailable = false;
+}
+
+const moduleState = new ModuleState();
/**
* [Note: File name for local EnteFile objects]
@@ -28,3 +64,80 @@ export function mergeMetadata(files: EnteFile[]): EnteFile[] {
return file;
});
}
+
+/**
+ * The returned blob.type is filled in, whenever possible, with the MIME type of
+ * the data that we're dealing with.
+ */
+export const getRenderableImage = async (fileName: string, imageBlob: Blob) => {
+ try {
+ const tempFile = new File([imageBlob], fileName);
+ const fileTypeInfo = await detectFileTypeInfo(tempFile);
+ log.debug(
+ () =>
+ `Need renderable image for ${JSON.stringify({ fileName, ...fileTypeInfo })}`,
+ );
+ const { extension } = fileTypeInfo;
+
+ if (!isNonWebImageFileExtension(extension)) {
+ // Either it is something that the browser already knows how to
+ // render, or something we don't even about yet.
+ const mimeType = fileTypeInfo.mimeType;
+ if (!mimeType) {
+ log.info(
+ "Trying to render a file without a MIME type",
+ fileName,
+ );
+ return imageBlob;
+ } else {
+ return new Blob([imageBlob], { type: mimeType });
+ }
+ }
+
+ const available = !moduleState.isNativeJPEGConversionNotAvailable;
+ if (isElectron() && available && isSupportedRawFormat(extension)) {
+ // If we're running in our desktop app, see if our Node.js layer can
+ // convert this into a JPEG using native tools for us.
+ try {
+ return await nativeConvertToJPEG(imageBlob);
+ } catch (e) {
+ if (
+ e instanceof Error &&
+ e.message.endsWith(CustomErrorMessage.NotAvailable)
+ ) {
+ moduleState.isNativeJPEGConversionNotAvailable = true;
+ } else {
+ log.error("Native conversion to JPEG failed", e);
+ }
+ }
+ }
+
+ if (extension == "heic" || extension == "heif") {
+ // For HEIC/HEIF files we can use our web HEIC converter.
+ return await heicToJPEG(imageBlob);
+ }
+
+ return undefined;
+ } catch (e) {
+ log.error(`Failed to get renderable image for ${fileName}`, e);
+ return undefined;
+ }
+};
+
+const nativeConvertToJPEG = async (imageBlob: Blob) => {
+ const startTime = Date.now();
+ const imageData = new Uint8Array(await imageBlob.arrayBuffer());
+ const electron = globalThis.electron;
+ // If we're running in a worker, we need to reroute the request back to
+ // the main thread since workers don't have access to the `window` (and
+ // thus, to the `window.electron`) object.
+ const jpegData = electron
+ ? await electron.convertToJPEG(imageData)
+ : await workerBridge.convertToJPEG(imageData);
+ log.debug(() => `Native JPEG conversion took ${Date.now() - startTime} ms`);
+ return new Blob([jpegData], { type: "image/jpeg" });
+};
+
+export function isSupportedRawFormat(exactType: string) {
+ return SUPPORTED_RAW_FORMATS.includes(exactType.toLowerCase());
+}