Skip to content

Commit

Permalink
对下载歌曲模块添加防护 (#42)
Browse files Browse the repository at this point in the history
下载歌曲前对内容进行验证,避免下到html
  • Loading branch information
vatebur authored Dec 17, 2023
1 parent aefd1d5 commit 062dc31
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions neteasemusic/neteasemusic.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/url"
"os"
"strconv"
"strings"

"github.com/FloatTech/floatbox/binary"
"github.com/FloatTech/floatbox/file"
Expand Down Expand Up @@ -138,6 +139,13 @@ func DownloadMusic(musicID int, musicName, pathOfMusic string) error {
if response.StatusCode != 200 {
return errors.Errorf("Status Code: %d", response.StatusCode)
}

// 检查 Content-Type 是否为 HTML
contentType := response.Header.Get("Content-Type")
if strings.HasPrefix(contentType, "text/html") {
return errors.New("URL points to an HTML page instead of an MP3 file")
}

// 下载歌曲
err = file.DownloadTo(musicURL, downMusic)
process.SleepAbout1sTo2s()
Expand Down

0 comments on commit 062dc31

Please sign in to comment.