add cache service
This commit is contained in:
parent
8bb63f0780
commit
8c21a1f833
31
src/services/cacheService.ts
Normal file
31
src/services/cacheService.ts
Normal file
|
@ -0,0 +1,31 @@
|
|||
import electronService from './electron/common';
|
||||
import electronCacheService from './electron/cache';
|
||||
import { logError } from 'utils/sentry';
|
||||
|
||||
const THUMB_CACHE = 'thumbs';
|
||||
|
||||
export function getCacheProvider() {
|
||||
if (electronService.checkIsBundledApp()) {
|
||||
return electronCacheService;
|
||||
} else {
|
||||
return caches;
|
||||
}
|
||||
}
|
||||
|
||||
export async function openThumbnailCache() {
|
||||
try {
|
||||
return await getCacheProvider().open(THUMB_CACHE);
|
||||
} catch (e) {
|
||||
logError(e, 'openThumbnailCache failed');
|
||||
// log and ignore
|
||||
}
|
||||
}
|
||||
|
||||
export async function deleteThumbnailCache() {
|
||||
try {
|
||||
return await getCacheProvider().delete(THUMB_CACHE);
|
||||
} catch (e) {
|
||||
logError(e, 'deleteThumbnailCache failed');
|
||||
// dont throw
|
||||
}
|
||||
}
|
|
@ -12,10 +12,7 @@ import { EnteFile } from 'types/file';
|
|||
import { logError } from 'utils/sentry';
|
||||
import { FILE_TYPE } from 'constants/file';
|
||||
import { CustomError } from 'utils/error';
|
||||
import electronService from 'services/electron/common';
|
||||
import electronCacheService from 'services/electron/cache';
|
||||
|
||||
const THUMB_CACHE = 'thumbs';
|
||||
import { openThumbnailCache } from './cacheService';
|
||||
|
||||
class DownloadManager {
|
||||
private fileObjectURLPromise = new Map<string, Promise<string[]>>();
|
||||
|
@ -29,19 +26,7 @@ class DownloadManager {
|
|||
}
|
||||
if (!this.thumbnailObjectURLPromise.get(file.id)) {
|
||||
const downloadPromise = async () => {
|
||||
const thumbnailCache = await (async () => {
|
||||
try {
|
||||
if (electronService.checkIsBundledApp()) {
|
||||
return await electronCacheService.open(
|
||||
THUMB_CACHE
|
||||
);
|
||||
} else {
|
||||
return await caches.open(THUMB_CACHE);
|
||||
}
|
||||
} catch (e) {
|
||||
logError(e, 'cache open failed');
|
||||
}
|
||||
})();
|
||||
const thumbnailCache = await openThumbnailCache();
|
||||
|
||||
const cacheResp: Response = await thumbnailCache?.match(
|
||||
file.id.toString()
|
||||
|
|
|
@ -21,6 +21,7 @@ import { getLocalFamilyData, isPartOfFamily } from 'utils/billing';
|
|||
import { ServerErrorCodes } from 'utils/error';
|
||||
import isElectron from 'is-electron';
|
||||
import safeStorageService from './electron/safeStorage';
|
||||
import { deleteThumbnailCache } from './cacheService';
|
||||
|
||||
const ENDPOINT = getEndpoint();
|
||||
|
||||
|
@ -118,7 +119,7 @@ export const logoutUser = async () => {
|
|||
clearKeys();
|
||||
clearData();
|
||||
try {
|
||||
await caches.delete('thumbs');
|
||||
await deleteThumbnailCache();
|
||||
} catch (e) {
|
||||
// ignore
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue