Blobs it is

This commit is contained in:
Manav Rathi 2024-04-13 08:04:43 +05:30
parent acedbdbe7d
commit 6d1f8b4728
No known key found for this signature in database
6 changed files with 25 additions and 23 deletions

View file

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

View file

@ -1,4 +1,4 @@
import { openCache, type EnteCache } from "@/next/cache";
import { openCache, type BlobCache } from "@/next/blob-cache";
import log from "@/next/log";
import { APPS } from "@ente/shared/apps/constants";
import ComlinkCryptoWorker from "@ente/shared/crypto";
@ -55,13 +55,13 @@ class DownloadManagerImpl {
private ready: boolean = false;
private downloadClient: DownloadClient;
/** Local cache for thumbnails. Might not be available. */
private thumbnailCache?: EnteCache;
private thumbnailCache?: BlobCache;
/**
* Local cache for the files themselves.
*
* Only available when we're running in the desktop app.
*/
private fileCache?: EnteCache;
private fileCache?: BlobCache;
private cryptoWorker: Remote<DedicatedCryptoWorker>;
private fileObjectURLPromises = new Map<number, Promise<SourceURLs>>();

View file

@ -1,4 +1,4 @@
import { openCache } from "@/next/cache";
import { openCache } from "@/next/blob-cache";
import { BlobOptions } from "types/image";
import {
FaceAlignment,

View file

@ -1,4 +1,4 @@
import { cached } from "@/next/cache";
import { cached } from "@/next/blob-cache";
import log from "@/next/log";
import { FILE_TYPE } from "constants/file";
import PQueue from "p-queue";

View file

@ -1,4 +1,4 @@
import { clearCaches } from "@/next/cache";
import { clearCaches } from "@/next/blob-cache";
import log from "@/next/log";
import { Events, eventBus } from "@ente/shared/events";
import InMemoryStore from "@ente/shared/storage/InMemoryStore";

View file

@ -1,6 +1,6 @@
import isElectron from "is-electron";
const cacheNames = [
const blobCacheNames = [
"thumbs",
"face-crops",
// Desktop app only
@ -8,15 +8,15 @@ const cacheNames = [
] as const;
/**
* Namespaces into which our caches data is divided
* Namespaces into which our blob caches are divided
*
* Note that namespaces are just arbitrary (but predefined) strings to split the
* cached data into "folders", so to speak.
* */
export type CacheName = (typeof cacheNames)[number];
export type BlobCacheNamespace = (typeof blobCacheNames)[number];
/**
* A namespaced cache.
* A namespaced blob cache.
*
* This cache is suitable for storing large amounts of data (entire files).
*
@ -53,11 +53,11 @@ export type CacheName = (typeof cacheNames)[number];
*
* See also: [Note: Increased disk cache for the desktop app].
*/
export interface EnteCache {
export interface BlobCache {
/**
* Get the data corresponding to {@link key} (if found) from the cache.
*/
get: (key: string) => Promise<Uint8Array | undefined>;
get: (key: string) => Promise<Blob | undefined>;
match: (key: string) => Promise<Response | undefined>;
/**
* Add the given {@link key}-value ({@link data}) pair to the cache.
@ -73,13 +73,15 @@ export interface EnteCache {
}
/**
* Return the {@link EnteCache} corresponding to the given {@link name}.
* Return the {@link BlobCache} corresponding to the given {@link name}.
*
* @param name One of the arbitrary but predefined namespaces of type
* {@link CacheName} which group related data and allow us to use the same key
* across namespaces.
* {@link BlobCacheNamespace} which group related data and allow us to use the
* same key across namespaces.
*/
export const openCache = async (name: CacheName): Promise<EnteCache> =>
export const openCache = async (
name: BlobCacheNamespace,
): Promise<BlobCache> =>
isElectron() ? openOPFSCacheWeb(name) : openWebCache(name);
/**
@ -111,8 +113,8 @@ export const openCache = async (name: CacheName): Promise<EnteCache> =>
* - https://stackoverflow.com/questions/11821096/what-is-the-difference-between-an-arraybuffer-and-a-blob
*/
/** An implementation of {@link EnteCache} using Web Cache APIs */
const openWebCache = async (name: CacheName) => {
/** An implementation of {@link BlobCache} using Web Cache APIs */
const openWebCache = async (name: BlobCacheNamespace) => {
const cache = await caches.open(name);
return {
get: async (key: string) => {
@ -132,8 +134,8 @@ const openWebCache = async (name: CacheName) => {
};
};
/** An implementation of {@link EnteCache} using OPFS */
const openOPFSCacheWeb = async (name: CacheName) => {
/** An implementation of {@link BlobCache} using OPFS */
const openOPFSCacheWeb = async (name: BlobCacheNamespace) => {
// While all major browsers support OPFS now, their implementations still
// have various quirks. However, we don't need to handle all possible cases
// and can just instead use the APIs and guarantees Chromium provides since
@ -186,7 +188,7 @@ const openOPFSCacheWeb = async (name: CacheName) => {
};
export async function cached(
cacheName: CacheName,
cacheName: BlobCacheNamespace,
id: string,
get: () => Promise<Blob>,
): Promise<Blob> {
@ -219,7 +221,7 @@ export const clearCaches = async () =>
isElectron() ? clearOPFSCaches() : clearWebCaches();
export const clearWebCaches = async () => {
await Promise.all(cacheNames.map((name) => caches.delete(name)));
await Promise.all(blobCacheNames.map((name) => caches.delete(name)));
};
export const clearOPFSCaches = async () => {