fix: truncate long filenames to prevent filesystem ENAMETOOLONG errors

This commit is contained in:
ashim-hq
2026-04-20 22:14:14 +08:00
parent c738f16107
commit 795965ed43
2 changed files with 64 additions and 2 deletions
+13
View File
@@ -45,5 +45,18 @@ export function sanitizeFilename(raw: string): string {
}
}
// Truncate to filesystem-safe length (255 byte NAME_MAX minus margin for _toolId suffix)
const MAX_NAME_BYTES = 200;
const enc = new TextEncoder();
if (enc.encode(name).length > MAX_NAME_BYTES) {
const dotIdx = name.lastIndexOf(".");
const ext = dotIdx > 0 ? name.slice(dotIdx) : "";
let base = dotIdx > 0 ? name.slice(0, dotIdx) : name;
while (enc.encode(base + ext).length > MAX_NAME_BYTES) {
base = base.slice(0, -1);
}
name = base + ext;
}
return name;
}