From 38ee667958cac499086fa7e1b3ab92a2e4d8f847 Mon Sep 17 00:00:00 2001 From: weibaohui Date: Sat, 19 Oct 2024 21:05:30 +0800 Subject: [PATCH] =?UTF-8?q?fix(controller):=20=E6=9B=BF=E6=8D=A2=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E5=90=8D=E4=B8=AD=E7=9A=84=E9=9D=9E=E6=B3=95=E5=AD=97?= =?UTF-8?q?=E7=AC=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在 pod_file.go 中添加文件名非法字符替换逻辑 - 在 strings.go 中新增 SanitizeFileName 函数用于去除非法字符 - 非法字符包括 \ / : * ? "< > | 以及小括号 ()- 所有非法字符将被替换为下划线 '_' --- internal/utils/strings.go | 12 ++++++++++++ pkg/controller/pod/pod_file.go | 3 ++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/internal/utils/strings.go b/internal/utils/strings.go index a94d9c3..67689e1 100644 --- a/internal/utils/strings.go +++ b/internal/utils/strings.go @@ -2,6 +2,7 @@ package utils import ( "bytes" + "regexp" "strconv" "strings" "unicode/utf8" @@ -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 +} diff --git a/pkg/controller/pod/pod_file.go b/pkg/controller/pod/pod_file.go index 10d10cc..bc5fe30 100644 --- a/pkg/controller/pod/pod_file.go +++ b/pkg/controller/pod/pod_file.go @@ -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,