Skip to content

Commit

Permalink
fix(utils.go): 修复了下载未缓存文件时文件名错误的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
imxyy1soope1 committed Jul 15, 2023
1 parent d9c32f7 commit 7ae9c9a
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,9 @@ func DownloadFile(url, filename, dirname string) error {
func getCacheUri(songId int64) (uri string, ok bool) {
cacheDir := GetCacheDir()
if !FileOrDirExists(cacheDir) {
_ = os.MkdirAll(cacheDir, os.ModePerm)
if configs.ConfigRegistry.MainCacheLimit != 0 {
_ = os.MkdirAll(cacheDir, os.ModePerm)
}
return
}
files, err := ioutil.ReadDir(cacheDir)
Expand Down Expand Up @@ -272,8 +274,8 @@ func CopyCachedSong(song structs.Song) error {
}
split := strings.Split(path.Base(oldFilename), ".")
musicType := split[len(split)-1]
filename := fmt.Sprintf("%s-%s.%s", song.Name, song.ArtistName(), musicType)
// Windows Linux 均不允许文件名中出现 / \ 替换为 _
filename := fmt.Sprintf("%s-%s.%s", song.Name, song.ArtistName(), musicType)
// Windows Linux 均不允许文件名中出现 / \ 替换为 _
filename = strings.Replace(filename, "/", "_", -1)
targetFilename := path.Join(downloadDir, filename)

Expand Down Expand Up @@ -344,11 +346,11 @@ func SetSongTag(file *os.File, song structs.Song) {
}

func downloadMusic(url, musicType string, song structs.Song, downloadDir string) error {
err := DownloadFile(url, song.Name, downloadDir)
filename := fmt.Sprintf("%s-%s.%s", song.Name, song.ArtistName(), musicType)
err := DownloadFile(url, filename, downloadDir)
if err != nil {
return err
}
filename := path.Join(downloadDir, fmt.Sprintf("%s-%s.%s", song.Name, song.ArtistName(), musicType))
file, _ := os.OpenFile(path.Join(downloadDir, filename), os.O_RDWR, os.ModePerm)
SetSongTag(file, song)
return nil
Expand Down

0 comments on commit 7ae9c9a

Please sign in to comment.