Skip to content

armgit / ente

Public Imported from GitHub
0

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

Tweak

Manav Rathid740ebf0f5a1
diff --git a/web/apps/photos/src/components/PhotoFrame.tsx b/web/apps/photos/src/components/PhotoFrame.tsx
index 330d67cb35..607067d953 100644
--- a/web/apps/photos/src/components/PhotoFrame.tsx
+++ b/web/apps/photos/src/components/PhotoFrame.tsx
@@ -362,7 +362,9 @@ const PhotoFrame = ({
}
log.info(`[${item.id}] doesn't have thumbnail`);
thumbFetching[item.id] = true;
- const url = await DownloadManager.getThumbnailForPreview(item);
+ // URL will always be defined (unless an error is thrown) since
+ // we are not passing the `cachedOnly` option.
+ const url = await DownloadManager.renderableThumbnailURL(item)!;
updateThumb(instance, index, item, url, false);
} catch (e) {
log.error("getSlideData failed get msrc url failed", e);
diff --git a/web/apps/photos/src/components/pages/gallery/PreviewCard.tsx b/web/apps/photos/src/components/pages/gallery/PreviewCard.tsx
index 936152561e..a84709b010 100644
--- a/web/apps/photos/src/components/pages/gallery/PreviewCard.tsx
+++ b/web/apps/photos/src/components/pages/gallery/PreviewCard.tsx
@@ -274,7 +274,7 @@ export default function PreviewCard(props: IProps) {
return;
}
const url: string =
- await DownloadManager.getThumbnailForPreview(
+ await DownloadManager.renderableThumbnailURL(
file,
props.showPlaceholder,
);
diff --git a/web/packages/new/photos/components/Tiles.tsx b/web/packages/new/photos/components/Tiles.tsx
index 134b3b8d1f..18bf6a7aa1 100644
--- a/web/packages/new/photos/components/Tiles.tsx
+++ b/web/packages/new/photos/components/Tiles.tsx
@@ -72,7 +72,7 @@ export const ItemCard: React.FC<React.PropsWithChildren<ItemCardProps>> = ({
);
} else {
void downloadManager
- .getThumbnailForPreview(coverFile, isScrolling)
+ .renderableThumbnailURL(coverFile, isScrolling)
.then((url) => !didCancel && setCoverImageURL(url));
}
diff --git a/web/packages/new/photos/services/download.ts b/web/packages/new/photos/services/download.ts
index 7d15ad9444..2a8e7ba1fe 100644
--- a/web/packages/new/photos/services/download.ts
+++ b/web/packages/new/photos/services/download.ts
@@ -78,14 +78,21 @@ interface DownloadClient {
class DownloadManagerImpl {
private ready = false;
private downloadClient: DownloadClient | undefined;
- /** Local cache for thumbnails. Might not be available. */
- private thumbnailCache?: BlobCache;
+ /**
+ * Local cache for thumbnail blobs.
+ *
+ * Might not be available.
+ */
+ private thumbnailCache: BlobCache | undefined;
+ /**
+ * An in-memory cache for an object URL to a file's thumbnail.
+ *
+ * This object URL can be directly used to render the thumbnail (e.g. in an
+ * img tag). The entries are keyed by the file ID.
+ */
+ private thumbnailURLs = new Map<number, Promise<string | undefined>>();
private fileObjectURLPromises = new Map<number, Promise<SourceURLs>>();
private fileConversionPromises = new Map<number, Promise<SourceURLs>>();
- private thumbnailObjectURLPromises = new Map<
- number,
- Promise<string | undefined>
- >();
private fileDownloadProgress = new Map<number, number>();
@@ -122,7 +129,7 @@ class DownloadManagerImpl {
this.downloadClient = undefined;
this.fileObjectURLPromises.clear();
this.fileConversionPromises.clear();
- this.thumbnailObjectURLPromises.clear();
+ this.thumbnailURLs.clear();
this.fileDownloadProgress.clear();
this.progressUpdater = () => {};
}
@@ -144,13 +151,27 @@ class DownloadManagerImpl {
return decryptThumbnail({ encryptedData, decryptionHeader }, file.key);
};
- async getThumbnail(file: EnteFile, localOnly = false) {
+ /**
+ * Returns the thumbnail data for a file, downloading it if needed.
+ *
+ * The data is cached on disk for subsequent fetches.
+ *
+ * @param file The {@link EnteFile} whose thumbnail we want.
+ *
+ * @param cachedOnly If true, then the thumbnail is not downloaded if it is
+ * not already present in the disk cache.
+ *
+ * @returns The bytes of the thumbnail. This method can return `undefined`
+ * iff the thumbnail is not already cached, and {@link cachedOnly} is set to
+ * `true`.
+ */
+ async thumbnailBytes(file: EnteFile, cachedOnly = false) {
this.ensureInitialized();
const key = file.id.toString();
const cached = await this.thumbnailCache?.get(key);
if (cached) return new Uint8Array(await cached.arrayBuffer());
- if (localOnly) return undefined;
+ if (cachedOnly) return undefined;
const thumb = await this.downloadThumb(file);
await this.thumbnailCache?.put(key, new Blob([thumb]));
@@ -160,42 +181,39 @@ class DownloadManagerImpl {
/**
* Resolves with an URL that points to the file's thumbnail.
*
- * The thumbnail will be downloaded (unless {@link localOnly} is true) and
- * cached.
+ * The thumbnail will be downloaded if needed (unless {@link cachedOnly} is
+ * true). It will also be cached for subsequent fetches.
*
- * The optional {@link localOnly} parameter can be set to indicate that this
- * is being called as part of a scroll, so the downloader should not attempt
- * to download the file but should instead fulfill the request from the
- * cache. This avoids an unbounded flurry of requests on scroll, only
- * downloading when the position has quiescized.
+ * The optional {@link cachedOnly} parameter can be set to indicate that
+ * this is being called as part of a scroll, so the downloader should not
+ * attempt to download the file but should instead fulfill the request from
+ * the disk cache. This avoids an unbounded flurry of requests on scroll,
+ * only downloading when the position has quiescized.
*
* The returned URL is actually an object URL, but it should not be revoked
* since the download manager caches it for future use.
*/
- async getThumbnailForPreview(
+ async renderableThumbnailURL(
file: EnteFile,
- localOnly = false,
+ cachedOnly = false,
): Promise<string | undefined> {
this.ensureInitialized();
- try {
- if (!this.thumbnailObjectURLPromises.has(file.id)) {
- const thumbPromise = this.getThumbnail(file, localOnly);
- const thumbURLPromise = thumbPromise.then(
- (thumb) => thumb && URL.createObjectURL(new Blob([thumb])),
- );
- this.thumbnailObjectURLPromises.set(file.id, thumbURLPromise);
- }
- let thumb = await this.thumbnailObjectURLPromises.get(file.id);
- if (!thumb && !localOnly) {
- this.thumbnailObjectURLPromises.delete(file.id);
- thumb = await this.getThumbnailForPreview(file, localOnly);
- }
- return thumb;
- } catch (e) {
- this.thumbnailObjectURLPromises.delete(file.id);
- log.error("get DownloadManager preview Failed", e);
- throw e;
+
+ if (!this.thumbnailURLs.has(file.id)) {
+ const url = this.thumbnailBytes(file, cachedOnly).then((bytes) =>
+ bytes ? URL.createObjectURL(new Blob([bytes])) : undefined,
+ );
+ this.thumbnailURLs.set(file.id, url);
+ }
+
+ let thumb = await this.thumbnailURLs.get(file.id);
+ if (cachedOnly) return thumb;
+
+ if (!thumb) {
+ this.thumbnailURLs.delete(file.id);
+ thumb = await this.renderableThumbnailURL(file);
}
+ return thumb;
}
/**
diff --git a/web/packages/new/photos/services/ml/blob.ts b/web/packages/new/photos/services/ml/blob.ts
index 56fd258c06..e296dce73a 100644
--- a/web/packages/new/photos/services/ml/blob.ts
+++ b/web/packages/new/photos/services/ml/blob.ts
@@ -91,8 +91,8 @@ const fetchRenderableUploadItemBlob = async (
) => {
const fileType = file.metadata.fileType;
if (fileType == FileType.video) {
- const thumbnailData = await DownloadManager.getThumbnail(file);
- return new Blob([thumbnailData!]);
+ const data = await DownloadManager.thumbnailBytes(file);
+ return new Blob([data!]);
} else {
const blob = await readNonVideoUploadItem(uploadItem, electron);
return renderableImageBlob(file.metadata.title, blob);
@@ -146,7 +146,7 @@ export const fetchRenderableEnteFileBlob = async (
): Promise<Blob> => {
const fileType = file.metadata.fileType;
if (fileType == FileType.video) {
- const thumbnailData = await DownloadManager.getThumbnail(file);
+ const thumbnailData = await DownloadManager.thumbnailBytes(file);
return new Blob([thumbnailData!]);
}