Skip to content

Commit

Permalink
fix: repair invalid filename (GopeedLab#296)
Browse files Browse the repository at this point in the history
  • Loading branch information
monkeyWie authored Dec 12, 2023
1 parent f2af61f commit ff32971
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 0 deletions.
3 changes: 3 additions & 0 deletions pkg/download/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -725,15 +725,18 @@ func (d *Downloader) doStart(task *Task) (err error) {
if isCreate {
d.checkDuplicateLock.Lock()
defer d.checkDuplicateLock.Unlock()
task.Meta.Opts.Name = util.ReplaceInvalidFilename(task.Meta.Opts.Name)
// check if the download file is duplicated and rename it automatically.
if task.Meta.Res.Name != "" {
task.Meta.Res.Name = util.ReplaceInvalidFilename(task.Meta.Res.Name)
fullDirPath := task.Meta.FolderPath()
newName, err := util.CheckDuplicateAndRename(fullDirPath)
if err != nil {
return err
}
task.Meta.Opts.Name = newName
} else {
task.Meta.Res.Files[0].Name = util.ReplaceInvalidFilename(task.Meta.Res.Files[0].Name)
fullFilePath := task.Meta.SingleFilepath()
newName, err := util.CheckDuplicateAndRename(fullFilePath)
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions pkg/download/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,10 @@ func (d *Downloader) triggerOnResolve(req *base.Request) (res *base.Resource) {
}
ctx.Res.CalcSize(nil)
}
ctx.Res.Name = util.ReplaceInvalidFilename(ctx.Res.Name)
for _, file := range ctx.Res.Files {
file.Name = util.ReplaceInvalidFilename(file.Name)
}
res = ctx.Res
},
)
Expand Down
11 changes: 11 additions & 0 deletions pkg/util/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
6 changes: 6 additions & 0 deletions pkg/util/path_other.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
//go:build !windows
// +build !windows

package util

var invalidPathChars = []string{`/`, `:`}
47 changes: 47 additions & 0 deletions pkg/util/path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
}
}
3 changes: 3 additions & 0 deletions pkg/util/path_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package util

var invalidPathChars = []string{`\`, `/`, `:`, `*`, `?`, `"`, `<`, `>`, `|`}

0 comments on commit ff32971

Please sign in to comment.