Skip to content

Commit

Permalink
fix: #11 pattern contains path separator
Browse files Browse the repository at this point in the history
  • Loading branch information
chaunsin committed Jan 8, 2025
1 parent 2cc4db4 commit 4483e36
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 4 deletions.
13 changes: 9 additions & 4 deletions internal/ncmctl/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -484,8 +484,8 @@ func (c *Download) download(ctx context.Context, cli *api.Client, request *weapi
var (
// drd = downResp.Data[0]
drd = downResp.Data
dest = filepath.Join(c.opts.Output, fmt.Sprintf("%s - %s.%s", music.ArtistString(), music.Name, drd.Type))
tempName = fmt.Sprintf("ncmctl-*-%s.tmp", music.Name)
dest = filepath.Join(c.opts.Output, fmt.Sprintf("%s - %s.%s", music.ArtistString(), music.NameString(), drd.Type))
tempName = fmt.Sprintf("ncmctl-*-%s.tmp", music.NameString())
)

// 创建临时文件
Expand Down Expand Up @@ -536,7 +536,7 @@ func (c *Download) download(ctx context.Context, cli *api.Client, request *weapi

// 避免文件重名
for i := 1; utils.FileExists(dest); i++ {
dest = filepath.Join(c.opts.Output, fmt.Sprintf("%s - %s(%d).%s", music.ArtistString(), music.Name, i, drd.Type))
dest = filepath.Join(c.opts.Output, fmt.Sprintf("%s - %s(%d).%s", music.ArtistString(), music.NameString(), i, drd.Type))
}
if err := os.Rename(file.Name(), dest); err != nil {
_ = os.Remove(file.Name())
Expand All @@ -556,13 +556,18 @@ type Music struct {
Time int64
}

// NameString 返回去除特殊符号的歌曲名
func (m Music) NameString() string {
return utils.Filename(m.Name, "_")
}

func (m Music) ArtistString() string {
if len(m.Artist) <= 0 {
return ""
}
var artistList = make([]string, 0, len(m.Artist))
for _, ar := range m.Artist {
artistList = append(artistList, strings.TrimSpace(ar.Name))
artistList = append(artistList, utils.Filename(ar.Name, "_")) // #11 避免文件名中包含特殊字符
}
return strings.Join(artistList, ",")
}
Expand Down
10 changes: 10 additions & 0 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ const (

var (
parseBytesRegexp = regexp.MustCompile(`(?i)^(\d+)([a-zA-Z]*)$`)
filenameRegexp = regexp.MustCompile("[\\\\/:*?\"<>|]")
unitMap = map[string]int64{
"B": B,
"K": KB,
Expand Down Expand Up @@ -243,3 +244,12 @@ func TimeUntilMidnight(timeZone string) (time.Duration, error) {
midnight := time.Date(now.Year(), now.Month(), now.Day()+1, 0, 0, 0, 0, loc)
return midnight.Sub(now), nil
}

// Filename 清理文件名中的非法字符
func Filename(path string, new ...string) string {
path = strings.TrimSpace(path)
if len(new) > 0 {
return filenameRegexp.ReplaceAllString(path, new[0])
}
return filenameRegexp.ReplaceAllString(path, "")
}
122 changes: 122 additions & 0 deletions pkg/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,3 +266,125 @@ func TestTimeUntilMidnight(t *testing.T) {
})
}
}

func TestFilename(t *testing.T) {
type args struct {
path string
new string
}
tests := []struct {
name string
args args
want string
}{
{
name: "/",
args: args{
path: "path//to/file",
new: "_",
},
want: "path__to_file",
},
{
name: "\\",
args: args{
path: "path\\\\to\\file",
new: "_",
},
want: "path__to_file",
},
{
name: ":",
args: args{
path: "path:to::file",
new: "_",
},
want: "path_to__file",
},
{
name: "*",
args: args{
path: "path*to**file",
new: "_",
},
want: "path_to__file",
},
{
name: "?",
args: args{
path: "path?to??file",
new: "_",
},
want: "path_to__file",
},
{
name: "\"",
args: args{
path: `path"to""file`,
new: "_",
},
want: "path_to__file",
},
{
name: "<",
args: args{
path: "path<to<<file",
new: "_",
},
want: "path_to__file",
},
{
name: ">",
args: args{
path: "path>to>>file",
new: "_",
},
want: "path_to__file",
},
{
name: "|",
args: args{
path: "path|to||file",
new: "_",
},
want: "path_to__file",
},
{
name: "replace empty",
args: args{
path: "path|to||file",
new: "",
},
want: "pathtofile",
},
{
name: "empty1",
args: args{
path: "",
new: "_",
},
want: "",
},
{
name: "empty2",
args: args{
path: "",
new: "",
},
want: "",
},
{
name: "",
args: args{
path: "Empty string",
new: "",
},
want: "Empty string",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, Filename(tt.args.path, tt.args.new), "Filename(%v, %v)", tt.args.path, tt.args.new)
})
}
}

0 comments on commit 4483e36

Please sign in to comment.