This commit is contained in:
Manav Rathi 2024-04-13 09:41:35 +05:30
parent 997c74d064
commit 5082124aa5
No known key found for this signature in database
4 changed files with 14 additions and 47 deletions

View file

@ -1,3 +1,4 @@
import { cachedOrNew } from "@/next/blob-cache";
import { ensureLocalUser } from "@/next/local-user";
import log from "@/next/log";
import { Skeleton, styled } from "@mui/material";

View file

@ -135,21 +135,6 @@ class DownloadManagerImpl {
this.progressUpdater = progressUpdater;
}
private async getCachedFile(file: EnteFile): Promise<Response> {
const fileCache = this.fileCache;
if (!fileCache) return null;
try {
const cacheResp: Response = await fileCache.match(
file.id.toString(),
);
return cacheResp?.clone();
} catch (e) {
log.error("failed to get cached file", e);
throw e;
}
}
private downloadThumb = async (file: EnteFile) => {
const encrypted = await this.downloadClient.downloadThumbnail(file);
const decrypted = await this.cryptoWorker.decryptThumbnail(
@ -169,7 +154,7 @@ class DownloadManagerImpl {
if (localOnly) return null;
const thumb = await this.downloadThumb(file);
this.thumbnailCache?.put2(key, new Blob([thumb]));
this.thumbnailCache?.put(key, new Blob([thumb]));
return thumb;
}
@ -292,10 +277,7 @@ class DownloadManagerImpl {
onDownloadProgress,
);
encryptedArrayBuffer = array.buffer;
this.fileCache?.put2(
cacheKey,
new Blob([encryptedArrayBuffer]),
);
this.fileCache?.put(cacheKey, new Blob([encryptedArrayBuffer]));
}
this.clearDownloadProgress(file.id);
try {
@ -321,7 +303,7 @@ class DownloadManagerImpl {
if (cachedBlob) res = new Response(cachedBlob);
else {
res = await this.downloadClient.downloadFileStream(file);
this?.fileCache.put2(cacheKey, await res.blob());
this?.fileCache.put(cacheKey, await res.blob());
}
const reader = res.body.getReader();

View file

@ -53,9 +53,8 @@ async function storeFaceCropForBlob(
faceCropBlob: Blob,
) {
const faceCropUrl = `/${faceId}`;
const faceCropResponse = new Response(faceCropBlob);
const faceCropCache = await openCache("face-crops");
await faceCropCache.put(faceCropUrl, faceCropResponse);
await faceCropCache.put(faceCropUrl, faceCropBlob);
return {
imageUrl: faceCropUrl,
imageBox: imageBox,

View file

@ -58,12 +58,10 @@ export interface BlobCache {
* Get the data corresponding to {@link key} (if found) from the cache.
*/
get: (key: string) => Promise<Blob | undefined>;
match: (key: string) => Promise<Response | undefined>;
/**
* Add the given {@link key}-value ({@link blob}) pair to the cache.
*/
put2: (key: string, blob: Blob) => Promise<void>;
put: (key: string, data: Response) => Promise<void>;
put: (key: string, blob: Blob) => Promise<void>;
/**
* Delete the blob corresponding to the given {@link key}.
*
@ -133,13 +131,7 @@ const openWebCache = async (name: BlobCacheNamespace) => {
console.log("found cache hit", key, res);
return await res?.blob();
},
match: (key: string) => {
return cache.match(key);
},
put2: (key: string, blob: Blob) => cache.put(key, new Response(blob)),
put: (key: string, data: Response) => {
return cache.put(key, data);
},
put: (key: string, blob: Blob) => cache.put(key, new Response(blob)),
delete: (key: string) => cache.delete(key),
};
};
@ -154,17 +146,16 @@ const openOPFSCacheWeb = async (name: BlobCacheNamespace) => {
//
// So for our purpose, these can serve as the doc for what's available:
// https://web.dev/articles/origin-private-file-system
const cache = await caches.open(name);
const root = await navigator.storage.getDirectory();
const _caches = await root.getDirectoryHandle("cache", { create: true });
const caches = await root.getDirectoryHandle("cache", { create: true });
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const _cache = await _caches.getDirectoryHandle(name, { create: true });
const cache = await caches.getDirectoryHandle(name, { create: true });
return {
get: async (key: string) => {
try {
const fileHandle = await _cache.getFileHandle(key);
const fileHandle = await cache.getFileHandle(key);
return await fileHandle.getFile();
} catch (e) {
if (e instanceof DOMException && e.name == "NotFoundError")
@ -172,23 +163,17 @@ const openOPFSCacheWeb = async (name: BlobCacheNamespace) => {
throw e;
}
},
match: (key: string) => {
return cache.match(key);
},
put2: async (key: string, blob: Blob) => {
const fileHandle = await _cache.getFileHandle(key, {
put: async (key: string, blob: Blob) => {
const fileHandle = await cache.getFileHandle(key, {
create: true,
});
const writable = await fileHandle.createWritable();
await writable.write(blob);
await writable.close();
},
put: async (key: string, data: Response) => {
await cache.put(key, data);
},
delete: async (key: string) => {
try {
await _cache.removeEntry(key);
await cache.removeEntry(key);
return true;
} catch (e) {
if (e instanceof DOMException && e.name == "NotFoundError")
@ -214,7 +199,7 @@ export const cachedOrNew = async (
if (cachedBlob) return cachedBlob;
const blob = await get();
await cache.put2(key, blob);
await cache.put(key, blob);
return blob;
};