π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Rework the original downloads
Manav Rathi41f2433cee74
diff --git a/web/apps/photos/src/components/FixCreationTime.tsx b/web/apps/photos/src/components/FixCreationTime.tsxindex 35aa71f87f..b060d33d6c 100644--- a/web/apps/photos/src/components/FixCreationTime.tsx+++ b/web/apps/photos/src/components/FixCreationTime.tsx@@ -324,8 +324,7 @@ const updateEnteFileDate = async (timestamp: customDate!.timestamp,};} else if (enteFile.metadata.fileType == FileType.image) {- const stream = await downloadManager.getFile(enteFile);- const blob = await new Response(stream).blob();+ const blob = await downloadManager.fileBlob(enteFile);const file = new File([blob], enteFile.metadata.title);const { DateTimeOriginal, DateTimeDigitized, MetadataDate, DateTime } =await extractExifDates(file);diff --git a/web/apps/photos/src/services/export/index.ts b/web/apps/photos/src/services/export/index.tsindex 5ec3dea3ba..78cec8c197 100644--- a/web/apps/photos/src/services/export/index.ts+++ b/web/apps/photos/src/services/export/index.ts@@ -928,7 +928,7 @@ class ExportService {const electron = ensureElectron();try {const fileUID = getExportRecordFileUID(file);- const originalFileStream = await downloadManager.getFile(file);+ const originalFileStream = await downloadManager.fileStream(file);if (file.metadata.fileType === FileType.livePhoto) {await this.exportLivePhoto(exportDir,diff --git a/web/apps/photos/src/services/export/migration.ts b/web/apps/photos/src/services/export/migration.tsindex 2224ac73dc..1578085566 100644--- a/web/apps/photos/src/services/export/migration.ts+++ b/web/apps/photos/src/services/export/migration.ts@@ -309,8 +309,7 @@ async function getFileExportNamesFromExportedFiles(For Live Photos we need to download the file to get the image and video name*/if (file.metadata.fileType === FileType.livePhoto) {- const fileStream = await downloadManager.getFile(file);- const fileBlob = await new Response(fileStream).blob();+ const fileBlob = await downloadManager.fileBlob(file);const { imageFileName, videoFileName } = await decodeLivePhoto(file.metadata.title,fileBlob,diff --git a/web/apps/photos/src/utils/file/index.ts b/web/apps/photos/src/utils/file/index.tsindex 12b6e6a69b..ce5740039f 100644--- a/web/apps/photos/src/utils/file/index.ts+++ b/web/apps/photos/src/utils/file/index.ts@@ -56,9 +56,7 @@ export enum FILE_OPS_TYPE {export async function downloadFile(file: EnteFile) {try {- let fileBlob = await new Response(- await DownloadManager.getFile(file),- ).blob();+ let fileBlob = await DownloadManager.fileBlob(file);if (file.metadata.fileType === FileType.livePhoto) {const { imageFileName, imageData, videoFileName, videoData } =await decodeLivePhoto(file.metadata.title, fileBlob);@@ -392,7 +390,7 @@ async function downloadFileDesktop() {const fs = electron.fs;- const stream = await DownloadManager.getFile(file);+ const stream = await DownloadManager.fileStream(file);if (file.metadata.fileType === FileType.livePhoto) {const fileBlob = await new Response(stream).blob();diff --git a/web/packages/new/photos/services/download.ts b/web/packages/new/photos/services/download.tsindex 2a8e7ba1fe..72db5eee89 100644--- a/web/packages/new/photos/services/download.ts+++ b/web/packages/new/photos/services/download.ts@@ -90,8 +90,21 @@ class DownloadManagerImpl {* 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 thumbnailURLPromises = new Map<+ number,+ Promise<string | undefined>+ >();+ /**+ * An in-memory cache for an object URL to a file's original data.+ *+ * 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.+ *+ * The entries are keyed by the file ID.+ */+ private fileURLPromises = new Map<number, Promise<string>>();private fileConversionPromises = new Map<number, Promise<SourceURLs>>();private fileDownloadProgress = new Map<number, number>();@@ -127,9 +140,9 @@ class DownloadManagerImpl {logout() {this.ready = false;this.downloadClient = undefined;- this.fileObjectURLPromises.clear();+ this.thumbnailURLPromises.clear();+ this.fileURLPromises.clear();this.fileConversionPromises.clear();- this.thumbnailURLs.clear();this.fileDownloadProgress.clear();this.progressUpdater = () => {};}@@ -143,41 +156,6 @@ class DownloadManagerImpl {this.progressUpdater = progressUpdater;}- private downloadThumb = async (file: EnteFile) => {- const { downloadClient } = this.ensureInitialized();-- const encryptedData = await downloadClient.downloadThumbnail(file);- const decryptionHeader = file.thumbnail.decryptionHeader;- return decryptThumbnail({ encryptedData, decryptionHeader }, file.key);- };-- /**- * 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 (cachedOnly) return undefined;-- const thumb = await this.downloadThumb(file);- await this.thumbnailCache?.put(key, new Blob([thumb]));- return thumb;- }-/*** Resolves with an URL that points to the file's thumbnail.*@@ -199,23 +177,58 @@ class DownloadManagerImpl {): Promise<string | undefined> {this.ensureInitialized();- if (!this.thumbnailURLs.has(file.id)) {+ if (!this.thumbnailURLPromises.has(file.id)) {const url = this.thumbnailBytes(file, cachedOnly).then((bytes) =>bytes ? URL.createObjectURL(new Blob([bytes])) : undefined,);- this.thumbnailURLs.set(file.id, url);+ this.thumbnailURLPromises.set(file.id, url);}- let thumb = await this.thumbnailURLs.get(file.id);+ let thumb = await this.thumbnailURLPromises.get(file.id);if (cachedOnly) return thumb;if (!thumb) {- this.thumbnailURLs.delete(file.id);+ this.thumbnailURLPromises.delete(file.id);thumb = await this.renderableThumbnailURL(file);}return thumb;}+ /**+ * 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 (cachedOnly) return undefined;++ const thumb = await this.downloadThumb(file);+ await this.thumbnailCache?.put(key, new Blob([thumb]));+ return thumb;+ }++ private downloadThumb = async (file: EnteFile) => {+ const { downloadClient } = this.ensureInitialized();++ const encryptedData = await downloadClient.downloadThumbnail(file);+ const decryptionHeader = file.thumbnail.decryptionHeader;+ return decryptThumbnail({ encryptedData, decryptionHeader }, file.key);+ };+/*** The `forceConvert` option is true when the user presses the "Convert"* button. See: [Note: Forcing conversion of playable videos].@@ -228,19 +241,14 @@ class DownloadManagerImpl {try {const forceConvert = opts?.forceConvert ?? false;const getFileForPreviewPromise = async () => {- const fileBlob = await new Response(- await this.getFile(file, true),- ).blob();- // TODO: Is this ensure valid?- // The existing code was already dereferencing, so it shouldn't- // affect behaviour.- const { url: originalFileURL } =- (await this.fileObjectURLPromises.get(file.id))!;-+ const originalFileURL =+ await this.fileURLDownloadAndCacheIfNeeded(file);+ const res = await fetch(originalFileURL!);+ const fileBlob = await res.blob();const converted = await getRenderableFileURL(file,fileBlob,- originalFileURL as string,+ originalFileURL!,forceConvert,);return converted;@@ -260,43 +268,73 @@ class DownloadManagerImpl {}};- async getFile(+ /**+ * Return a blob to the file's contents, downloading it needed.+ *+ * This is a convenience abstraction over {@link fileStream} that converts+ * it into a {@link Blob}.+ */+ async fileBlob(file: EnteFile) {+ return this.fileStream(file).then((s) => new Response(s).blob());+ }++ /**+ * Return an stream to the file's contents, downloading it needed.+ *+ * Note that the results are not cached in-memory. That is, while the+ * request may be served from the existing item in the in-memory cache, if+ * it is not found and a download is required, that result will not be+ * cached for subsequent use.+ *+ * @param file The {@link EnteFile} whose data we want.+ *+ * @returns+ */+ async fileStream(file: EnteFile,- cacheInMemory = false,): Promise<ReadableStream<Uint8Array> | null> {this.ensureInitialized();- try {- const getFilePromise = async (): Promise<SourceURLs> => {- const fileStream = await this.downloadFile(file);- const fileBlob = await new Response(fileStream).blob();- return {- url: URL.createObjectURL(fileBlob),- isOriginal: true,- isRenderable: false,- type: "normal",- };- };- if (!this.fileObjectURLPromises.has(file.id)) {- if (!cacheInMemory) {- return await this.downloadFile(file);- }- this.fileObjectURLPromises.set(file.id, getFilePromise());- }- // TODO: Is this ensure valid?- // The existing code was already dereferencing, so it shouldn't- // affect behaviour.- const fileURLs = (await this.fileObjectURLPromises.get(file.id))!;- if (fileURLs.isOriginal) {- const fileStream = (await fetch(fileURLs.url as string)).body;- return fileStream;- } else {- return await this.downloadFile(file);++ const cached = await this.fileStreamFromCachedURL(file);+ if (cached) return cached;++ return this.downloadFile(file);+ }++ private async fileStreamFromCachedURL(file: EnteFile) {+ const cachedURL = this.fileURLPromises.get(file.id);+ if (cachedURL) {+ try {+ const url = await cachedURL;+ const res = await fetch(url);+ return res.body;+ } catch (e) {+ log.warn("Failed to use cached object URL", e);+ this.fileURLPromises.delete(file.id);+ throw e;}- } catch (e) {- this.fileObjectURLPromises.delete(file.id);- log.error("download manager getFile Failed", e);- throw e;}+ return null;+ }++ /**+ * A private variant of {@link fileStream} that also caches the results.+ */+ private async fileURLDownloadAndCacheIfNeeded(file: EnteFile) {+ this.ensureInitialized();++ if (!this.fileURLPromises.has(file.id)) {+ this.fileURLPromises.set(+ file.id,+ (async () => {+ const fileStream = await this.downloadFile(file);+ const fileBlob = await new Response(fileStream).blob();+ return URL.createObjectURL(fileBlob);+ })(),+ );+ }++ return this.fileURLPromises.get(file.id);}private async downloadFile(diff --git a/web/packages/new/photos/services/ml/blob.ts b/web/packages/new/photos/services/ml/blob.tsindex e296dce73a..aa7221982d 100644--- a/web/packages/new/photos/services/ml/blob.ts+++ b/web/packages/new/photos/services/ml/blob.ts@@ -150,20 +150,16 @@ export const fetchRenderableEnteFileBlob = async (return new Blob([thumbnailData!]);}- const fileStream = await DownloadManager.getFile(file);- const originalImageBlob = await new Response(fileStream).blob();+ const originalFileBlob = await DownloadManager.fileBlob(file);if (fileType == FileType.livePhoto) {const { imageFileName, imageData } = await decodeLivePhoto(file.metadata.title,- originalImageBlob,+ originalFileBlob,);return renderableImageBlob(imageFileName, new Blob([imageData]));} else if (fileType == FileType.image) {- return await renderableImageBlob(- file.metadata.title,- originalImageBlob,- );+ return await renderableImageBlob(file.metadata.title, originalFileBlob);} else {// A layer above us should've already filtered these out.throw new Error(`Cannot index unsupported file type ${fileType}`);