Skip to content

armgit / ente

Public Imported from GitHub
0

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

Reclaim preview

Manav Rathiff3033f1ae7f
diff --git a/web/apps/photos/src/components/PhotoFrame.tsx b/web/apps/photos/src/components/PhotoFrame.tsx
index 29a37169c6..3603e43535 100644
--- a/web/apps/photos/src/components/PhotoFrame.tsx
+++ b/web/apps/photos/src/components/PhotoFrame.tsx
@@ -2,7 +2,7 @@ import log from "@/base/log";
import {
downloadManager,
type LivePhotoSourceURL,
- type SourceURLs,
+ type RenderableSourceURLs,
} from "@/gallery/services/download";
import { EnteFile } from "@/media/file";
import { FileType } from "@/media/file-type";
@@ -44,7 +44,7 @@ const PHOTOSWIPE_HASH_SUFFIX = "&opened";
*/
export type DisplayFile = EnteFile & {
src?: string;
- srcURLs?: SourceURLs;
+ srcURLs?: RenderableSourceURLs;
msrc?: string;
html?: string;
w?: number;
@@ -365,12 +365,12 @@ const PhotoFrame = ({
try {
log.info(`[${item.id}] new file src request`);
fetching[item.id] = true;
- const srcURLs = await downloadManager.getFileForPreview(item);
+ const srcURLs = await downloadManager.renderableSourceURLs(item);
if (item.metadata.fileType === FileType.livePhoto) {
const srcImgURL = srcURLs.url as LivePhotoSourceURL;
const imageURL = await srcImgURL.image();
- const dummyImgSrcUrl: SourceURLs = {
+ const dummyImgSrcUrl: RenderableSourceURLs = {
url: imageURL,
type: "normal",
};
@@ -381,7 +381,7 @@ const PhotoFrame = ({
}
const videoURL = await srcImgURL.video();
- const loadedLivePhotoSrcURL: SourceURLs = {
+ const loadedLivePhotoSrcURL: RenderableSourceURLs = {
url: { video: videoURL, image: imageURL },
type: "livePhoto",
};
@@ -429,7 +429,7 @@ const PhotoFrame = ({
instance: PhotoSwipe<PhotoSwipe.Options>,
index: number,
item: DisplayFile,
- srcURL: SourceURLs,
+ srcURL: RenderableSourceURLs,
overwrite: boolean,
) => {
const file = displayFiles[index];
@@ -463,7 +463,7 @@ const PhotoFrame = ({
);
fetching[item.id] = true;
- const srcURL = await downloadManager.getFileForPreview(item, {
+ const srcURL = await downloadManager.renderableSourceURLs(item, {
forceConvert: true,
});
@@ -537,7 +537,7 @@ const updateDisplayFileThumbnail = (file: DisplayFile, url: string) => {
const updateDisplayFileSource = (
file: DisplayFile,
- srcURLs: SourceURLs,
+ srcURLs: RenderableSourceURLs,
enableDownload: boolean,
) => {
const { url } = srcURLs;
diff --git a/web/apps/photos/src/components/PhotoViewer/ImageEditorOverlay.tsx b/web/apps/photos/src/components/PhotoViewer/ImageEditorOverlay.tsx
index f2063c4f12..6ae746b9e3 100644
--- a/web/apps/photos/src/components/PhotoViewer/ImageEditorOverlay.tsx
+++ b/web/apps/photos/src/components/PhotoViewer/ImageEditorOverlay.tsx
@@ -370,7 +370,7 @@ export const ImageEditorOverlay: React.FC<ImageEditorOverlayProps> = (
const ctx = canvasRef.current.getContext("2d");
ctx.imageSmoothingEnabled = false;
if (!fileURL) {
- const srcURLs = await downloadManager.getFileForPreview(
+ const srcURLs = await downloadManager.renderableSourceURLs(
props.file,
);
img.src = srcURLs.url as string;
diff --git a/web/packages/gallery/services/download.ts b/web/packages/gallery/services/download.ts
index 20864eb977..43e134168d 100644
--- a/web/packages/gallery/services/download.ts
+++ b/web/packages/gallery/services/download.ts
@@ -34,7 +34,21 @@ export interface LoadedLivePhotoSourceURL {
video: string;
}
-export interface SourceURLs {
+/**
+ * URL for the original image or video, but with potential conversions applied
+ * to make it more likely that the browser (or desktop app) will be able to
+ * render (or play) it.
+ *
+ * This structure originally arose as the source object URL, and a bunch of
+ * associated metadata, that the file viewer needs when the user wants to see
+ * the original file (as opposed to say, the thumbnail which we were showing
+ * them earlier as the file is getting downloaded).
+ *
+ * The word "renderable" is not a guarantee, but rather a best effort indicator
+ * as we might not always be able to convert all formats to something that the
+ * browser (or desktop app) can show.
+ */
+export interface RenderableSourceURLs {
url: string | LivePhotoSourceURL | LoadedLivePhotoSourceURL;
type: "normal" | "livePhoto";
/**
@@ -95,12 +109,25 @@ class DownloadManager {
* Unlike {@link thumbnailURLPromises}, there is no guarantee that the
* browser will be able to render the original file (e.g. it might be in an
* unsupported format). If a renderable URL is needed for the file,
- * {@link renderableFileData} should be used instead.
+ * {@link renderableSourceURL} should be used instead.
*
* The entries are keyed by the file ID.
*/
private fileURLPromises = new Map<number, Promise<string>>();
- private fileConversionPromises = new Map<number, Promise<SourceURLs>>();
+ /**
+ * An in-memory cache for {@link RenderableSourceURLs} for a file.
+ *
+ * These are saved as a result of invocation of {@link renderableSourceURL},
+ * which goes one step beyond {@link fileURLPromises}, and also attempts to
+ * convert the downloaded file into a URL that the browser (or the desktop
+ * app) is likely to be able to render or play.
+ *
+ * The entries are keyed by file ID.
+ */
+ private renderableSourceURLPromises = new Map<
+ number,
+ Promise<RenderableSourceURLs>
+ >();
/**
* A map from file ID to the progress (0-100%) of its active download (if
@@ -138,7 +165,7 @@ class DownloadManager {
this.publicAlbumsCredentials = undefined;
this.thumbnailURLPromises.clear();
this.fileURLPromises.clear();
- this.fileConversionPromises.clear();
+ this.renderableSourceURLPromises.clear();
this.fileDownloadProgress.clear();
this.fileDownloadProgressListeners = [];
}
@@ -255,39 +282,42 @@ class DownloadManager {
}
/**
+ * Return a URL (and associated metadata) that can be used to show the given
+ * {@link file} within the app, converting its format (on the fly) if needed
+ * (if possible).
+ *
+ * See the documentation of {@link RenderableSourceURLs} for more details.
+ *
* The `forceConvert` option is true when the user presses the "Convert"
* button. See: [Note: Forcing conversion of playable videos].
*/
- getFileForPreview = async (
+ renderableSourceURLs = async (
file: EnteFile,
opts?: { forceConvert?: boolean },
- ): Promise<SourceURLs | undefined> => {
+ ): Promise<RenderableSourceURLs | undefined> => {
+ const forceConvert = opts?.forceConvert ?? false;
+ if (forceConvert || !this.renderableSourceURLPromises.has(file.id)) {
+ this.renderableSourceURLPromises.set(
+ file.id,
+ (async () => {
+ const originalFileURL =
+ await this.fileURLDownloadAndCacheIfNeeded(file);
+ const res = await fetch(originalFileURL);
+ const fileBlob = await res.blob();
+ return await getRenderableFileURL(
+ file,
+ fileBlob,
+ originalFileURL,
+ forceConvert,
+ );
+ })(),
+ );
+ }
try {
- const forceConvert = opts?.forceConvert ?? false;
- const getFileForPreviewPromise = async () => {
- const originalFileURL =
- await this.fileURLDownloadAndCacheIfNeeded(file);
- const res = await fetch(originalFileURL);
- const fileBlob = await res.blob();
- const converted = await getRenderableFileURL(
- file,
- fileBlob,
- originalFileURL,
- forceConvert,
- );
- return converted;
- };
- if (forceConvert || !this.fileConversionPromises.has(file.id)) {
- this.fileConversionPromises.set(
- file.id,
- getFileForPreviewPromise(),
- );
- }
- const fileURLs = await this.fileConversionPromises.get(file.id);
- return fileURLs;
+ return await this.renderableSourceURLPromises.get(file.id);
} catch (e) {
- this.fileConversionPromises.delete(file.id);
- log.error("download manager getFileForPreview Failed", e);
+ log.error("Failed to obtain renderableSourceURLs", e);
+ this.renderableSourceURLPromises.delete(file.id);
throw e;
}
};
@@ -497,7 +527,7 @@ async function getRenderableFileURL(
fileBlob: Blob,
originalFileURL: string,
forceConvert: boolean,
-): Promise<SourceURLs> {
+): Promise<RenderableSourceURLs> {
const existingOrNewObjectURL = (convertedBlob: Blob | null | undefined) =>
convertedBlob
? convertedBlob === fileBlob
@@ -505,8 +535,8 @@ async function getRenderableFileURL(
: URL.createObjectURL(convertedBlob)
: undefined;
- let url: SourceURLs["url"] | undefined;
- let type: SourceURLs["type"] = "normal";
+ let url: RenderableSourceURLs["url"] | undefined;
+ let type: RenderableSourceURLs["type"] = "normal";
let mimeType: string | undefined;
let canForceConvert = false;