π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Attach validity
Manav Rathi55345cf60ec6
diff --git a/web/packages/gallery/services/download.ts b/web/packages/gallery/services/download.tsindex cde94103c3..e0f8c2d968 100644--- a/web/packages/gallery/services/download.ts+++ b/web/packages/gallery/services/download.ts@@ -93,6 +93,25 @@ export type RenderableSourceURLs =videoURL: () => Promise<string>;};+/**+ * A URL that can be used to download a file, along a validity of the said URL.+ *+ * Used by {@link fileURLPromisesWithValidity}.+ */+interface FileURLPromiseWithValidity {+ /**+ * A promise for a presigned URL that can be used to download the file.+ */+ url: Promise<string>;+ /**+ * Date until the URL is expected to be valid.+ *+ * We undercount a bit to be on the safer side, so this is a conservative+ * estimate not an exact value.+ */+ validTill: Date;+}+/*** A class that tracks the state of in-progress downloads and conversions,* including caching them for subsequent retrieval if appropriate.@@ -133,17 +152,21 @@ class DownloadManager {* unsupported format). If a renderable URL is needed for the file,* {@link renderableSourceURLs} should be used instead.*- * The entries are keyed by the file ID.+ * The entries are keyed by the file ID, and include a timestamp that can be+ * used to discard potentially stale ones.*/- private fileURLPromises = new Map<number, Promise<string>>();+ private fileURLPromisesWithValidity = new Map<+ number,+ FileURLPromiseWithValidity+ >();/*** An in-memory cache for {@link RenderableSourceURLs} for a file.** These are saved as a result of invocation of* {@link renderableSourceURLs}, 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.+ * {@link fileURLWithValidityPromises}, 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.*/@@ -187,7 +210,7 @@ class DownloadManager {logout() {this.publicAlbumsCredentials = undefined;this.thumbnailURLPromises.clear();- this.fileURLPromises.clear();+ this.fileURLPromisesWithValidity.clear();this.renderableSourceURLPromises.clear();this.fileDownloadProgress.clear();this.fileDownloadProgressListeners = [];@@ -360,15 +383,15 @@ class DownloadManager {async fileStream(file: EnteFile,): Promise<ReadableStream<Uint8Array> | null> {- const cachedURL = this.fileURLPromises.get(file.id);- if (cachedURL) {+ const cached = this.fileURLPromisesWithValidity.get(file.id);+ if (cached && cached.validTill > new Date()) {try {- const url = await cachedURL;+ const url = await cached.url;const res = await fetch(url);return res.body;} catch (e) {log.warn("Failed to use cached object URL", e);- this.fileURLPromises.delete(file.id);+ this.fileURLPromisesWithValidity.delete(file.id);}}@@ -379,18 +402,21 @@ class DownloadManager {* A private variant of {@link fileStream} that also caches the results.*/private async fileURLDownloadAndCacheIfNeeded(file: EnteFile) {- const cachedURL = this.fileURLPromises.get(file.id);- if (cachedURL) return cachedURL;+ const cached = this.fileURLPromisesWithValidity.get(file.id);+ if (cached && cached.validTill > new Date()) {+ return cached.url;+ }const url = this.downloadFile(file).then((stream) => new Response(stream).blob()).then((blob) => URL.createObjectURL(blob));- this.fileURLPromises.set(file.id, url);+ const validTill = createFileURLValidity();+ this.fileURLPromisesWithValidity.set(file.id, { url, validTill });try {return await url;} catch (e) {- this.fileURLPromises.delete(file.id);+ this.fileURLPromisesWithValidity.delete(file.id);throw e;}}@@ -581,6 +607,16 @@ export class NetworkDownloadError extends Error {export const isNetworkDownloadError = (e: unknown) =>e instanceof NetworkDownloadError;+/**+ * Return a new validity for a file download URL obtained from remote.+ *+ * Remote generates presigned URLs with a validity of 7 days (see+ * `PreSignedRequestValidityDuration` in the museum source). As a conservative+ * estimate, we only cache it for 5 days henceforth, for a 2 day margin.+ */+const createFileURLValidity = () =>+ new Date(Date.now() + 5 * 24 * 60 * 60 * 1000); /* 5 days */+/*** A helper function to convert all rejections of the given promise {@link op}* into {@link NetworkDownloadError}s.