Skip to content

armgit / ente

Public Imported from GitHub
0

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

Move force conversion decision to the converter

Manav Rathi3b2bc8769f8c
diff --git a/web/apps/photos/src/components/PhotoFrame.tsx b/web/apps/photos/src/components/PhotoFrame.tsx
index a69766c937..2596e2c61a 100644
--- a/web/apps/photos/src/components/PhotoFrame.tsx
+++ b/web/apps/photos/src/components/PhotoFrame.tsx
@@ -348,23 +348,7 @@ const PhotoFrame = ({
log.info(`[${item.id}] doesn't have thumbnail`);
thumbFetching[item.id] = true;
const url = await DownloadManager.getThumbnailForPreview(item);
- try {
- if (updateURL(index)(item.id, url)) {
- log.info(
- `[${item.id}] calling invalidateCurrItems for thumbnail msrc: ${!!item.msrc}`,
- );
- instance.invalidateCurrItems();
- if ((instance as any).isOpen()) {
- instance.updateSize(true);
- }
- }
- } catch (e) {
- log.error(
- "updating photoswipe after msrc url update failed",
- e,
- );
- // ignore
- }
+ updateThumb(instance, index, item, url, false);
} catch (e) {
log.error("getSlideData failed get msrc url failed", e);
thumbFetching[item.id] = false;
@@ -450,22 +434,7 @@ const PhotoFrame = ({
);
}
} else {
- try {
- if (await updateSrcURL(index, item.id, srcURLs)) {
- log.info(
- `[${item.id}] calling invalidateCurrItems for src, source loaded: ${item.isSourceLoaded}`,
- );
- instance.invalidateCurrItems();
- if ((instance as any).isOpen()) {
- instance.updateSize(true);
- }
- }
- } catch (e) {
- log.error(
- "updating photoswipe after src url update failed",
- e,
- );
- }
+ await updateOrig(instance, index, item, srcURLs, false);
}
} catch (e) {
log.error("getSlideData failed get src url failed", e);
@@ -474,26 +443,15 @@ const PhotoFrame = ({
}
};
- const getConvertedItem = async (
+ const updateThumb = (
instance: PhotoSwipe<PhotoSwipe.Options>,
index: number,
item: EnteFile,
+ url: string,
+ forceUpdate?: boolean,
) => {
- if (
- item.metadata.fileType !== FileType.video &&
- item.metadata.fileType !== FileType.livePhoto
- ) {
- log.error("getConvertedVideo called for non video file");
- return;
- }
- if (item.conversionFailed) {
- log.error(
- "getConvertedVideo called for file that conversion failed",
- );
- return;
- }
try {
- if (updateURL(index)(item.id, item.msrc, true)) {
+ if (updateURL(index)(item.id, url, forceUpdate)) {
log.info(
`[${item.id}] calling invalidateCurrItems for thumbnail msrc: ${!!item.msrc}`,
);
@@ -506,6 +464,38 @@ const PhotoFrame = ({
log.error("updating photoswipe after msrc url update failed", e);
// ignore
}
+ };
+
+ const updateOrig = async (
+ instance: PhotoSwipe<PhotoSwipe.Options>,
+ index: number,
+ item: EnteFile,
+ srcURL: SourceURLs,
+ forceUpdate?: boolean,
+ ) => {
+ try {
+ if (await updateSrcURL(index, item.id, srcURL, forceUpdate)) {
+ log.info(
+ `[${item.id}] calling invalidateCurrItems for src, source loaded: ${item.isSourceLoaded}`,
+ );
+ instance.invalidateCurrItems();
+ if ((instance as any).isOpen()) {
+ instance.updateSize(true);
+ }
+ }
+ } catch (e) {
+ log.error("updating photoswipe after src url update failed", e);
+ throw e;
+ }
+ };
+
+ const forceConvertItem = async (
+ instance: PhotoSwipe<PhotoSwipe.Options>,
+ index: number,
+ item: EnteFile,
+ ) => {
+ updateThumb(instance, index, item, item.msrc, true);
+
try {
log.info(
`[${item.id}] new file getConvertedVideo request ${item.metadata.title}}`,
@@ -513,23 +503,10 @@ const PhotoFrame = ({
fetching[item.id] = true;
const srcURL = await DownloadManager.getFileForPreview(item, {
- forceConvertVideos: true,
+ forceConvert: true,
});
- try {
- if (await updateSrcURL(index, item.id, srcURL, true)) {
- log.info(
- `[${item.id}] calling invalidateCurrItems for src, source loaded: ${item.isSourceLoaded}`,
- );
- instance.invalidateCurrItems();
- if ((instance as any).isOpen()) {
- instance.updateSize(true);
- }
- }
- } catch (e) {
- log.error("updating photoswipe after src url update failed", e);
- throw e;
- }
+ await updateOrig(instance, index, item, srcURL, true);
} catch (e) {
log.error("getConvertedVideo failed get src url failed", e);
fetching[item.id] = false;
@@ -571,7 +548,7 @@ const PhotoFrame = ({
currentIndex={currentIndex}
onClose={handleClose}
gettingData={getSlideData}
- getConvertedItem={getConvertedItem}
+ forceConvertItem={forceConvertItem}
favItemIds={favItemIds}
markTempDeleted={markTempDeleted}
isTrashCollection={activeCollectionID === TRASH_SECTION}
diff --git a/web/apps/photos/src/components/PhotoViewer/index.tsx b/web/apps/photos/src/components/PhotoViewer/index.tsx
index 5c711892ac..db2cbfe79f 100644
--- a/web/apps/photos/src/components/PhotoViewer/index.tsx
+++ b/web/apps/photos/src/components/PhotoViewer/index.tsx
@@ -111,7 +111,7 @@ export interface PhotoViewerProps {
currentIndex?: number;
onClose?: (needUpdate: boolean) => void;
gettingData: (instance: any, index: number, item: EnteFile) => void;
- getConvertedItem: (instance: any, index: number, item: EnteFile) => void;
+ forceConvertItem: (instance: any, index: number, item: EnteFile) => void;
id?: string;
favItemIds: Set<number>;
markTempDeleted?: (tempDeletedFiles: EnteFile[]) => void;
@@ -125,6 +125,8 @@ export interface PhotoViewerProps {
}
function PhotoViewer(props: PhotoViewerProps) {
+ const { id, forceConvertItem } = props;
+
const galleryContext = useContext(GalleryContext);
const { showLoadingBar, hideLoadingBar, showMiniDialog } =
useContext(AppContext);
@@ -151,7 +153,7 @@ function PhotoViewer(props: PhotoViewerProps) {
defaultLivePhotoDefaultOptions,
);
const [isOwnFile, setIsOwnFile] = useState(false);
- const [showConvertBtn, setShowConvertBtn] = useState(false);
+ const [showConvertButton, setShowConvertButton] = useState(false);
const [isSourceLoaded, setIsSourceLoaded] = useState(false);
const [isInFullScreenMode, setIsInFullScreenMode] = useState(false);
@@ -366,14 +368,7 @@ function PhotoViewer(props: PhotoViewerProps) {
}
function updateShowConvertBtn(file: EnteFile) {
- const shouldShowConvertBtn =
- isDesktop &&
- (file.metadata.fileType === FileType.video ||
- file.metadata.fileType === FileType.livePhoto) &&
- !file.isConverted &&
- file.isSourceLoaded &&
- !file.conversionFailed;
- setShowConvertBtn(shouldShowConvertBtn);
+ setShowConvertButton(!!file.canForceConvert);
}
function updateConversionFailedNotification(file: EnteFile) {
@@ -752,16 +747,14 @@ function PhotoViewer(props: PhotoViewerProps) {
}
};
- const triggerManualConvert = () => {
- props.getConvertedItem(
+ const handleForceConvert = () =>
+ forceConvertItem(
photoSwipe,
photoSwipe.getCurrentIndex(),
photoSwipe.currItem as EnteFile,
);
- };
const scheduleUpdate = () => (needUpdate.current = true);
- const { id } = props;
return (
<>
<div
@@ -943,11 +936,11 @@ function PhotoViewer(props: PhotoViewerProps) {
</button>
</>
)}
- {showConvertBtn && (
+ {showConvertButton && (
<button
title={t("CONVERT")}
className="pswp__button pswp__button--custom"
- onClick={triggerManualConvert}
+ onClick={handleForceConvert}
>
<ReplayIcon fontSize="small" />
</button>
diff --git a/web/apps/photos/src/utils/photoFrame/index.ts b/web/apps/photos/src/utils/photoFrame/index.ts
index 8d752d1bd8..1072539157 100644
--- a/web/apps/photos/src/utils/photoFrame/index.ts
+++ b/web/apps/photos/src/utils/photoFrame/index.ts
@@ -29,6 +29,7 @@ export function updateFileMsrcProps(file: EnteFile, url: string) {
file.w = window.innerWidth;
file.h = window.innerHeight;
file.msrc = url;
+ file.canForceConvert = false;
file.isSourceLoaded = false;
file.conversionFailed = false;
file.isConverted = false;
@@ -55,6 +56,7 @@ export async function updateFileSrcProps(
file.metadata.fileType === FileType.livePhoto
? srcURLs.type === "livePhoto"
: true;
+ file.canForceConvert = srcURLs.canForceConvert;
file.isConverted = !isOriginal;
file.conversionFailed = !isRenderable;
file.srcURLs = srcURLs;
diff --git a/web/packages/gallery/utils/convert.ts b/web/packages/gallery/utils/convert.ts
index a4fcedd66c..5c5b865c9e 100644
--- a/web/packages/gallery/utils/convert.ts
+++ b/web/packages/gallery/utils/convert.ts
@@ -181,8 +181,9 @@ export const playableVideoBlob = async (
/**
* Try to see if the browser thinks it can play the video pointed to by the
- * given {@link url} by creating a <video>
- * element and initiating playback.
+ * given {@link url} by creating a <video> element and initiating playback.
+ *
+ * [Note: Forcing conversion of playable videos]
*
* Note that this can sometimes cause false positives if the browser can play
* some of the streams in the video, but not all. For example, the browser may
@@ -193,7 +194,8 @@ export const playableVideoBlob = async (
*
* As an escape hatch, we provide a force convert button in the UI for such
* cases, which'll cause the {@link forceConvert} flag in our caller function to
- * be set (and force a conversion)
+ * be set. If so, it'll bypasses this preflight check we use to see if the
+ * browser can already play the video, and instead will always be transcoded.
*/
const isPlaybackPossible = async (url: string) =>
new Promise((resolve) => {
diff --git a/web/packages/media/file.ts b/web/packages/media/file.ts
index 69182c1d5e..788f2a2688 100644
--- a/web/packages/media/file.ts
+++ b/web/packages/media/file.ts
@@ -93,6 +93,7 @@ export interface EnteFile
isSourceLoaded?: boolean;
conversionFailed?: boolean;
isConverted?: boolean;
+ canForceConvert?: boolean;
}
export interface LivePhotoSourceURL {
@@ -110,6 +111,12 @@ export interface SourceURLs {
isOriginal: boolean;
isRenderable: boolean;
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.
*
diff --git a/web/packages/new/photos/services/download.ts b/web/packages/new/photos/services/download.ts
index 5df2fa61d7..bf9c182156 100644
--- a/web/packages/new/photos/services/download.ts
+++ b/web/packages/new/photos/services/download.ts
@@ -1,6 +1,7 @@
// TODO: Remove this override
/* eslint-disable @typescript-eslint/no-empty-function */
+import { isDesktop } from "@/base/app";
import { blobCache, type BlobCache } from "@/base/blob-cache";
import {
decryptStreamBytes,
@@ -177,18 +178,16 @@ class DownloadManagerImpl {
}
/**
- * The `forceConvertVideos` option is true when the user presses the
- * "Convert" button. It bypasses the preflight check we use to see if the
- * browser can already play the video, and instead always does the
- * transcoding.
+ * The `forceConvert` option is true when the user presses the "Convert"
+ * button. See: [Note: Forcing conversion of playable videos].
*/
getFileForPreview = async (
file: EnteFile,
- opts?: { forceConvertVideos?: boolean },
+ opts?: { forceConvert?: boolean },
): Promise<SourceURLs | undefined> => {
this.ensureInitialized();
try {
- const forceConvertVideos = opts?.forceConvertVideos ?? false;
+ const forceConvert = opts?.forceConvert ?? false;
const getFileForPreviewPromise = async () => {
const fileBlob = await new Response(
await this.getFile(file, true),
@@ -203,14 +202,11 @@ class DownloadManagerImpl {
file,
fileBlob,
originalFileURL as string,
- forceConvertVideos,
+ forceConvert,
);
return converted;
};
- if (
- forceConvertVideos ||
- !this.fileConversionPromises.has(file.id)
- ) {
+ if (forceConvert || !this.fileConversionPromises.has(file.id)) {
this.fileConversionPromises.set(
file.id,
getFileForPreviewPromise(),
@@ -438,7 +434,7 @@ async function getRenderableFileURL(
file: EnteFile,
fileBlob: Blob,
originalFileURL: string,
- forceConvertVideos: boolean,
+ forceConvert: boolean,
): Promise<SourceURLs> {
const existingOrNewObjectURL = (convertedBlob: Blob | null | undefined) =>
convertedBlob
@@ -452,6 +448,7 @@ async function getRenderableFileURL(
let isRenderable: boolean;
let type: SourceURLs["type"] = "normal";
let mimeType: string | undefined;
+ let canForceConvert = false;
const fileName = file.metadata.title;
switch (file.metadata.fileType) {
@@ -475,13 +472,17 @@ async function getRenderableFileURL(
const convertedBlob = await playableVideoBlob(
fileName,
fileBlob,
- forceConvertVideos,
+ forceConvert,
);
const convertedURL = existingOrNewObjectURL(convertedBlob);
url = convertedURL;
isOriginal = convertedURL === originalFileURL;
isRenderable = !!convertedURL;
mimeType = convertedBlob?.type;
+
+ canForceConvert =
+ isDesktop && !forceConvert && isOriginal && isRenderable;
+
break;
}
default: {
@@ -493,7 +494,14 @@ async function getRenderableFileURL(
}
// TODO: Can we remove this non-null assertion and reflect it in the types?
- return { url: url!, isOriginal, isRenderable, type, mimeType };
+ return {
+ url: url!,
+ isOriginal,
+ isRenderable,
+ type,
+ mimeType,
+ canForceConvert,
+ };
}
async function getRenderableLivePhotoURL(