Skip to content

Commit

Permalink
fix(controller): 替换文件名中的非法字符
Browse files Browse the repository at this point in the history
- 在 pod_file.go 中添加文件名非法字符替换逻辑
- 在 strings.go 中新增 SanitizeFileName 函数用于去除非法字符
- 非法字符包括 \ / : * ? "< > | 以及小括号 ()- 所有非法字符将被替换为下划线 '_'
  • Loading branch information
weibaohui committed Oct 19, 2024
1 parent 9c58db9 commit 38ee667
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
12 changes: 12 additions & 0 deletions internal/utils/strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package utils

import (
"bytes"
"regexp"
"strconv"
"strings"
"unicode/utf8"
Expand Down Expand Up @@ -97,3 +98,14 @@ func IsTextFile(ob []byte) (bool, error) {

return true, nil
}

// SanitizeFileName 去除文件名中的非法字符,并替换为下划线 '_'
func SanitizeFileName(filename string) string {
// 定义非法字符的正则表达式,包括 \ / : * ? " < > | 以及小括号 ()
reg := regexp.MustCompile(`[\\/:*?"<>|()]+`)

// 替换非法字符为下划线 '_'
sanitizedFilename := reg.ReplaceAllString(filename, "_")

return sanitizedFilename
}
3 changes: 2 additions & 1 deletion pkg/controller/pod/pod_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,8 @@ func UploadFile(c *gin.Context) {
amis.WriteJsonError(c, fmt.Errorf("路径不能为空"))
return
}
// 删除path最后一个/后面的内容,取当前选中文件的父级路径
// 替换FileName中非法字符
info.FileName = utils.SanitizeFileName(info.FileName)

pf := kubectl.PodFileInfo{
Namespace: info.Namespace,
Expand Down

0 comments on commit 38ee667

Please sign in to comment.