do cache size validation on the node layer
This commit is contained in:
parent
173bee6001
commit
35566c9785
|
@ -1,7 +1,6 @@
|
||||||
import {
|
import {
|
||||||
generateStreamFromArrayBuffer,
|
generateStreamFromArrayBuffer,
|
||||||
getRenderableFileURL,
|
getRenderableFileURL,
|
||||||
getStreamLength,
|
|
||||||
} from 'utils/file';
|
} from 'utils/file';
|
||||||
import { EnteFile } from 'types/file';
|
import { EnteFile } from 'types/file';
|
||||||
|
|
||||||
|
@ -136,25 +135,12 @@ class DownloadManagerImpl {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
const cacheResp: Response = await this.diskFileCache?.match(
|
const cacheResp: Response = await this.diskFileCache?.match(
|
||||||
file.id.toString()
|
file.id.toString(),
|
||||||
|
{ sizeInBytes: file.info?.fileSize }
|
||||||
);
|
);
|
||||||
if (!cacheResp) {
|
if (!cacheResp) {
|
||||||
return null;
|
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) {
|
} catch (e) {
|
||||||
logError(e, 'failed to get cached thumbnail');
|
logError(e, 'failed to get cached thumbnail');
|
||||||
throw e;
|
throw e;
|
||||||
|
|
|
@ -958,18 +958,3 @@ const getFileObjectURL = (
|
||||||
: null;
|
: null;
|
||||||
return convertedURL;
|
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(
|
function transformMatch(
|
||||||
fn: ProxiedWorkerLimitedCache['match']
|
fn: ProxiedWorkerLimitedCache['match']
|
||||||
): LimitedCache['match'] {
|
): LimitedCache['match'] {
|
||||||
return async (key: string) => {
|
return async (key: string, options) => {
|
||||||
return deserializeToResponse(await fn(key));
|
return deserializeToResponse(await fn(key, options));
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -16,7 +16,10 @@ export interface ProxiedLimitedElectronAPIs {
|
||||||
) => Promise<Uint8Array>;
|
) => Promise<Uint8Array>;
|
||||||
}
|
}
|
||||||
export interface ProxiedWorkerLimitedCache {
|
export interface ProxiedWorkerLimitedCache {
|
||||||
match: (key: string) => Promise<ArrayBuffer>;
|
match: (
|
||||||
|
key: string,
|
||||||
|
options?: { sizeInBytes?: number }
|
||||||
|
) => Promise<ArrayBuffer>;
|
||||||
put: (key: string, data: ArrayBuffer) => Promise<void>;
|
put: (key: string, data: ArrayBuffer) => Promise<void>;
|
||||||
delete: (key: string) => Promise<boolean>;
|
delete: (key: string) => Promise<boolean>;
|
||||||
}
|
}
|
||||||
|
@ -53,8 +56,8 @@ export class WorkerSafeElectronClient implements ProxiedLimitedElectronAPIs {
|
||||||
function transformMatch(
|
function transformMatch(
|
||||||
fn: LimitedCache['match']
|
fn: LimitedCache['match']
|
||||||
): ProxiedWorkerLimitedCache['match'] {
|
): ProxiedWorkerLimitedCache['match'] {
|
||||||
return async (key: string) => {
|
return async (key: string, options: { sizeInBytes?: number }) => {
|
||||||
return serializeResponse(await fn(key));
|
return serializeResponse(await fn(key, options));
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -7,7 +7,10 @@ export interface LimitedCacheStorage {
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface LimitedCache {
|
export interface LimitedCache {
|
||||||
match: (key: string) => Promise<Response>;
|
match: (
|
||||||
|
key: string,
|
||||||
|
options?: { sizeInBytes?: number }
|
||||||
|
) => Promise<Response>;
|
||||||
put: (key: string, data: Response) => Promise<void>;
|
put: (key: string, data: Response) => Promise<void>;
|
||||||
delete: (key: string) => Promise<boolean>;
|
delete: (key: string) => Promise<boolean>;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue