fix: 文件上传名称去除空格

This commit is contained in:
TsMask
2024-03-30 20:31:59 +08:00
parent 7fa6b81f12
commit 349f73d883

View File

@@ -60,9 +60,12 @@ func uploadWhiteList() []string {
// fileName 原始文件名称含后缀logo.png
func generateFileName(fileName string) string {
fileExt := filepath.Ext(fileName)
// 替换掉后缀和特殊字符保留文件名
// 去除后缀
newFileName := regular.Replace(fileName, fileExt, "")
newFileName = regular.Replace(newFileName, `[<>:"\\|?*]+`, "")
// 去除非法字符
newFileName = regular.Replace(newFileName, `[\\/:*?"<>|]`, "")
// 去除空格
newFileName = regular.Replace(newFileName, `\s`, "_")
newFileName = strings.TrimSpace(newFileName)
return fmt.Sprintf("%s_%s%s", newFileName, generate.Code(6), fileExt)
}