mirror of
https://github.com/GopeedLab/gopeed.git
synced 2026-11-09 10:00:27 +00:00
fix: repair invalid filename (#296)
This commit is contained in:
6 files changed
+74
No files matched your search
@@ -183,3 +183,14 @@ func IsExistsFile(path string) bool {
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// ReplaceInvalidFilename replace invalid path characters
|
||||
func ReplaceInvalidFilename(path string) string {
|
||||
if path == "" {
|
||||
return ""
|
||||
}
|
||||
for _, char := range invalidPathChars {
|
||||
path = strings.ReplaceAll(path, char, "_")
|
||||
}
|
||||
return path
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
//go:build !windows
|
||||
// +build !windows
|
||||
|
||||
package util
|
||||
|
||||
var invalidPathChars = []string{`/`, `:`}
|
||||
@@ -178,3 +178,50 @@ func TestIsExistsFile(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestReplaceInvalidFilename(t *testing.T) {
|
||||
type args struct {
|
||||
path string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want string
|
||||
}{
|
||||
{
|
||||
name: "blank",
|
||||
args: args{
|
||||
path: "",
|
||||
},
|
||||
want: "",
|
||||
},
|
||||
{
|
||||
name: "normal",
|
||||
args: args{
|
||||
path: "test.txt",
|
||||
},
|
||||
want: "test.txt",
|
||||
},
|
||||
{
|
||||
name: "case1",
|
||||
args: args{
|
||||
path: "te/st.txt",
|
||||
},
|
||||
want: "te_st.txt",
|
||||
},
|
||||
{
|
||||
name: "case2",
|
||||
args: args{
|
||||
path: "te/st:.txt",
|
||||
},
|
||||
want: "te_st_.txt",
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := ReplaceInvalidFilename(tt.args.path); got != tt.want {
|
||||
t.Errorf("ReplaceInvalidFilename() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
package util
|
||||
|
||||
var invalidPathChars = []string{`\`, `/`, `:`, `*`, `?`, `"`, `<`, `>`, `|`}
|
||||
Reference in new issue
Block a user