Skip to content

Commit 062dc31

Browse files
author
vatebur
authored
对下载歌曲模块添加防护 (FloatTech#42)
下载歌曲前对内容进行验证,避免下到html
1 parent aefd1d5 commit 062dc31

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

neteasemusic/neteasemusic.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"net/url"
88
"os"
99
"strconv"
10+
"strings"
1011

1112
"github.com/FloatTech/floatbox/binary"
1213
"github.com/FloatTech/floatbox/file"
@@ -138,6 +139,13 @@ func DownloadMusic(musicID int, musicName, pathOfMusic string) error {
138139
if response.StatusCode != 200 {
139140
return errors.Errorf("Status Code: %d", response.StatusCode)
140141
}
142+
143+
// 检查 Content-Type 是否为 HTML
144+
contentType := response.Header.Get("Content-Type")
145+
if strings.HasPrefix(contentType, "text/html") {
146+
return errors.New("URL points to an HTML page instead of an MP3 file")
147+
}
148+
141149
// 下载歌曲
142150
err = file.DownloadTo(musicURL, downMusic)
143151
process.SleepAbout1sTo2s()

0 commit comments

Comments
 (0)