Skip to content

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

web: handle errors during attachment downloading

Abdullah Attacf1322952d2b
diff --git a/apps/web/src/interfaces/fs.ts b/apps/web/src/interfaces/fs.ts
index 6fdb303c7..dfd21ea59 100644
--- a/apps/web/src/interfaces/fs.ts
+++ b/apps/web/src/interfaces/fs.ts
@@ -446,18 +446,19 @@ function reportProgress(
}
async function downloadFile(filename: string, requestOptions: RequestOptions) {
- const { url, headers, chunkSize, signal } = requestOptions;
- const handle = await streamablefs.readFile(filename);
+ try {
+ const { url, headers, chunkSize, signal } = requestOptions;
+ const handle = await streamablefs.readFile(filename);
- if (
- handle &&
- handle.file.size === (await handle.size()) - handle.file.chunks * ABYTES
- )
- return true;
- else if (handle) await handle.delete();
+ if (
+ handle &&
+ handle.file.size === (await handle.size()) - handle.file.chunks * ABYTES
+ )
+ return true;
+ else if (handle) await handle.delete();
+
+ const attachment = db.attachments?.attachment(filename);
- const attachment = db.attachments?.attachment(filename);
- try {
reportProgress(
{ total: 100, loaded: 0 },
{ type: "download", hash: filename }
diff --git a/apps/web/src/interfaces/opfs.worker.ts b/apps/web/src/interfaces/opfs.worker.ts
index 1f840cd04..3aa19cbc4 100644
--- a/apps/web/src/interfaces/opfs.worker.ts
+++ b/apps/web/src/interfaces/opfs.worker.ts
@@ -85,7 +85,9 @@ class OriginPrivateFileStore implements IFileStorage {
// on all browsers so we wait for the file handle to be released before
// continuing. This is temporary until all browsers start supporting
// the read-only mode.
- return this.safeOp(chunkName, () => readFile(this.directory, chunkName));
+ return await this.safeOp(chunkName, () =>
+ readFile(this.directory, chunkName)
+ );
} catch (e) {
console.error("Failed to read chunk", e);
return;
@@ -99,7 +101,7 @@ class OriginPrivateFileStore implements IFileStorage {
const promise = createPromise();
this.locks.set(chunkName, promise);
- return promise.finally(() => this.locks.delete(chunkName));
+ return await promise.finally(() => this.locks.delete(chunkName));
}
}
@@ -145,14 +147,11 @@ expose(workerModule);
export type OriginPrivateFileStoreWorkerType = typeof workerModule;
-function readFile(directory: FileSystemDirectoryHandle, name: string) {
- return directory
- .getFileHandle(name)
- .then((file) => file.createSyncAccessHandle({ mode: "read-only" }))
- .then((handle) => {
- const buffer = new Uint8Array(handle.getSize());
- handle.read(buffer);
- handle.close();
- return buffer;
- });
+async function readFile(directory: FileSystemDirectoryHandle, name: string) {
+ const file = await directory.getFileHandle(name);
+ const handle = await file.createSyncAccessHandle({ mode: "read-only" });
+ const buffer = new Uint8Array(handle.getSize());
+ handle.read(buffer);
+ handle.close();
+ return buffer;
}
diff --git a/apps/web/src/stores/attachment-store.js b/apps/web/src/stores/attachment-store.js
index f790dae90..11b856009 100644
--- a/apps/web/src/stores/attachment-store.js
+++ b/apps/web/src/stores/attachment-store.js
@@ -72,9 +72,8 @@ class AttachmentStore extends BaseStore {
await createWriteStream("attachments.zip", {
signal: abortController.signal
})
- );
-
- this.set((state) => (state.status = undefined));
+ )
+ .finally(() => this.set((state) => (state.status = undefined)));
};
cancel = async () => {
diff --git a/apps/web/src/utils/streams/attachment-stream.ts b/apps/web/src/utils/streams/attachment-stream.ts
index 80074a3c6..8e03dff29 100644
--- a/apps/web/src/utils/streams/attachment-stream.ts
+++ b/apps/web/src/utils/streams/attachment-stream.ts
@@ -40,46 +40,51 @@ export class AttachmentStream extends ReadableStream<ZipFile> {
super({
start() {},
async pull(controller) {
- if (signal?.aborted) {
- controller.close();
- return;
- }
-
- onProgress && onProgress(index);
- const attachment = attachments[index++];
+ try {
+ if (signal?.aborted) {
+ controller.close();
+ return;
+ }
- await db.fs?.downloadFile(
- GROUP_ID,
- attachment.metadata.hash,
- attachment.chunkSize,
- attachment.metadata
- );
+ onProgress && onProgress(index);
+ const attachment = attachments[index++];
- const key = await db.attachments?.decryptKey(attachment.key);
- const file = await lazify(
- import("../../interfaces/fs"),
- ({ decryptFile }) =>
- decryptFile(attachment.metadata.hash, {
- key,
- iv: attachment.iv,
- name: attachment.metadata.filename,
- type: attachment.metadata.type,
- isUploaded: !!attachment.dateUploaded
- })
- );
+ await db.fs?.downloadFile(
+ GROUP_ID,
+ attachment.metadata.hash,
+ attachment.chunkSize,
+ attachment.metadata
+ );
- if (file) {
- const filePath: string = attachment.metadata.filename;
- controller.enqueue({
- path: makeUniqueFilename(filePath, counters),
- data: new Uint8Array(await file.arrayBuffer())
- });
- } else {
- controller.error(new Error("Failed to decrypt file."));
- }
+ const key = await db.attachments?.decryptKey(attachment.key);
+ const file = await lazify(
+ import("../../interfaces/fs"),
+ ({ decryptFile }) =>
+ decryptFile(attachment.metadata.hash, {
+ key,
+ iv: attachment.iv,
+ name: attachment.metadata.filename,
+ type: attachment.metadata.type,
+ isUploaded: !!attachment.dateUploaded
+ })
+ );
- if (index === attachments.length) {
- controller.close();
+ if (file) {
+ const filePath: string = attachment.metadata.filename;
+ controller.enqueue({
+ path: makeUniqueFilename(filePath, counters),
+ data: new Uint8Array(await file.arrayBuffer())
+ });
+ } else {
+ controller.error(new Error("Failed to decrypt file."));
+ }
+ } catch (e) {
+ console.error(e);
+ controller.error(e);
+ } finally {
+ if (index === attachments.length) {
+ controller.close();
+ }
}
}
});