fix(uploads): route browser file uploads via files endpoint

This commit is contained in:
svemagie
2026-03-08 00:57:07 +01:00
parent e7d0a3d382
commit e212b164f2
4 changed files with 58 additions and 4 deletions

View File

@@ -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`).
- 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.

View File

@@ -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": [],

View File

@@ -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)`,
);
}

View File

@@ -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