mirror of
https://github.com/streetwriters/notesnook.git
synced 2025-12-29 00:20:04 +01:00
web: make duplicate filenames in attachments unique on download (#3678)
* web: make duplicate filenames in attachments unique * Update apps/web/src/utils/streams/attachment-stream.ts Signed-off-by: Abdullah Atta <thecodrr@protonmail.com> * web: make attachment filename case insensitive --------- Signed-off-by: Abdullah Atta <thecodrr@protonmail.com>
This commit is contained in:
@@ -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]
|
||||
}`;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user