Skip to content

armgit / ente

Public Imported from GitHub
0

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

scaffold wip

Manav Rathi291833e1e177
diff --git a/web/packages/gallery/services/download.ts b/web/packages/gallery/services/download.ts
index c56e8bc8af..555cec7834 100644
--- a/web/packages/gallery/services/download.ts
+++ b/web/packages/gallery/services/download.ts
@@ -1,4 +1,3 @@
-import { isDesktop } from "ente-base/app";
import { blobCache, type BlobCache } from "ente-base/blob-cache";
import {
decryptStreamBytes,
@@ -55,12 +54,6 @@ export interface RenderableSourceURLs {
url: string | LivePhotoSourceURL | LoadedLivePhotoSourceURL;
originalImageBlob?: Blob | undefined;
type: "normal" | "livePhoto";
- /**
- * `true` if there is potential conversion that can still be applied.
- *
- * See: [Note: Forcing conversion of playable videos]
- */
- canForceConvert?: boolean;
/**
* Best effort attempt at obtaining the MIME type.
*
@@ -582,14 +575,11 @@ const wrapErrors = <T>(op: () => Promise<T>) =>
* Create and return a {@link RenderableSourceURLs} for the given {@link file},
* where {@link originalFileURLPromise} is a promise that resolves with an
* (object) URL to the contents of the original file.
- *
- * @param forceConvert `true` when the user presses the "Convert" button. See:
- * [Note: Forcing conversion of playable videos].
*/
const createRenderableSourceURLs = async (
file: EnteFile,
originalFileURLPromise: Promise<string>,
- forceConvert: boolean,
+ opts: RenderableSourceURLsOpts,
): Promise<RenderableSourceURLs> => {
const originalFileURL = await originalFileURLPromise;
const fileBlob = await fetch(originalFileURL).then((res) => res.blob());
@@ -607,7 +597,6 @@ const createRenderableSourceURLs = async (
| undefined;
let type: RenderableSourceURLs["type"] = "normal";
let mimeType: string | undefined;
- let canForceConvert = false;
const fileName = file.metadata.title;
switch (file.metadata.fileType) {
@@ -625,20 +614,10 @@ const createRenderableSourceURLs = async (
break;
}
case FileType.video: {
- const convertedBlob = await playableVideoBlob(
- fileName,
- fileBlob,
- forceConvert,
- );
+ const convertedBlob = await playableVideoBlob(fileName, fileBlob);
const convertedURL = existingOrNewObjectURL(convertedBlob);
url = convertedURL;
mimeType = convertedBlob?.type;
-
- const isOriginal = convertedURL === originalFileURL;
- const isRenderable = !!convertedURL;
- canForceConvert =
- isDesktop && !forceConvert && isOriginal && isRenderable;
-
break;
}
default: {
@@ -650,7 +629,7 @@ const createRenderableSourceURLs = async (
// TODO: Can we remove this non-null assertion and reflect it in the types?
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
- return { url: url!, originalImageBlob, type, mimeType, canForceConvert };
+ return { url: url!, originalImageBlob, type, mimeType };
};
async function getRenderableLivePhotoURL(
diff --git a/web/packages/gallery/utils/convert.ts b/web/packages/gallery/utils/convert.ts
index 9430c16390..008e1cb0f0 100644
--- a/web/packages/gallery/utils/convert.ts
+++ b/web/packages/gallery/utils/convert.ts
@@ -6,6 +6,22 @@ import { heicToJPEG } from "ente-media/heic-convert";
import { convertToMP4 } from "../services/ffmpeg";
import { detectFileTypeInfo } from "./detect-type";
+export interface RenderableBlobOpts {
+ /**
+ * If true, then the conversion step is skipped when for a format that we
+ * usually always convert, we are able to detect that the browser might be
+ * able to render the file natively.
+ *
+ * [Note: Browser HEIC support]
+ *
+ * Currently this has an effect only for HEIC files. Safari can show them
+ * natively, and if we detect HEIC support (by trying to ask the browser to
+ * show a small HEIC file), we skip the conversion to JPEG. This is then
+ * used by the file viewer to bypass the costly Wasm HEIC conversion.
+ */
+ avoidFormatConversion?: boolean;
+}
+
/**
* Return a new {@link Blob} containing an image's data in a format that the
* browser (likely) knows how to render (in an img tag, or on the canvas).
@@ -17,6 +33,8 @@ import { detectFileTypeInfo } from "./detect-type";
*
* @param fileName The name of the file whose data {@link imageBlob} is.
*
+ * @param opts Options to tweak the conversion flowchart.
+ *
* The logic used by this function is:
*
* 1. Try to detect the MIME type of the file from its contents and/or name.
@@ -29,8 +47,8 @@ import { detectFileTypeInfo } from "./detect-type";
* desktop app can natively convert to a JPEG (using ffmpeg), do that and
* return the resultant JPEG blob.
*
- * 4. If this is an HEIC file, use our (Wasm) HEIC converter and return the
- * resultant JPEG blob.
+ * 4. If this is an HEIC file (and `opts.avoidFormatConversion` was not set),
+ * then use our (Wasm) HEIC converter and return the resultant JPEG blob.
*
* 5. Otherwise return the original (with the MIME type if we were able to
* deduce one).
@@ -40,6 +58,7 @@ import { detectFileTypeInfo } from "./detect-type";
export const renderableImageBlob = async (
imageBlob: Blob,
fileName: string,
+ opts: RenderableBlobOpts,
) => {
try {
const file = new File([imageBlob], fileName);
@@ -65,6 +84,13 @@ export const renderableImageBlob = async (
// to our web HEIC converter.
if (isHEICExtension(extension)) {
+ // However, if the `avoidFormatConversion` option is set, skip
+ // conversion if we're able to detect that the browser can
+ // already render them (e.g. Safari 18+).
+ if (opts.avoidFormatConversion) {
+ console.log("TODO implement me");
+ }
+
return await heicToJPEG(imageBlob);
}
}
@@ -124,14 +150,9 @@ const nativeConvertToJPEG = async (imageBlob: Blob) => {
*
* - Otherwise try to convert using FFmpeg. This conversion always happens on
* the desktop app, but in the browser the conversion only happens for short
- * videos since the Wasm FFmpeg implementation is much slower. There is also a
- * flag to force this conversion regardless.
+ * videos since the Wasm FFmpeg implementation is much slower.
*/
-export const playableVideoBlob = async (
- fileName: string,
- videoBlob: Blob,
- forceConvert: boolean,
-) => {
+export const playableVideoBlob = async (fileName: string, videoBlob: Blob) => {
const converted = async () => {
try {
log.info(`Converting ${fileName} to mp4`);
@@ -143,9 +164,6 @@ export const playableVideoBlob = async (
}
};
- // If we've been asked to force convert, do it regardless of anything else.
- if (forceConvert) return converted();
-
const isPlayable = await isPlaybackPossible(URL.createObjectURL(videoBlob));
if (isPlayable) return videoBlob;