diff --git a/pkg/download/downloader.go b/pkg/download/downloader.go index a8cc5349a..3d20c9a8a 100644 --- a/pkg/download/downloader.go +++ b/pkg/download/downloader.go @@ -725,8 +725,10 @@ 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 { @@ -734,6 +736,7 @@ func (d *Downloader) doStart(task *Task) (err error) { } 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 { diff --git a/pkg/download/extension.go b/pkg/download/extension.go index 772da1d88..d2a766f6d 100644 --- a/pkg/download/extension.go +++ b/pkg/download/extension.go @@ -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 }, ) diff --git a/pkg/util/path.go b/pkg/util/path.go index d70806943..a6a78a9d2 100644 --- a/pkg/util/path.go +++ b/pkg/util/path.go @@ -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 +} diff --git a/pkg/util/path_other.go b/pkg/util/path_other.go new file mode 100644 index 000000000..4d4b29295 --- /dev/null +++ b/pkg/util/path_other.go @@ -0,0 +1,6 @@ +//go:build !windows +// +build !windows + +package util + +var invalidPathChars = []string{`/`, `:`} diff --git a/pkg/util/path_test.go b/pkg/util/path_test.go index 0f8f4c379..13d558dff 100644 --- a/pkg/util/path_test.go +++ b/pkg/util/path_test.go @@ -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) + } + }) + } +} diff --git a/pkg/util/path_windows.go b/pkg/util/path_windows.go new file mode 100644 index 000000000..c054c528e --- /dev/null +++ b/pkg/util/path_windows.go @@ -0,0 +1,3 @@ +package util + +var invalidPathChars = []string{`\`, `/`, `:`, `*`, `?`, `"`, `<`, `>`, `|`}