fix(media): handle empty media
This commit is contained in:
@@ -137,6 +137,7 @@ func (c *Client) FetchMedia(ctx context.Context, song *Song, quality string) (*M
|
||||
|
||||
return nil, fmt.Errorf("%s", media.Errors[0].Message)
|
||||
}
|
||||
|
||||
if len(media.Data) > 0 && len(media.Data[0].Errors) > 0 {
|
||||
if media.Data[0].Errors[0].Code == 2002 {
|
||||
return nil, fmt.Errorf("invalid track token")
|
||||
@@ -145,6 +146,10 @@ func (c *Client) FetchMedia(ctx context.Context, song *Song, quality string) (*M
|
||||
return nil, fmt.Errorf("%s", media.Data[0].Errors[0].Message)
|
||||
}
|
||||
|
||||
if len(media.Data) == 0 || len(media.Data[0].Media) == 0 || len(media.Data[0].Media[0].Sources) == 0 {
|
||||
return nil, fmt.Errorf("no sources found")
|
||||
}
|
||||
|
||||
return &media, nil
|
||||
}
|
||||
|
||||
@@ -169,11 +174,7 @@ func (c *Client) FetchCoverImage(ctx context.Context, song *Song) ([]byte, error
|
||||
}
|
||||
|
||||
func (c *Client) GetMediaStream(ctx context.Context, media *Media, songID string) (io.ReadCloser, error) {
|
||||
url, err := media.GetURL()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
url := media.GetURL()
|
||||
req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
@@ -1,9 +1,5 @@
|
||||
package deezer
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type Media struct {
|
||||
Errors []MediaError `json:"errors"`
|
||||
Data []struct {
|
||||
@@ -31,18 +27,10 @@ type Source struct {
|
||||
Provider string `json:"provider"`
|
||||
}
|
||||
|
||||
func (m *Media) GetURL() (string, error) {
|
||||
if len(m.Data) == 0 || len(m.Data[0].Media) == 0 || len(m.Data[0].Media[0].Sources) == 0 {
|
||||
return "", fmt.Errorf("no media sources found")
|
||||
}
|
||||
|
||||
return m.Data[0].Media[0].Sources[0].URL, nil
|
||||
func (m *Media) GetURL() string {
|
||||
return m.Data[0].Media[0].Sources[0].URL
|
||||
}
|
||||
|
||||
func (m *Media) GetFormat() (string, error) {
|
||||
if len(m.Data) == 0 || len(m.Data[0].Media) == 0 {
|
||||
return "", fmt.Errorf("no media format found")
|
||||
}
|
||||
|
||||
return m.Data[0].Media[0].Format, nil
|
||||
func (m *Media) GetFormat() string {
|
||||
return m.Data[0].Media[0].Format
|
||||
}
|
||||
|
||||
@@ -48,9 +48,9 @@ func (s *Song) GetTitle() string {
|
||||
return songTitle
|
||||
}
|
||||
|
||||
func (s *Song) GetFileName(resourceType string, song *Song, media *Media) string {
|
||||
func (s *Song) GetFileName(resourceType, mediaFormat string, song *Song) string {
|
||||
ext := "mp3"
|
||||
if media.Data[0].Media[0].Format == "FLAC" {
|
||||
if mediaFormat == "FLAC" {
|
||||
ext = "flac"
|
||||
}
|
||||
trackNumber := ""
|
||||
@@ -59,7 +59,7 @@ func (s *Song) GetFileName(resourceType string, song *Song, media *Media) string
|
||||
}
|
||||
|
||||
fileName := fmt.Sprintf("%s%s - %s.%s", trackNumber, s.Artist, s.GetTitle(), ext)
|
||||
fileName, _ = filenamify.Filenamify(fileName, filenamify.Options{})
|
||||
fileName, _ = filenamify.Filenamify(fileName, filenamify.Options{MaxLength: 255})
|
||||
|
||||
return fileName
|
||||
}
|
||||
|
||||
@@ -172,13 +172,7 @@ func (c *Client) downloadSong(ctx context.Context, resource deezer.Resource, son
|
||||
return nil, fmt.Errorf("failed to fetch media: %w", err)
|
||||
}
|
||||
|
||||
fileName := song.GetFileName(c.resourceType, song, media)
|
||||
outputPath := path.Join(outputDir, fileName)
|
||||
|
||||
mediaFormat, err := media.GetFormat()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get media format: %w", err)
|
||||
}
|
||||
mediaFormat := media.GetFormat()
|
||||
if opts.Strict && strings.ToLower(mediaFormat) != opts.Quality {
|
||||
return nil, fmt.Errorf("requested quality '%s' not available", opts.Quality)
|
||||
}
|
||||
@@ -211,6 +205,9 @@ func (c *Client) downloadSong(ctx context.Context, resource deezer.Resource, son
|
||||
dlCtx, cancel := context.WithTimeout(ctx, opts.Timeout)
|
||||
defer cancel()
|
||||
|
||||
fileName := song.GetFileName(c.resourceType, mediaFormat, song)
|
||||
outputPath := path.Join(outputDir, fileName)
|
||||
|
||||
key := crypto.GetKey(c.appConfig.SecretKey, song.ID)
|
||||
if err := c.streamToFile(dlCtx, stream, outputPath, key); err != nil {
|
||||
fileutil.DeleteFile(outputPath)
|
||||
|
||||
Reference in New Issue
Block a user