π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
add disk cache limit config and remove cacheKey hashing
Abhinavee9b70db574a
diff --git a/src/api/cache.ts b/src/api/cache.tsindex 3424331cf3..18f6edbfea 100644--- a/src/api/cache.ts+++ b/src/api/cache.ts@@ -16,12 +16,12 @@ const getCacheBucketDir = async (cacheName: string) => {return cacheBucketDir;};-export async function openDiskCache(cacheName: string) {+export async function openDiskCache(cacheName: string, cacheLimit?: number) {const cacheBucketDir = await getCacheBucketDir(cacheName);if (!existsSync(cacheBucketDir)) {await mkdir(cacheBucketDir, { recursive: true });}- return new DiskCache(cacheBucketDir);+ return new DiskCache(cacheBucketDir, cacheLimit);}export async function deleteDiskCache(cacheName: string) {diff --git a/src/services/diskCache.ts b/src/services/diskCache.tsindex 59a7085cf2..cf0093e9a9 100644--- a/src/services/diskCache.ts+++ b/src/services/diskCache.ts@@ -3,35 +3,49 @@ import crypto from 'crypto';import { existsSync, readFile, writeFile, unlink } from 'promise-fs';import path from 'path';import { LimitedCache } from '../types/cache';+import { logError } from './logging';-const MAX_CACHE_SIZE = 1000 * 1000 * 1000; // 1GB+const DEFAULT_CACHE_LIMIT = 1000 * 1000 * 1000; // 1GBexport class DiskCache implements LimitedCache {- constructor(private cacheBucketDir: string) {}+ constructor(+ private cacheBucketDir: string,+ private cacheLimit = DEFAULT_CACHE_LIMIT+ ) {}async put(cacheKey: string, response: Response): Promise<void> {- const cachePath = getAssetCachePath(this.cacheBucketDir, cacheKey);+ const cachePath = path.join(this.cacheBucketDir, cacheKey);await writeFile(cachePath,new Uint8Array(await response.arrayBuffer()));DiskLRUService.enforceCacheSizeLimit(this.cacheBucketDir,- MAX_CACHE_SIZE+ this.cacheLimit);}async match(cacheKey: string): Promise<Response> {- const cachePath = getAssetCachePath(this.cacheBucketDir, cacheKey);+ const cachePath = path.join(this.cacheBucketDir, cacheKey);if (existsSync(cachePath)) {DiskLRUService.touch(cachePath);return new Response(await readFile(cachePath));} else {+ // add fallback for old cache keys+ const oldCachePath = getAssetCachePath(+ this.cacheBucketDir,+ cacheKey+ );+ if (existsSync(cachePath)) {+ const match = new Response(await readFile(oldCachePath));+ void migrateOldCacheKey(oldCachePath, cachePath, match);+ return match;+ }return undefined;}}async delete(cacheKey: string): Promise<boolean> {- const cachePath = getAssetCachePath(this.cacheBucketDir, cacheKey);+ const cachePath = path.join(this.cacheBucketDir, cacheKey);if (existsSync(cachePath)) {await unlink(cachePath);return true;@@ -40,6 +54,7 @@ export class DiskCache implements LimitedCache {}}}+function getAssetCachePath(cacheDir: string, cacheKey: string) {// hashing the key to prevent illegal filenamesconst cacheKeyHash = crypto@@ -48,3 +63,16 @@ function getAssetCachePath(cacheDir: string, cacheKey: string) {.digest('hex');return path.join(cacheDir, cacheKeyHash);}++async function migrateOldCacheKey(+ oldCacheKey: string,+ newCacheKey: string,+ value: Response+) {+ try {+ await this.put(newCacheKey, value);+ await this.delete(oldCacheKey);+ } catch (e) {+ logError(e, 'Failed to move cache key to new cache key');+ }+}