feat: 优化配置和文件上传
- 上传文件名添加唯一ID避免覆盖 - 支持通过环境变量配置 JWT 密钥和 Token 过期时间 - 支持通过环境变量覆盖管理员账号(可选)
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
@@ -52,9 +54,16 @@ func Upload(c *gin.Context) {
|
||||
}
|
||||
billType := req.Type
|
||||
|
||||
// 3. 保存上传的文件
|
||||
// 3. 保存上传的文件(添加唯一ID避免覆盖)
|
||||
timestamp := time.Now().Format("20060102_150405")
|
||||
inputFileName := fmt.Sprintf("%s_%s", timestamp, header.Filename)
|
||||
uniqueID := generateShortID()
|
||||
|
||||
// 获取文件扩展名和基础名
|
||||
ext := filepath.Ext(header.Filename)
|
||||
baseName := header.Filename[:len(header.Filename)-len(ext)]
|
||||
|
||||
// 文件名格式: 时间戳_唯一ID_原始文件名
|
||||
inputFileName := fmt.Sprintf("%s_%s_%s%s", timestamp, uniqueID, baseName, ext)
|
||||
uploadDirAbs := config.ResolvePath(config.Global.UploadDir)
|
||||
inputPath := filepath.Join(uploadDirAbs, inputFileName)
|
||||
|
||||
@@ -182,3 +191,13 @@ func generateFileSequence(dir, timestamp, billType, ext string) string {
|
||||
}
|
||||
return fmt.Sprintf("%03d", len(matches)+1)
|
||||
}
|
||||
|
||||
// generateShortID 生成 6 位随机唯一标识符
|
||||
func generateShortID() string {
|
||||
bytes := make([]byte, 3) // 3 字节 = 6 个十六进制字符
|
||||
if _, err := rand.Read(bytes); err != nil {
|
||||
// 如果随机数生成失败,使用时间纳秒作为备选
|
||||
return fmt.Sprintf("%06x", time.Now().UnixNano()%0xFFFFFF)
|
||||
}
|
||||
return hex.EncodeToString(bytes)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user