Skip to content

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

web: make duplicate filenames in attachments unique on download (#3678)

Abdullah Attaebc95fcea70d
diff --git a/apps/web/src/utils/streams/attachment-stream.ts b/apps/web/src/utils/streams/attachment-stream.ts
index 3458ebfbe..89373c65f 100644
--- a/apps/web/src/utils/streams/attachment-stream.ts
+++ b/apps/web/src/utils/streams/attachment-stream.ts
@@ -29,12 +29,13 @@ export class AttachmentStream extends ReadableStream<ZipFile> {
signal?: AbortSignal,
onProgress?: (current: number) => void
) {
+ let index = 0;
+ const counters: Record<string, number> = {};
if (signal)
signal.onabort = async () => {
await db.fs?.cancel(GROUP_ID, "download");
};
- let index = 0;
super({
start() {},
async pull(controller) {
@@ -63,9 +64,9 @@ export class AttachmentStream extends ReadableStream<ZipFile> {
});
if (file) {
- const filePath = attachment.metadata.filename;
+ const filePath: string = attachment.metadata.filename;
controller.enqueue({
- path: filePath,
+ path: makeUniqueFilename(filePath, counters),
data: new Uint8Array(await file.arrayBuffer())
});
} else {
@@ -79,3 +80,17 @@ export class AttachmentStream extends ReadableStream<ZipFile> {
});
}
}
+
+function makeUniqueFilename(
+ filePath: string,
+ counters: Record<string, number>
+) {
+ filePath = filePath.toLowerCase();
+ counters[filePath] = (counters[filePath] || 0) + 1;
+ if (counters[filePath] === 1) return filePath;
+
+ const parts = filePath.split(".");
+ return `${parts.slice(0, -1).join(".")}-${counters[filePath]}.${
+ parts[parts.length - 1]
+ }`;
+}