fix(files): add robust upload fallback in file form template patch

This commit is contained in:
svemagie
2026-03-08 01:54:35 +01:00
parent 7a61f4da53
commit f64f8b8c16

View File

@@ -8,6 +8,34 @@ const candidates = [
const oldCode = "xhr.open('POST', endpoint);";
const newCode = "xhr.open('POST', window.location.pathname);";
const textFallbackPatches = [
{
from: '{{ __("files.upload.dropText") }}',
to: '{{ __("files.upload.dropText") == "files.upload.dropText" and "Drag files here or" or __("files.upload.dropText") }}',
},
{
from: '{{ __("files.upload.browse") }}',
to: '{{ __("files.upload.browse") == "files.upload.browse" and "Browse files" or __("files.upload.browse") }}',
},
{
from: '{{ __("files.upload.submitMultiple") }}',
to: '{{ __("files.upload.submitMultiple") == "files.upload.submitMultiple" and "Upload files" or __("files.upload.submitMultiple") }}',
},
];
const fallbackSubmitBlock = ` {# Reliable fallback submit (works even when Alpine/XHR upload is unavailable) #}
<div class="button-group">
{{ button({
text: __("files.form.submit"),
attributes: {
type: "submit",
formenctype: "multipart/form-data",
"x-show": "files.length === 0 || allDone"
}
}) }}
</div>
`;
async function exists(path) {
try {
await access(path);
@@ -27,16 +55,33 @@ for (const filePath of candidates) {
checked += 1;
const source = await readFile(filePath, "utf8");
let updated = source;
let changed = false;
if (source.includes(newCode)) {
if (updated.includes(oldCode)) {
updated = updated.replace(oldCode, newCode);
changed = true;
}
for (const patch of textFallbackPatches) {
if (updated.includes(patch.from) && !updated.includes(patch.to)) {
updated = updated.replace(patch.from, patch.to);
changed = true;
}
}
if (updated.includes("<noscript>")) {
const noScriptPattern = /\n\s*\{# No-JS fallback #\}[\s\S]*?<\/noscript>\n/;
if (noScriptPattern.test(updated)) {
updated = updated.replace(noScriptPattern, `\n${fallbackSubmitBlock}\n`);
changed = true;
}
}
if (!changed) {
continue;
}
if (!source.includes(oldCode)) {
continue;
}
const updated = source.replace(oldCode, newCode);
await writeFile(filePath, updated, "utf8");
patched += 1;
}
@@ -44,9 +89,9 @@ for (const filePath of candidates) {
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");
console.log("[postinstall] endpoint-files upload template already patched");
} else {
console.log(
`[postinstall] Patched endpoint-files upload route in ${patched} file(s)`,
`[postinstall] Patched endpoint-files upload template in ${patched} file(s)`,
);
}