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)
}
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
+4 -16
View File
@@ -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
}
+3 -3
View File
@@ -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
}