diff --git a/frontend/src/utils/downloadPath.test.ts b/frontend/src/utils/downloadPath.test.ts index 52cf8c015..4f1d112a1 100644 --- a/frontend/src/utils/downloadPath.test.ts +++ b/frontend/src/utils/downloadPath.test.ts @@ -1,6 +1,6 @@ import { describe, expect, it } from "vitest"; import type { SimpleRom } from "@/stores/roms"; -import { getDownloadPath } from "./downloadPath"; +import { getDownloadLink, getDownloadPath } from "./downloadPath"; import { makeRom } from "./rom.fixtures"; describe("getDownloadPath", () => { @@ -14,7 +14,7 @@ describe("getDownloadPath", () => { ] as SimpleRom["files"], }); expect(getDownloadPath({ rom })).toBe( - "/api/roms/14/content/Maniac Mansion (1989).adf", + "/api/roms/14/content/Maniac%20Mansion%20(1989).adf", ); }); @@ -83,3 +83,50 @@ describe("getDownloadPath", () => { ); }); }); + +describe("download URL encoding", () => { + const rom = makeRom({ + id: 24, + fs_name: "Game & (USA)", + has_nested_single_file: false, + files: [ + { id: 29, file_name: "Game & (USA) Disk1.adf" }, + { id: 30, file_name: "Disk2.adf" }, + ] as SimpleRom["files"], + }); + + it("encodes a name that would otherwise truncate the URL", () => { + // A bare `#` starts the fragment, so an unencoded fs_name loses + // everything after it before the request is even sent. + const hashRom = makeRom({ id: 7, fs_name: "Game #1 (100%)" }); + const url = new URL(getDownloadLink({ rom: hashRom })); + expect(url.hash).toBe(""); + expect(decodeURIComponent(url.pathname)).toBe( + "/api/roms/7/content/Game #1 (100%)", + ); + }); + + it("does not double-encode the selected file name", () => { + const url = new URL(getDownloadLink({ rom, fileIDs: [29] })); + expect(url.pathname).not.toContain("%25"); + expect(decodeURIComponent(url.pathname)).toBe( + "/api/roms/24/content/Game & (USA) Disk1.adf", + ); + }); + + it("leaves file_ids parseable for a multi-file zip link", () => { + // The backend splits this on "," and int()s each part, so a re-encoded + // comma reaches it as one unparseable value. + const url = new URL(getDownloadLink({ rom, fileIDs: [29, 30] })); + expect(url.searchParams.get("file_ids")).toBe("29,30"); + expect(decodeURIComponent(url.pathname)).toBe( + "/api/roms/24/content/Game & (USA)", + ); + }); + + it("prefixes the path with the origin and nothing else", () => { + expect(getDownloadLink({ rom })).toBe( + `${window.location.origin}${getDownloadPath({ rom })}`, + ); + }); +}); diff --git a/frontend/src/utils/downloadPath.ts b/frontend/src/utils/downloadPath.ts index 14f8cf180..3c00ef000 100644 --- a/frontend/src/utils/downloadPath.ts +++ b/frontend/src/utils/downloadPath.ts @@ -25,9 +25,12 @@ export function getDownloadPath({ ? rom.files[0] : undefined; const contentFile = selectedFile ?? nestedFile; - const contentName = contentFile - ? encodeURIComponent(contentFile.file_name) - : rom.fs_name; + // One path segment, so it is encoded here and never again: callers that + // prepend an origin must not re-encode. A bare `#` or `%` in a name would + // otherwise truncate the URL or survive as a literal in the zip's name. + const contentName = encodeURIComponent( + contentFile ? contentFile.file_name : rom.fs_name, + ); return `/api/roms/${rom.id}/content/${contentName}${ queryString ? `?${queryString}` : "" @@ -41,7 +44,5 @@ export function getDownloadLink({ rom: SimpleRom; fileIDs?: number[]; }) { - return `${window.location.origin}${encodeURI( - getDownloadPath({ rom, fileIDs }), - )}`; + return `${window.location.origin}${getDownloadPath({ rom, fileIDs })}`; }