修复报销附件上传与中文文件名(07-28-travel-reimbursement-guide)

这个提交包含在:
2026-07-30 14:41:08 +08:00
父节点 531d2846a6
当前提交 734153847b
共修改 15 个文件,包含 434 行新增和 65 行删除
@@ -1,6 +1,7 @@
const crypto = require("node:crypto");
const sharp = require("sharp");
const { buildAttachmentContentDisposition: buildContentDisposition } = require("../utils/contentDisposition");
const PURCHASE_ATTACHMENT_MAX_IMAGE_PIXELS = 40_000_000;
const PURCHASE_ATTACHMENT_MAX_FILE_NAME_CODE_POINTS = 180;
@@ -393,50 +394,11 @@ function buildAttachmentStorageKey(purchaseRequestId, extension) {
return `purchase-requests/${purchaseRequestId.toLowerCase()}/${crypto.randomUUID()}${extension}`;
}
function sanitizeHeaderFileName(fileName) {
const source = typeof fileName === "string" && fileName.trim()
? fileName.normalize("NFC").trim()
: "attachment.bin";
const sanitized = truncateCodePoints(
source
.replace(/[\\/]/gu, "_")
.replace(/[\u0000-\u001f\u007f-\u009f]/gu, "_")
.replace(/[\u200b-\u200f\u202a-\u202e\u2060-\u2069\ufeff]/gu, "_")
.replace(/"+/gu, "_")
.replace(/\s+/gu, " ")
.trim(),
PURCHASE_ATTACHMENT_MAX_FILE_NAME_CODE_POINTS,
);
return sanitized && sanitized !== "." && sanitized !== ".."
? sanitized
: "attachment.bin";
}
function buildAsciiFallback(fileName) {
const lastDotIndex = fileName.lastIndexOf(".");
const rawExtension = lastDotIndex > 0 ? fileName.slice(lastDotIndex).toLowerCase() : ".bin";
const extension = /^\.[a-z0-9]{1,10}$/u.test(rawExtension) ? rawExtension : ".bin";
const rawStem = lastDotIndex > 0 ? fileName.slice(0, lastDotIndex) : fileName;
const asciiStem = rawStem
.normalize("NFKD")
.replace(/[\u0300-\u036f]/gu, "")
.replace(/[^a-zA-Z0-9._-]+/gu, "_")
.replace(/_+/gu, "_")
.replace(/^[._-]+|[._-]+$/gu, "")
.slice(0, 100);
return `${asciiStem || "attachment"}${extension}`;
}
function encodeRfc5987Value(value) {
return encodeURIComponent(value).replace(/[\u0027\u0028\u0029\u002a]/gu, (character) =>
`%${character.charCodeAt(0).toString(16).toUpperCase()}`,
);
}
function buildAttachmentContentDisposition(fileName) {
const safeFileName = sanitizeHeaderFileName(fileName);
const asciiFallback = buildAsciiFallback(safeFileName);
return `attachment; filename="${asciiFallback}"; filename*=UTF-8''${encodeRfc5987Value(safeFileName)}`;
function buildAttachmentContentDisposition(fileName, options = {}) {
return buildContentDisposition(fileName, {
...options,
maxCodePoints: PURCHASE_ATTACHMENT_MAX_FILE_NAME_CODE_POINTS,
});
}
module.exports = {