Skip to content

armgit / ente

Public Imported from GitHub
0

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

Consolidate

Manav Rathi7fa9e924ebb5
diff --git a/web/apps/photos/src/components/MachineLearning/ImageViews.tsx b/web/apps/photos/src/components/MachineLearning/ImageViews.tsx
index 72800271b1..4605cafe89 100644
--- a/web/apps/photos/src/components/MachineLearning/ImageViews.tsx
+++ b/web/apps/photos/src/components/MachineLearning/ImageViews.tsx
@@ -1,5 +1,5 @@
import log from "@/next/log";
-import { cached } from "@ente/shared/storage/cacheStorage/helpers";
+import { cached } from "@ente/shared/storage/cache";
import { LS_KEYS, getData } from "@ente/shared/storage/localStorage";
import { User } from "@ente/shared/user/types";
import { Skeleton, styled } from "@mui/material";
diff --git a/web/apps/photos/src/components/MachineLearning/PeopleList.tsx b/web/apps/photos/src/components/MachineLearning/PeopleList.tsx
index 0e358ce337..dc5f0fd435 100644
--- a/web/apps/photos/src/components/MachineLearning/PeopleList.tsx
+++ b/web/apps/photos/src/components/MachineLearning/PeopleList.tsx
@@ -1,5 +1,5 @@
import log from "@/next/log";
-import { CACHES } from "@ente/shared/storage/cacheStorage/constants";
+import { CACHES } from "@ente/shared/storage/cache";
import { styled } from "@mui/material";
import { Legend } from "components/PhotoViewer/styledComponents/Legend";
import { t } from "i18next";
diff --git a/web/apps/photos/src/services/download/index.ts b/web/apps/photos/src/services/download/index.ts
index d262c74aac..64930989ac 100644
--- a/web/apps/photos/src/services/download/index.ts
+++ b/web/apps/photos/src/services/download/index.ts
@@ -4,9 +4,11 @@ import ComlinkCryptoWorker from "@ente/shared/crypto";
import { DedicatedCryptoWorker } from "@ente/shared/crypto/internal/crypto.worker";
import { CustomError } from "@ente/shared/error";
import { Events, eventBus } from "@ente/shared/events";
-import { CacheStorageService } from "@ente/shared/storage/cacheStorage";
-import { CACHES } from "@ente/shared/storage/cacheStorage/constants";
-import { LimitedCache } from "@ente/shared/storage/cacheStorage/types";
+import {
+ CACHES,
+ CacheStorageService,
+ type LimitedCache,
+} from "@ente/shared/storage/cache";
import { Remote } from "comlink";
import { FILE_TYPE } from "constants/file";
import isElectron from "is-electron";
diff --git a/web/apps/photos/src/utils/machineLearning/faceCrop.ts b/web/apps/photos/src/utils/machineLearning/faceCrop.ts
index 5486c64488..e681443263 100644
--- a/web/apps/photos/src/utils/machineLearning/faceCrop.ts
+++ b/web/apps/photos/src/utils/machineLearning/faceCrop.ts
@@ -1,5 +1,4 @@
-import { CacheStorageService } from "@ente/shared/storage/cacheStorage";
-import { CACHES } from "@ente/shared/storage/cacheStorage/constants";
+import { CACHES, CacheStorageService } from "@ente/shared/storage/cache";
import { BlobOptions } from "types/image";
import {
FaceAlignment,
diff --git a/web/apps/photos/src/utils/machineLearning/index.ts b/web/apps/photos/src/utils/machineLearning/index.ts
index d0eacf1a6e..0157f66c1c 100644
--- a/web/apps/photos/src/utils/machineLearning/index.ts
+++ b/web/apps/photos/src/utils/machineLearning/index.ts
@@ -1,6 +1,5 @@
import log from "@/next/log";
-import { CACHES } from "@ente/shared/storage/cacheStorage/constants";
-import { cached } from "@ente/shared/storage/cacheStorage/helpers";
+import { CACHES, cached } from "@ente/shared/storage/cache";
import { FILE_TYPE } from "constants/file";
import PQueue from "p-queue";
import DownloadManager from "services/download";
diff --git a/web/packages/accounts/services/user.ts b/web/packages/accounts/services/user.ts
index 87a320e361..777709c3a3 100644
--- a/web/packages/accounts/services/user.ts
+++ b/web/packages/accounts/services/user.ts
@@ -1,7 +1,7 @@
import log from "@/next/log";
import { Events, eventBus } from "@ente/shared/events";
import InMemoryStore from "@ente/shared/storage/InMemoryStore";
-import { deleteAllCache } from "@ente/shared/storage/cacheStorage/helpers";
+import { clearCaches } from "@ente/shared/storage/cache";
import { clearFiles } from "@ente/shared/storage/localForage/helpers";
import { clearData } from "@ente/shared/storage/localStorage";
import { clearKeys } from "@ente/shared/storage/sessionStorage";
@@ -31,7 +31,7 @@ export const logoutUser = async () => {
log.error("Ignoring error when clearing data", e);
}
try {
- await deleteAllCache();
+ await clearCaches();
} catch (e) {
log.error("Ignoring error when clearing caches", e);
}
diff --git a/web/packages/shared/storage/cache.ts b/web/packages/shared/storage/cache.ts
new file mode 100644
index 0000000000..035d029c1d
--- /dev/null
+++ b/web/packages/shared/storage/cache.ts
@@ -0,0 +1,123 @@
+import log from "@/next/log";
+
+export enum CACHES {
+ THUMBS = "thumbs",
+ FACE_CROPS = "face-crops",
+ // Desktop app only
+ FILES = "files",
+}
+
+export interface LimitedCacheStorage {
+ open: (
+ cacheName: string,
+ cacheLimitInBytes?: number,
+ ) => Promise<LimitedCache>;
+ delete: (cacheName: string) => Promise<boolean>;
+}
+
+export interface LimitedCache {
+ match: (
+ key: string,
+ options?: { sizeInBytes?: number },
+ ) => Promise<Response>;
+ put: (key: string, data: Response) => Promise<void>;
+ delete: (key: string) => Promise<boolean>;
+}
+
+class cacheStorageFactory {
+ getCacheStorage(): LimitedCacheStorage {
+ return transformBrowserCacheStorageToLimitedCacheStorage(caches);
+ }
+}
+
+export const CacheStorageFactory = new cacheStorageFactory();
+
+function transformBrowserCacheStorageToLimitedCacheStorage(
+ caches: CacheStorage,
+): LimitedCacheStorage {
+ return {
+ async open(cacheName) {
+ const cache = await caches.open(cacheName);
+ return {
+ match: (key) => {
+ // options are not supported in the browser
+ return cache.match(key);
+ },
+ put: cache.put.bind(cache),
+ delete: cache.delete.bind(cache),
+ };
+ },
+ delete: caches.delete.bind(caches),
+ };
+}
+
+const SecurityError = "SecurityError";
+const INSECURE_OPERATION = "The operation is insecure.";
+
+async function openCache(cacheName: string, cacheLimit?: number) {
+ try {
+ return await CacheStorageFactory.getCacheStorage().open(
+ cacheName,
+ cacheLimit,
+ );
+ } catch (e) {
+ // ignoring insecure operation error, as it is thrown in incognito mode in firefox
+ if (e.name === SecurityError && e.message === INSECURE_OPERATION) {
+ // no-op
+ } else {
+ // log and ignore, we don't want to break the caller flow, when cache is not available
+ log.error("openCache failed", e);
+ }
+ }
+}
+async function deleteCache(cacheName: string) {
+ try {
+ return await CacheStorageFactory.getCacheStorage().delete(cacheName);
+ } catch (e) {
+ // ignoring insecure operation error, as it is thrown in incognito mode in firefox
+ if (e.name === SecurityError && e.message === INSECURE_OPERATION) {
+ // no-op
+ } else {
+ // log and ignore, we don't want to break the caller flow, when cache is not available
+ log.error("deleteCache failed", e);
+ }
+ }
+}
+
+export const CacheStorageService = { open: openCache, delete: deleteCache };
+
+export async function cached(
+ cacheName: string,
+ id: string,
+ get: () => Promise<Blob>,
+): Promise<Blob> {
+ const cache = await CacheStorageService.open(cacheName);
+ const cacheResponse = await cache.match(id);
+
+ let result: Blob;
+ if (cacheResponse) {
+ result = await cacheResponse.blob();
+ } else {
+ result = await get();
+
+ try {
+ await cache.put(id, new Response(result));
+ } catch (e) {
+ // TODO: handle storage full exception.
+ console.error("Error while storing file to cache: ", id);
+ }
+ }
+
+ return result;
+}
+
+/**
+ * Delete all cached data.
+ *
+ * Meant for use during logout, to reset the state of the user's account.
+ */
+export const clearCaches = async () => {
+ await CacheStorageService.delete(CACHES.THUMBS);
+ await CacheStorageService.delete(CACHES.FACE_CROPS);
+ await CacheStorageService.delete(CACHES.FILES);
+};
diff --git a/web/packages/shared/storage/cacheStorage/constants.ts b/web/packages/shared/storage/cacheStorage/constants.ts
deleted file mode 100644
index 4a31b78972..0000000000
--- a/web/packages/shared/storage/cacheStorage/constants.ts
+++ /dev/null
@@ -1,6 +0,0 @@
-export enum CACHES {
- THUMBS = "thumbs",
- FACE_CROPS = "face-crops",
- // Desktop app only
- FILES = "files",
-}
diff --git a/web/packages/shared/storage/cacheStorage/factory.ts b/web/packages/shared/storage/cacheStorage/factory.ts
deleted file mode 100644
index 0de07aeec0..0000000000
--- a/web/packages/shared/storage/cacheStorage/factory.ts
+++ /dev/null
@@ -1,28 +0,0 @@
-import { LimitedCacheStorage } from "./types";
-
-class cacheStorageFactory {
- getCacheStorage(): LimitedCacheStorage {
- return transformBrowserCacheStorageToLimitedCacheStorage(caches);
- }
-}
-
-export const CacheStorageFactory = new cacheStorageFactory();
-
-function transformBrowserCacheStorageToLimitedCacheStorage(
- caches: CacheStorage,
-): LimitedCacheStorage {
- return {
- async open(cacheName) {
- const cache = await caches.open(cacheName);
- return {
- match: (key) => {
- // options are not supported in the browser
- return cache.match(key);
- },
- put: cache.put.bind(cache),
- delete: cache.delete.bind(cache),
- };
- },
- delete: caches.delete.bind(caches),
- };
-}
diff --git a/web/packages/shared/storage/cacheStorage/helpers.ts b/web/packages/shared/storage/cacheStorage/helpers.ts
deleted file mode 100644
index b306d56837..0000000000
--- a/web/packages/shared/storage/cacheStorage/helpers.ts
+++ /dev/null
@@ -1,55 +0,0 @@
-import log from "@/next/log";
-import { CacheStorageService } from ".";
-import { CACHES } from "./constants";
-import { LimitedCache } from "./types";
-
-export async function cached(
- cacheName: string,
- id: string,
- get: () => Promise<Blob>,
-): Promise<Blob> {
- const cache = await CacheStorageService.open(cacheName);
- const cacheResponse = await cache.match(id);
-
- let result: Blob;
- if (cacheResponse) {
- result = await cacheResponse.blob();
- } else {
- result = await get();
-
- try {
- await cache.put(id, new Response(result));
- } catch (e) {
- // TODO: handle storage full exception.
- console.error("Error while storing file to cache: ", id);
- }
- }
-
- return result;
-}
-
-let thumbCache: LimitedCache;
-
-export async function getBlobFromCache(
- cacheName: string,
- url: string,
-): Promise<Blob> {
- if (!thumbCache) {
- thumbCache = await CacheStorageService.open(cacheName);
- }
- const response = await thumbCache.match(url);
- if (!response) {
- return undefined;
- }
- return response.blob();
-}
-
-export async function deleteAllCache() {
- try {
- await CacheStorageService.delete(CACHES.THUMBS);
- await CacheStorageService.delete(CACHES.FACE_CROPS);
- await CacheStorageService.delete(CACHES.FILES);
- } catch (e) {
- log.error("deleteAllCache failed", e); // log and ignore
- }
-}
diff --git a/web/packages/shared/storage/cacheStorage/index.ts b/web/packages/shared/storage/cacheStorage/index.ts
deleted file mode 100644
index a70bf09272..0000000000
--- a/web/packages/shared/storage/cacheStorage/index.ts
+++ /dev/null
@@ -1,36 +0,0 @@
-import log from "@/next/log";
-import { CacheStorageFactory } from "./factory";
-
-const SecurityError = "SecurityError";
-const INSECURE_OPERATION = "The operation is insecure.";
-async function openCache(cacheName: string, cacheLimit?: number) {
- try {
- return await CacheStorageFactory.getCacheStorage().open(
- cacheName,
- cacheLimit,
- );
- } catch (e) {
- // ignoring insecure operation error, as it is thrown in incognito mode in firefox
- if (e.name === SecurityError && e.message === INSECURE_OPERATION) {
- // no-op
- } else {
- // log and ignore, we don't want to break the caller flow, when cache is not available
- log.error("openCache failed", e);
- }
- }
-}
-async function deleteCache(cacheName: string) {
- try {
- return await CacheStorageFactory.getCacheStorage().delete(cacheName);
- } catch (e) {
- // ignoring insecure operation error, as it is thrown in incognito mode in firefox
- if (e.name === SecurityError && e.message === INSECURE_OPERATION) {
- // no-op
- } else {
- // log and ignore, we don't want to break the caller flow, when cache is not available
- log.error("deleteCache failed", e);
- }
- }
-}
-
-export const CacheStorageService = { open: openCache, delete: deleteCache };
diff --git a/web/packages/shared/storage/cacheStorage/types.ts b/web/packages/shared/storage/cacheStorage/types.ts
deleted file mode 100644
index d520559224..0000000000
--- a/web/packages/shared/storage/cacheStorage/types.ts
+++ /dev/null
@@ -1,16 +0,0 @@
-export interface LimitedCacheStorage {
- open: (
- cacheName: string,
- cacheLimitInBytes?: number,
- ) => Promise<LimitedCache>;
- delete: (cacheName: string) => Promise<boolean>;
-}
-
-export interface LimitedCache {
- match: (
- key: string,
- options?: { sizeInBytes?: number },
- ) => Promise<Response>;
- put: (key: string, data: Response) => Promise<void>;
- delete: (key: string) => Promise<boolean>;
-}