Skip to content

armgit / ente

Public Imported from GitHub
0

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

add option to fileSize for cache size validation in cache match

Abhinav3e4dc8c127d2
diff --git a/src/services/diskCache.ts b/src/services/diskCache.ts
index 237c8a8940..a9e07a5b79 100644
--- a/src/services/diskCache.ts
+++ b/src/services/diskCache.ts
@@ -1,6 +1,6 @@
import DiskLRUService from '../services/diskLRU';
import crypto from 'crypto';
-import { existsSync, unlink, rename } from 'promise-fs';
+import { existsSync, unlink, rename, stat } from 'promise-fs';
import path from 'path';
import { LimitedCache } from '../types/cache';
import { logError } from './logging';
@@ -23,9 +23,18 @@ export class DiskCache implements LimitedCache {
);
}
- async match(cacheKey: string): Promise<Response> {
+ async match(
+ cacheKey: string,
+ { sizeInBytes }: { sizeInBytes?: number } = {}
+ ): Promise<Response> {
const cachePath = path.join(this.cacheBucketDir, cacheKey);
+
if (existsSync(cachePath)) {
+ const fileStats = await stat(cachePath);
+ if (sizeInBytes && fileStats.size !== sizeInBytes) {
+ unlink(cachePath);
+ return undefined;
+ }
DiskLRUService.touch(cachePath);
return new Response(await getFileStream(cachePath));
} else {
@@ -35,6 +44,11 @@ export class DiskCache implements LimitedCache {
cacheKey
);
if (existsSync(oldCachePath)) {
+ const fileStats = await stat(oldCachePath);
+ if (sizeInBytes && fileStats.size !== sizeInBytes) {
+ unlink(oldCachePath);
+ return undefined;
+ }
const match = new Response(await getFileStream(oldCachePath));
void migrateOldCacheKey(oldCachePath, cachePath);
return match;
diff --git a/src/types/cache.ts b/src/types/cache.ts
index a5412f0eb7..fde7dc0384 100644
--- a/src/types/cache.ts
+++ b/src/types/cache.ts
@@ -1,5 +1,8 @@
export interface LimitedCache {
- match: (key: string) => Promise<Response>;
+ match: (
+ key: string,
+ options?: { sizeInBytes?: number }
+ ) => Promise<Response>;
put: (key: string, data: Response) => Promise<void>;
delete: (key: string) => Promise<boolean>;
}