diff --git a/README.md b/README.md index 1e6a67d3..0d6649c3 100644 --- a/README.md +++ b/README.md @@ -62,5 +62,6 @@ - `start.sh` is intentionally ignored by Git (`.gitignore`) so server secrets are not committed. - Use `start.example.sh` as the tracked template and keep real credentials in environment variables (or `.env` on the server). -- Startup scripts run patch helpers before boot (`scripts/patch-lightningcss.mjs`, `scripts/patch-endpoint-media-scope.mjs`). -- The media scope patch fixes a known upstream issue where file uploads can fail if the token scope is `create update delete` without explicit `media`. \ No newline at end of file +- Startup scripts run patch helpers before boot (`scripts/patch-lightningcss.mjs`, `scripts/patch-endpoint-media-scope.mjs`, `scripts/patch-endpoint-files-upload-route.mjs`). +- The media scope patch fixes a known upstream issue where file uploads can fail if the token scope is `create update delete` without explicit `media`. +- The files upload route patch fixes browser multi-upload by posting to `/files/upload` (session-authenticated) instead of direct `/media` calls without bearer token. \ No newline at end of file diff --git a/package.json b/package.json index 1bb2a84e..91c4af2a 100644 --- a/package.json +++ b/package.json @@ -4,8 +4,8 @@ "description": "", "main": "index.js", "scripts": { - "postinstall": "node scripts/patch-lightningcss.mjs && node scripts/patch-endpoint-media-scope.mjs", - "serve": "node scripts/patch-lightningcss.mjs && node scripts/patch-endpoint-media-scope.mjs && node node_modules/@indiekit/indiekit/bin/cli.js serve --config indiekit.config.mjs", + "postinstall": "node scripts/patch-lightningcss.mjs && node scripts/patch-endpoint-media-scope.mjs && node scripts/patch-endpoint-files-upload-route.mjs", + "serve": "node scripts/patch-lightningcss.mjs && node scripts/patch-endpoint-media-scope.mjs && node scripts/patch-endpoint-files-upload-route.mjs && node node_modules/@indiekit/indiekit/bin/cli.js serve --config indiekit.config.mjs", "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], diff --git a/scripts/patch-endpoint-files-upload-route.mjs b/scripts/patch-endpoint-files-upload-route.mjs new file mode 100644 index 00000000..86b4543d --- /dev/null +++ b/scripts/patch-endpoint-files-upload-route.mjs @@ -0,0 +1,52 @@ +import { access, readFile, writeFile } from "node:fs/promises"; + +const candidates = [ + "node_modules/@indiekit/endpoint-files/views/file-form.njk", + "node_modules/@indiekit/indiekit/node_modules/@indiekit/endpoint-files/views/file-form.njk", +]; + +const oldCode = "xhr.open('POST', endpoint);"; +const newCode = "xhr.open('POST', window.location.pathname);"; + +async function exists(path) { + try { + await access(path); + return true; + } catch { + return false; + } +} + +let checked = 0; +let patched = 0; + +for (const filePath of candidates) { + if (!(await exists(filePath))) { + continue; + } + + checked += 1; + const source = await readFile(filePath, "utf8"); + + if (source.includes(newCode)) { + continue; + } + + if (!source.includes(oldCode)) { + continue; + } + + const updated = source.replace(oldCode, newCode); + await writeFile(filePath, updated, "utf8"); + patched += 1; +} + +if (checked === 0) { + console.log("[postinstall] No endpoint-files upload template files found"); +} else if (patched === 0) { + console.log("[postinstall] endpoint-files upload route already patched"); +} else { + console.log( + `[postinstall] Patched endpoint-files upload route in ${patched} file(s)`, + ); +} diff --git a/start.example.sh b/start.example.sh index 0b03397c..16743f42 100644 --- a/start.example.sh +++ b/start.example.sh @@ -30,5 +30,6 @@ export NODE_ENV="${NODE_ENV:-production}" # Ensure runtime dependency patches are applied even if node_modules already exists. /usr/local/bin/node scripts/patch-lightningcss.mjs /usr/local/bin/node scripts/patch-endpoint-media-scope.mjs +/usr/local/bin/node scripts/patch-endpoint-files-upload-route.mjs exec /usr/local/bin/node node_modules/@indiekit/indiekit/bin/cli.js serve --config indiekit.config.mjs