Adjust encoded paths so filenames resolve as a basename properly when downloading with things like wget.

This commit is contained in:
Jaret Burkett
2026-08-15 08:56:55 -06:00
parent 151ad0e959
commit e6cffbc002
15 changed files with 94 additions and 39 deletions

View File

@@ -0,0 +1,19 @@
/**
* Turn a `[...param]` catch-all route param into the requested filesystem path.
*
* Next.js hands catch-all params over as an array of URL segments with the
* percent-encoding of each segment already decoded, so both URL forms map to
* the same path:
* /api/files/%2Fmnt%2Fout%2Fjob%2Ffile.safetensors (legacy: one segment)
* /api/files/%2Fmnt%2Fout%2Fjob/file.safetensors (folder / filename)
* The second form gives downloaders (wget, browsers) the real filename as the
* last URL segment. Windows paths arrive with their backslashes intact inside
* a segment (`C:\out\job` + `/` + `file.safetensors`); path.resolve on the
* caller side normalizes the mixed separators.
*
* Mirrors the per-segment decode in cron/fileServer.ts serveFile().
*/
export const catchAllToFilePath = (param: string | string[] | undefined): string => {
if (param === undefined) return '';
return (Array.isArray(param) ? param : [param]).join('/');
};