fix(media): handle empty media

This commit is contained in:
Mathis Maquenne
2025-09-09 10:05:42 +02:00
parent 3022e226ce
commit 22ddb2408b
4 changed files with 17 additions and 31 deletions
+6 -5
View File
@@ -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) return nil, fmt.Errorf("%s", media.Errors[0].Message)
} }
if len(media.Data) > 0 && len(media.Data[0].Errors) > 0 { if len(media.Data) > 0 && len(media.Data[0].Errors) > 0 {
if media.Data[0].Errors[0].Code == 2002 { if media.Data[0].Errors[0].Code == 2002 {
return nil, fmt.Errorf("invalid track token") 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) 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 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) { func (c *Client) GetMediaStream(ctx context.Context, media *Media, songID string) (io.ReadCloser, error) {
url, err := media.GetURL() url := media.GetURL()
if err != nil {
return nil, err
}
req, err := http.NewRequestWithContext(ctx, "GET", url, nil) req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
if err != nil { if err != nil {
return nil, err return nil, err
+4 -16
View File
@@ -1,9 +1,5 @@
package deezer package deezer
import (
"fmt"
)
type Media struct { type Media struct {
Errors []MediaError `json:"errors"` Errors []MediaError `json:"errors"`
Data []struct { Data []struct {
@@ -31,18 +27,10 @@ type Source struct {
Provider string `json:"provider"` Provider string `json:"provider"`
} }
func (m *Media) GetURL() (string, error) { func (m *Media) GetURL() string {
if len(m.Data) == 0 || len(m.Data[0].Media) == 0 || len(m.Data[0].Media[0].Sources) == 0 { return m.Data[0].Media[0].Sources[0].URL
return "", fmt.Errorf("no media sources found")
} }
return m.Data[0].Media[0].Sources[0].URL, nil func (m *Media) GetFormat() string {
} return m.Data[0].Media[0].Format
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
} }
+3 -3
View File
@@ -48,9 +48,9 @@ func (s *Song) GetTitle() string {
return songTitle 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" ext := "mp3"
if media.Data[0].Media[0].Format == "FLAC" { if mediaFormat == "FLAC" {
ext = "flac" ext = "flac"
} }
trackNumber := "" 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 := 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 return fileName
} }
+4 -7
View File
@@ -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) return nil, fmt.Errorf("failed to fetch media: %w", err)
} }
fileName := song.GetFileName(c.resourceType, song, media) mediaFormat := media.GetFormat()
outputPath := path.Join(outputDir, fileName)
mediaFormat, err := media.GetFormat()
if err != nil {
return nil, fmt.Errorf("failed to get media format: %w", err)
}
if opts.Strict && strings.ToLower(mediaFormat) != opts.Quality { if opts.Strict && strings.ToLower(mediaFormat) != opts.Quality {
return nil, fmt.Errorf("requested quality '%s' not available", 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) dlCtx, cancel := context.WithTimeout(ctx, opts.Timeout)
defer cancel() defer cancel()
fileName := song.GetFileName(c.resourceType, mediaFormat, song)
outputPath := path.Join(outputDir, fileName)
key := crypto.GetKey(c.appConfig.SecretKey, song.ID) key := crypto.GetKey(c.appConfig.SecretKey, song.ID)
if err := c.streamToFile(dlCtx, stream, outputPath, key); err != nil { if err := c.streamToFile(dlCtx, stream, outputPath, key); err != nil {
fileutil.DeleteFile(outputPath) fileutil.DeleteFile(outputPath)