fallback to placeholder only after checking for cached file on disk

This commit is contained in:
Abhinav 2023-05-28 11:51:32 +05:30
parent 48d1c3e186
commit c47480ca95
3 changed files with 122 additions and 39 deletions

View file

@ -238,13 +238,34 @@ export default function PreviewCard(props: IProps) {
}, []);
useEffect(() => {
if (!file.msrc && !props.showPlaceholder) {
const main = async () => {
try {
let url;
if (imgSrc) {
return;
}
let url: string;
// check in in-memory cache
if (thumbs.has(file.id)) {
url = thumbs.get(file.id);
} else {
// check in cacheStorage
if (
publicCollectionGalleryContext.accessedThroughSharedURL
) {
url;
await PublicCollectionDownloadManager.getCachedThumbnail(
file
);
} else {
url = await DownloadManager.getCachedThumbnail(file);
}
if (url) {
thumbs.set(file.id, url);
} else {
// download thumbnail
if (props.showPlaceholder) {
return;
}
if (
publicCollectionGalleryContext.accessedThroughSharedURL
) {
@ -259,6 +280,7 @@ export default function PreviewCard(props: IProps) {
}
thumbs.set(file.id, url);
}
}
if (!isMounted.current) {
return;
}
@ -270,7 +292,6 @@ export default function PreviewCard(props: IProps) {
}
};
main();
}
}, [props.showPlaceholder]);
const handleClick = () => {

View file

@ -16,6 +16,7 @@ import { CacheStorageService } from './cache/cacheStorageService';
import { CACHES } from 'constants/cache';
import { Remote } from 'comlink';
import { DedicatedCryptoWorker } from 'worker/crypto.worker';
import { LimitedCache } from 'types/cache';
class DownloadManager {
private fileObjectURLPromise = new Map<
@ -24,6 +25,40 @@ class DownloadManager {
>();
private thumbnailObjectURLPromise = new Map<number, Promise<string>>();
private async getThumbnailCache() {
try {
const thumbnailCache = await CacheStorageService.open(
CACHES.THUMBS
);
return thumbnailCache;
} catch (e) {
return null;
// ignore
}
}
public async getCachedThumbnail(
file: EnteFile,
thumbnailCache?: LimitedCache
) {
try {
if (!thumbnailCache) {
thumbnailCache = await this.getThumbnailCache();
}
const cacheResp: Response = await thumbnailCache?.match(
file.id.toString()
);
if (cacheResp) {
return URL.createObjectURL(await cacheResp.blob());
}
return null;
} catch (e) {
logError(e, 'failed to get cached thumbnail');
throw e;
}
}
public async getThumbnail(
file: EnteFile,
tokenOverride?: string,
@ -37,15 +72,13 @@ class DownloadManager {
}
if (!this.thumbnailObjectURLPromise.has(file.id)) {
const downloadPromise = async () => {
const thumbnailCache = await CacheStorageService.open(
CACHES.THUMBS
const thumbnailCache = await this.getThumbnailCache();
const cachedThumb = await this.getCachedThumbnail(
file,
thumbnailCache
);
const cacheResp: Response = await thumbnailCache?.match(
file.id.toString()
);
if (cacheResp) {
return URL.createObjectURL(await cacheResp.blob());
if (cachedThumb) {
return cachedThumb;
}
const thumb = await this.downloadThumb(
token,

View file

@ -15,6 +15,9 @@ import { FILE_TYPE } from 'constants/file';
import { CustomError } from 'utils/error';
import QueueProcessor from './queueProcessor';
import ComlinkCryptoWorker from 'utils/comlink/ComlinkCryptoWorker';
import { CACHES } from 'constants/cache';
import { CacheStorageService } from './cache/cacheStorageService';
import { LimitedCache } from 'types/cache';
class PublicCollectionDownloadManager {
private fileObjectURLPromise = new Map<
@ -25,6 +28,40 @@ class PublicCollectionDownloadManager {
private thumbnailDownloadRequestsProcessor = new QueueProcessor<any>(5);
private async getThumbnailCache() {
try {
const thumbnailCache = await CacheStorageService.open(
CACHES.THUMBS
);
return thumbnailCache;
} catch (e) {
return null;
// ignore
}
}
public async getCachedThumbnail(
file: EnteFile,
thumbnailCache?: LimitedCache
) {
try {
if (!thumbnailCache) {
thumbnailCache = await this.getThumbnailCache();
}
const cacheResp: Response = await thumbnailCache?.match(
file.id.toString()
);
if (cacheResp) {
return URL.createObjectURL(await cacheResp.blob());
}
return null;
} catch (e) {
logError(e, 'failed to get cached thumbnail');
throw e;
}
}
public async getThumbnail(
file: EnteFile,
token: string,
@ -37,21 +74,13 @@ class PublicCollectionDownloadManager {
if (!this.thumbnailObjectURLPromise.has(file.id)) {
const downloadPromise = async () => {
const thumbnailCache = await (async () => {
try {
return await caches.open('thumbs');
} catch (e) {
return null;
// ignore
}
})();
const cacheResp: Response = await thumbnailCache?.match(
file.id.toString()
const thumbnailCache = await this.getThumbnailCache();
const cachedThumb = await this.getCachedThumbnail(
file,
thumbnailCache
);
if (cacheResp) {
return URL.createObjectURL(await cacheResp.blob());
if (cachedThumb) {
return cachedThumb;
}
const thumb =