do cache size validation on the node layer
This commit is contained in:
parent
173bee6001
commit
35566c9785
|
@ -1,7 +1,6 @@
|
|||
import {
|
||||
generateStreamFromArrayBuffer,
|
||||
getRenderableFileURL,
|
||||
getStreamLength,
|
||||
} from 'utils/file';
|
||||
import { EnteFile } from 'types/file';
|
||||
|
||||
|
@ -136,25 +135,12 @@ class DownloadManagerImpl {
|
|||
return null;
|
||||
}
|
||||
const cacheResp: Response = await this.diskFileCache?.match(
|
||||
file.id.toString()
|
||||
file.id.toString(),
|
||||
{ sizeInBytes: file.info?.fileSize }
|
||||
);
|
||||
if (!cacheResp) {
|
||||
return null;
|
||||
}
|
||||
// check if cached file size is same as file size
|
||||
const fileSize = file.info?.fileSize;
|
||||
if (!fileSize) {
|
||||
return cacheResp;
|
||||
}
|
||||
const contentLength = await getStreamLength(cacheResp.clone().body);
|
||||
if (file.info?.fileSize === contentLength) {
|
||||
return cacheResp;
|
||||
} else {
|
||||
addLogLine(
|
||||
`mismatch in file size, delete the cache, {actualSize: ${contentLength}, expectedSize: ${fileSize}`
|
||||
);
|
||||
this.diskFileCache?.delete(file.id.toString());
|
||||
}
|
||||
} catch (e) {
|
||||
logError(e, 'failed to get cached thumbnail');
|
||||
throw e;
|
||||
|
|
|
@ -958,18 +958,3 @@ const getFileObjectURL = (
|
|||
: null;
|
||||
return convertedURL;
|
||||
};
|
||||
|
||||
export const getStreamLength = async (stream: ReadableStream<Uint8Array>) => {
|
||||
const reader = stream.getReader();
|
||||
let length = 0;
|
||||
|
||||
// eslint-disable-next-line no-constant-condition
|
||||
while (true) {
|
||||
const { done, value } = await reader.read();
|
||||
if (done) {
|
||||
break;
|
||||
}
|
||||
length += value.length;
|
||||
}
|
||||
return length;
|
||||
};
|
||||
|
|
|
@ -73,8 +73,8 @@ export const WorkerSafeElectronService = new WorkerSafeElectronServiceImpl();
|
|||
function transformMatch(
|
||||
fn: ProxiedWorkerLimitedCache['match']
|
||||
): LimitedCache['match'] {
|
||||
return async (key: string) => {
|
||||
return deserializeToResponse(await fn(key));
|
||||
return async (key: string, options) => {
|
||||
return deserializeToResponse(await fn(key, options));
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
@ -16,7 +16,10 @@ export interface ProxiedLimitedElectronAPIs {
|
|||
) => Promise<Uint8Array>;
|
||||
}
|
||||
export interface ProxiedWorkerLimitedCache {
|
||||
match: (key: string) => Promise<ArrayBuffer>;
|
||||
match: (
|
||||
key: string,
|
||||
options?: { sizeInBytes?: number }
|
||||
) => Promise<ArrayBuffer>;
|
||||
put: (key: string, data: ArrayBuffer) => Promise<void>;
|
||||
delete: (key: string) => Promise<boolean>;
|
||||
}
|
||||
|
@ -53,8 +56,8 @@ export class WorkerSafeElectronClient implements ProxiedLimitedElectronAPIs {
|
|||
function transformMatch(
|
||||
fn: LimitedCache['match']
|
||||
): ProxiedWorkerLimitedCache['match'] {
|
||||
return async (key: string) => {
|
||||
return serializeResponse(await fn(key));
|
||||
return async (key: string, options: { sizeInBytes?: number }) => {
|
||||
return serializeResponse(await fn(key, options));
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
@ -7,7 +7,10 @@ export interface LimitedCacheStorage {
|
|||
}
|
||||
|
||||
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>;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue