🐛 下载地址返回

This commit is contained in:
sugarBecomer
2025-11-09 00:58:59 +08:00
parent 194065af8d
commit 4a29772de0
2 changed files with 5 additions and 31 deletions
+2 -7
View File
@@ -46,7 +46,7 @@ func (c *Downloader) Search(ctx context.Context, keyword string) (result []Music
return
}
func (c *Downloader) Download(ctx context.Context, url string) (result []byte) {
func (c *Downloader) Download(ctx context.Context, url string) string {
client := httpclient.Music.Clone(ctx)
response, err := client.R().Get("https://www.gequbao.net" + url)
if err != nil {
@@ -64,10 +64,5 @@ func (c *Downloader) Download(ctx context.Context, url string) (result []byte) {
download_url := gjson.Get(post.String(), "data.url").String()
// 下载音频文件
downloadResp, err := client.R().Get(download_url)
if err != nil {
utils.OkOrPanic(errors.NewBusinessError(503, "下载出错3"), oops.WithContext(ctx).Wrap)
}
return downloadResp.Bytes()
return download_url
}
+3 -24
View File
@@ -2,12 +2,9 @@ package music
import (
"context"
"fmt"
v1 "likedream-goframe/internal/api/v1"
"likedream-goframe/internal/application/music"
"likedream-goframe/internal/utils"
"github.com/gogf/gf/v2/net/ghttp"
)
type MusicCtrlV1 struct {
@@ -25,25 +22,7 @@ func (c *MusicCtrlV1) Search(ctx context.Context, req *v1.SearchMusicReq) (res *
func (c *MusicCtrlV1) Download(ctx context.Context, req *v1.DownloadMusicReq) (res *utils.Response[any], err error) {
defer utils.Pass(&err)
// 获取音频二进制数据
audioData := music.MusicDownloader.Download(ctx, req.Path)
// 从 context 获取 ghttp.Request
r := ghttp.RequestFromCtx(ctx)
// 构建文件名:Name - Singer.mp3
filename := fmt.Sprintf("music.mp3")
// 设置响应头
r.Response.Header().Set("Content-Type", "audio/mpeg")
r.Response.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"", filename))
r.Response.Header().Set("Content-Length", fmt.Sprintf("%d", len(audioData)))
// 写入二进制数据
r.Response.Write(audioData)
// 标记已处理,防止框架再次处理响应
r.ExitAll()
return nil, nil
// 获取音频下载地址
downloadUrl := music.MusicDownloader.Download(ctx, req.Path)
return utils.SuccessWithData[any](downloadUrl), nil
}