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
+43 -13
View File
@@ -32,13 +32,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,
}
// allowedExt 允许的扩展名白名单(图纸/报告/图片/表格/压缩包)。白名单而非黑名单,避免上传可执行文件。
@@ -48,27 +49,45 @@ var allowedExt = map[string]bool{
".dwg": true, ".dxf": true, ".zip": true, ".txt": true, ".csv": true,
}
// 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":
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 "material":
if ext == ".pdf" || ext == ".dwg" || ext == ".dxf" {
return FileTypeDrawing
}
case bizType == "material" && (ext == ".pdf" || ext == ".dwg" || ext == ".dxf"):
return FileTypeDrawing
case bizType == "work_order":
case "work_order":
if ext == ".pdf" {
return FileTypeProcessCard
}
case bizType == "process_flow" && ext == ".pdf":
return FileTypeProcessPDF
case "process_flow":
if ext == ".pdf" {
return FileTypeProcessPDF
}
}
if ext == ".xlsx" || ext == ".xls" || ext == ".csv" {
return FileTypeExcelImport
}
return FileTypeOther
}
@@ -267,6 +286,17 @@ func serveUploadFile(w http.ResponseWriter, r *http.Request, dir, name string) {
http.ServeContent(w, r, filepath.Base(abs), time.Time{}, f)
}
// isInlineExt 判断扩展名是否可在浏览器内联预览(图片 / PDF)。
// 与 WMS 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
}
// randHex 随机十六进制串(保留给其他场景的短随机名使用)。
func randHex(n int) string {
b := make([]byte, n)