22 lines
796 B
TypeScript
22 lines
796 B
TypeScript
export function normalizeTaskArtifactFileName(value: unknown): string {
|
|
let candidate = String(value ?? '').trim();
|
|
if (!candidate) return '';
|
|
// Some legacy ERP responses exposed raw UTF-8 bytes as Latin-1-looking
|
|
// characters. Repair only when the converted value is clearly CJK text.
|
|
if (/[ÃÂâåæçèé]/u.test(candidate)) {
|
|
try {
|
|
const repaired = Buffer.from(candidate, 'latin1').toString('utf8');
|
|
if (repaired !== candidate && /[\u3400-\u9fff]/u.test(repaired) && !repaired.includes('�')) {
|
|
candidate = repaired;
|
|
}
|
|
} catch {
|
|
// Keep the original candidate when the legacy value cannot be repaired.
|
|
}
|
|
}
|
|
return candidate
|
|
.replace(/[\\/\u0000-\u001f\u007f]/g, '_')
|
|
.replace(/^\.+$/, '')
|
|
.trim()
|
|
.slice(0, 200);
|
|
}
|