Skip to content

armgit / ente

Public Imported from GitHub
0

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

Add staleness check

Manav Rathi4db1a39de2de
diff --git a/web/packages/gallery/components/viewer/data-source.ts b/web/packages/gallery/components/viewer/data-source.ts
index fef3db6ec6..52fff9ff02 100644
--- a/web/packages/gallery/components/viewer/data-source.ts
+++ b/web/packages/gallery/components/viewer/data-source.ts
@@ -171,6 +171,11 @@ class FileViewerDataSourceState {
* The best data we have for a particular file (ID).
*/
itemDataByFileID = new Map<number, ItemData>();
+ /**
+ * The validity (if applicable) for the corresponding entry in
+ * {@link itemDataByFileID}.
+ */
+ itemDataValidTillByFileID = new Map<number, Date>();
/**
* The latest callback registered for notifications of better data being
* available for a particular file (ID).
@@ -303,6 +308,13 @@ export const itemDataForFile = (file: EnteFile, needsRefresh: () => void) => {
const fileID = file.id;
const fileType = file.metadata.fileType;
+ const validTill = _state.itemDataValidTillByFileID.get(fileID);
+ if (validTill && validTill < new Date()) {
+ // Don't use the cached entry if it has become stale.
+ _state.itemDataByFileID.delete(fileID);
+ _state.itemDataValidTillByFileID.delete(fileID);
+ }
+
let itemData = _state.itemDataByFileID.get(fileID);
// We assume that there is only one file viewer that is using us at a given
@@ -312,6 +324,7 @@ export const itemDataForFile = (file: EnteFile, needsRefresh: () => void) => {
if (!itemData) {
itemData = { fileID, fileType, isContentLoading: true };
_state.itemDataByFileID.set(file.id, itemData);
+ _state.itemDataValidTillByFileID.delete(fileID);
void enqueueUpdates(file);
}
@@ -327,6 +340,7 @@ export const itemDataForFile = (file: EnteFile, needsRefresh: () => void) => {
export const forgetFailedItemDataForFileID = (fileID: number) => {
if (_state.itemDataByFileID.get(fileID)?.fetchFailed) {
_state.itemDataByFileID.delete(fileID);
+ _state.itemDataValidTillByFileID.delete(fileID);
}
};
@@ -356,7 +370,7 @@ const enqueueUpdates = async (file: EnteFile) => {
const fileID = file.id;
const fileType = file.metadata.fileType;
- const update = (itemData: Partial<ItemData>) => {
+ const update = (itemData: Partial<ItemData>, validTill?: Date) => {
// Use the file's caption as its alt text (in addition to using it as
// the visible caption).
const alt = fileCaption(file);
@@ -367,6 +381,11 @@ const enqueueUpdates = async (file: EnteFile) => {
fileID,
alt,
});
+ if (validTill) {
+ _state.itemDataValidTillByFileID.set(file.id, validTill);
+ } else {
+ _state.itemDataValidTillByFileID.delete(file.id);
+ }
_state.needsRefreshByFileID.get(file.id)?.();
};
@@ -416,7 +435,10 @@ const enqueueUpdates = async (file: EnteFile) => {
width,
height,
} = playlistData;
- update({ videoPlaylistURL, width, height });
+ update(
+ { videoPlaylistURL, width, height },
+ createHLSPlaylistItemDataValidity(),
+ );
return;
}
}
@@ -514,6 +536,18 @@ const withDimensionsIfPossible = (
image.src = imageURL;
});
+/**
+ * Return a new validity for a HLS playlist containing presigned URLs.
+ *
+ * The content chunks in HLS playlist generated by
+ * {@link hlsPlaylistDataForFile} use presigned URLs generated by remote (see
+ * `PreSignedRequestValidityDuration` in the museum source). These have a
+ * validity of 7 days. We keep a 2 day buffer, and consider any item data that
+ * uses such playlist as stale after 5 days.
+ */
+const createHLSPlaylistItemDataValidity = () =>
+ new Date(Date.now() + 5 * 24 * 60 * 60 * 1000); /* 5 days */
+
/**
* Return the cached Exif data for the given {@link file}.
*