Skip to content

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

web: uniquify dup filenames when creating zip

Abdullah Atta9f853f0b3655
diff --git a/apps/web/src/utils/zip.ts b/apps/web/src/utils/zip.ts
index d7a99b646..6ebcdd5a5 100644
--- a/apps/web/src/utils/zip.ts
+++ b/apps/web/src/utils/zip.ts
@@ -23,13 +23,22 @@ import { sanitizeFilename } from "./filename";
const textEncoder = new TextEncoder();
type File = { filename: string; content: string };
async function zip(files: File[], format: string): Promise<Uint8Array> {
- const obj: Unzipped = {};
+ const obj: Unzipped = Object.create(null);
files.forEach((file) => {
- obj[`${sanitizeFilename(file.filename)}.${format}`] = textEncoder.encode(
- file.content
- );
+ const name = sanitizeFilename(file.filename);
+ let counter = 0;
+ while (obj[makeFilename(name, format, counter)]) ++counter;
+
+ obj[makeFilename(name, format, counter)] = textEncoder.encode(file.content);
});
const { zipSync } = await import("fflate");
return zipSync(obj);
}
export { zip };
+
+function makeFilename(filename: string, extension: string, counter: number) {
+ let final = filename;
+ if (counter) final += `-${counter}`;
+ final += `.${extension}`;
+ return final;
+}