This commit is contained in:
SunYF
2026-10-26 16:32:35 +08:00
parent 4b14f9540a
commit f86feb2271
99 changed files with 1652 additions and 521 deletions
+46 -16
View File
@@ -31,13 +31,14 @@ const (
FileTypeProcessCard = "PROCESS_CARD" // 流程卡
FileTypePackagePhoto = "PACKAGE_PHOTO" // 包装照片
FileTypeExcelImport = "EXCEL_IMPORT" // Excel 导入
FileTypeImage = "IMAGE" // 图片(通用,不细分业务:png/jpg/jpeg/gif/webp)
FileTypeOther = "OTHER" // 其他
)
var validFileTypes = map[string]bool{
FileTypeProcessPDF: true, FileTypeInspectionPhoto: true, FileTypeInspectionPDF: true,
FileTypeDrawing: true, FileTypeProcessCard: true, FileTypePackagePhoto: true,
FileTypeExcelImport: true, FileTypeOther: true,
FileTypeExcelImport: true, FileTypeImage: true, FileTypeOther: true,
}
// allowedAttachmentExt 允许的附件扩展名白名单(图纸/报告/图片/表格)。白名单而非黑名单:避免上传可执行文件。
@@ -69,29 +70,42 @@ func maxSizeFor(ctx *svc.ServiceContext, fileType string) int64 {
return 20 << 20
}
// deriveFileType 未显式指定文件类型时,按业务类型 + 扩展名推断,兜底 OTHER。
// deriveFileType 未显式指定文件类型时,按「扩展名优先」推断:
// 1) 图片类(png/jpg/jpeg/gif/webp)→ 按业务类型细分(检验/来料/半成品→检验照片,包裹/出库→包裹照片),无更细业务类型归「图片(IMAGE)」;
// 2) 非图片按业务类型 + 扩展名细分(PDF/DWG 等);
// 3) 兜底 OTHER。
// 扩展名优先可避免「material 的 png 照片被误判为其他」等问题。
func deriveFileType(bizType, ext string) string {
ext = strings.ToLower(ext)
switch {
case ext == ".xlsx" || ext == ".xls" || ext == ".csv":
return FileTypeExcelImport
case bizType == "inspection":
// 扩展名优先:图片类
if ext == ".png" || ext == ".jpg" || ext == ".jpeg" || ext == ".gif" || ext == ".webp" {
switch bizType {
case "inspection", "inbound", "semi":
return FileTypeInspectionPhoto
case "outbound", "package":
return FileTypePackagePhoto
default:
return FileTypeImage
}
}
// 非图片:按业务类型 + 扩展名细分
switch bizType {
case "inspection":
if ext == ".pdf" {
return FileTypeInspectionPDF
}
if ext == ".png" || ext == ".jpg" || ext == ".jpeg" || ext == ".gif" {
return FileTypeInspectionPhoto
}
case bizType == "material" && (ext == ".pdf" || ext == ".dwg" || ext == ".dxf"):
return FileTypeDrawing
case bizType == "outbound":
if ext == ".png" || ext == ".jpg" || ext == ".jpeg" || ext == ".gif" {
return FileTypePackagePhoto
case "material":
if ext == ".pdf" || ext == ".dwg" || ext == ".dxf" {
return FileTypeDrawing
}
case "outbound":
if ext == ".pdf" {
return FileTypeProcessPDF
}
}
if ext == ".xlsx" || ext == ".xls" || ext == ".csv" {
return FileTypeExcelImport
}
return FileTypeOther
}
@@ -412,8 +426,13 @@ func serveUploadFileHandler(ctx *svc.ServiceContext) http.HandlerFunc {
return
}
if fileName != "" {
// 中文文件名走 RFC 5987;下载(dl=1)才强制 attachment,否则内联预览
disp := "inline"
// 中文文件名走 RFC 5987;图片/PDF 内联预览,其他类型强制下载;
// 显式 dl=1 一律强制下载。与 MES 附件服务判定保持一致(B1.1)。
ext := strings.ToLower(filepath.Ext(fileName))
disp := "attachment"
if isInlineExt(ext) {
disp = "inline"
}
if r.URL.Query().Get("dl") == "1" {
disp = "attachment"
}
@@ -554,6 +573,17 @@ func archiveAttachmentsHandler(ctx *svc.ServiceContext) http.HandlerFunc {
}
}
// isInlineExt 判断扩展名是否可在浏览器内联预览(图片 / PDF)。
// 与 MES attachment 包的判定保持一致:只有这类文件才允许 inline,其余一律强制下载,
// 避免 docx/xlsx/zip 等被当垃圾数据内联打开(B1.1 对齐两端行为)。
func isInlineExt(ext string) bool {
switch strings.ToLower(ext) {
case ".png", ".jpg", ".jpeg", ".gif", ".webp", ".pdf":
return true
}
return false
}
// copyFile 兜底复制(跨盘符 Rename 失败时用)。
func copyFile(src, dst string) error {
in, err := os.Open(src)