Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

web: impl chunkSize method for faster file size calculation

Abdullah Atta782c53b62220
diff --git a/apps/web/src/interfaces/file-store.ts b/apps/web/src/interfaces/file-store.ts
index efaf65332..796877b6f 100644
--- a/apps/web/src/interfaces/file-store.ts
+++ b/apps/web/src/interfaces/file-store.ts
@@ -57,6 +57,12 @@ export class IndexedDBFileStore implements IFileStorage {
(k as string).startsWith(chunkPrefix)
) as string[];
}
+
+ async chunkSize(chunkName: string): Promise<number> {
+ const chunk = await this.storage.get<Uint8Array>(chunkName);
+ return chunk?.length || 0;
+ }
+
async list(): Promise<string[]> {
return (await this.storage.keys()) as string[];
}
@@ -131,6 +137,14 @@ export class CacheStorageFileStore implements IFileStorage {
return keys.map((r) => r.url.slice(1));
}
+ async chunkSize(chunkName: string): Promise<number> {
+ const cache = await this.getCache();
+ const response = await cache.match(this.toURL(chunkName));
+ const length = response?.headers.get("Content-Length");
+ if (length) return parseInt(length);
+ return response ? (await response.arrayBuffer()).byteLength : 0;
+ }
+
private toURL(chunkName: string) {
return `/${chunkName}`;
}
@@ -185,6 +199,10 @@ export class OriginPrivateFileSystem implements IFileStorage {
await this.create();
return (await this.worker.listChunks(this.name, chunkPrefix)) || [];
}
+ async chunkSize(chunkName: string): Promise<number> {
+ await this.create();
+ return await this.worker.chunkSize(this.name, chunkName);
+ }
async list(): Promise<string[]> {
await this.create();
return (await this.worker.list(this.name)) || [];
diff --git a/apps/web/src/interfaces/opfs.worker.ts b/apps/web/src/interfaces/opfs.worker.ts
index 752a5fe33..5cba93b40 100644
--- a/apps/web/src/interfaces/opfs.worker.ts
+++ b/apps/web/src/interfaces/opfs.worker.ts
@@ -93,6 +93,20 @@ class OriginPrivateFileStore implements IFileStorage {
}
}
+ async chunkSize(chunkName: string): Promise<number> {
+ try {
+ if (Object.hasOwn(FileSystemSyncAccessHandle.prototype, "mode")) {
+ return readFileSize(this.directory, chunkName);
+ }
+ return await this.safeOp(chunkName, () =>
+ readFileSize(this.directory, chunkName)
+ );
+ } catch (e) {
+ console.error("Failed to get chunk size", e);
+ }
+ return 0;
+ }
+
async listChunks(chunkPrefix: string): Promise<string[]> {
const chunks: string[] = [];
for await (const entry of this.directory.keys()) {
@@ -161,6 +175,9 @@ const workerModule = {
},
async list(directoryName: string) {
return (await fileStores.get(directoryName)?.list()) || [];
+ },
+ async chunkSize(directoryName: string, chunkName: string) {
+ return (await fileStores.get(directoryName)?.chunkSize(chunkName)) || 0;
}
};
@@ -176,3 +193,11 @@ async function readFile(directory: FileSystemDirectoryHandle, name: string) {
handle.close();
return buffer;
}
+async function readFileSize(
+ directory: FileSystemDirectoryHandle,
+ name: string
+) {
+ const file = await directory.getFileHandle(name);
+ const handle = await file.createSyncAccessHandle({ mode: "read-only" });
+ return handle.getSize();
+}