From d33617914dee67500b52fb5b8a64bb6379a82d78 Mon Sep 17 00:00:00 2001 From: OpenDeernet <241758973+OpenDeernet@users.noreply.github.com> Date: Fri, 6 Nov 2026 09:51:36 +0800 Subject: [PATCH] Fix uniqueName hanging on extensionless file names When a dropped photo has no extension (macOS Photos sometimes drops one) and that name is already taken, the old replace left the candidate unchanged and the save loop never finished. Append -N when there is no extension. --- src/lib/files.ts | 6 +++++- tests/unit/files.test.ts | 5 +++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/lib/files.ts b/src/lib/files.ts index 8d2f8ce..4d629a6 100644 --- a/src/lib/files.ts +++ b/src/lib/files.ts @@ -18,6 +18,10 @@ export function droppedPhotos(transfer: DataTransfer): File[] { // in the chosen folder, must not be overwritten: IMG_0001-date-2.jpg. export function uniqueName(taken: (name: string) => boolean, name: string) { let candidate = name - for (let n = 2; taken(candidate); n++) candidate = name.replace(/(\.[^.]+)$/, `-${n}$1`) + for (let n = 2; taken(candidate); n++) { + // A name without an extension (macOS Photos sometimes drops one) would + // otherwise keep the same candidate forever and hang the save. + candidate = /(\.[^.]+)$/.test(name) ? name.replace(/(\.[^.]+)$/, `-${n}$1`) : `${name}-${n}` + } return candidate } diff --git a/tests/unit/files.test.ts b/tests/unit/files.test.ts index 9452187..0f82536 100644 --- a/tests/unit/files.test.ts +++ b/tests/unit/files.test.ts @@ -40,3 +40,8 @@ describe("stampedFileName", () => { expect(stampedFileName("holiday.photo.png")).toBe("holiday.photo-date.jpg") }) }) + + it("numbers an extensionless name without looping", () => { + const taken = new Set(["export", "export-2"]) + expect(uniqueName((name) => taken.has(name), "export")).toBe("export-3") + })