2020-01-19 18:21:44 -05:00
|
|
|
package subsonic
|
2016-03-03 12:08:44 -05:00
|
|
|
|
|
|
|
|
import (
|
2022-12-20 18:49:59 -05:00
|
|
|
"context"
|
2022-09-30 18:54:25 -04:00
|
|
|
"errors"
|
2016-03-21 20:31:28 -04:00
|
|
|
"io"
|
2020-01-07 14:56:26 -05:00
|
|
|
"net/http"
|
2022-12-27 12:54:51 -05:00
|
|
|
"time"
|
2016-03-21 20:31:28 -04:00
|
|
|
|
2020-11-13 14:57:49 -05:00
|
|
|
"github.com/navidrome/navidrome/conf"
|
2020-06-03 09:40:37 -04:00
|
|
|
"github.com/navidrome/navidrome/consts"
|
2025-04-30 12:10:19 +00:00
|
|
|
"github.com/navidrome/navidrome/core/lyrics"
|
2020-01-23 19:44:08 -05:00
|
|
|
"github.com/navidrome/navidrome/log"
|
|
|
|
|
"github.com/navidrome/navidrome/model"
|
2020-05-01 14:35:57 -04:00
|
|
|
"github.com/navidrome/navidrome/resources"
|
2021-10-20 02:03:06 +05:30
|
|
|
"github.com/navidrome/navidrome/server/subsonic/filter"
|
2020-01-23 19:44:08 -05:00
|
|
|
"github.com/navidrome/navidrome/server/subsonic/responses"
|
2021-02-09 11:33:26 -05:00
|
|
|
"github.com/navidrome/navidrome/utils/gravatar"
|
2023-12-21 17:41:09 -05:00
|
|
|
"github.com/navidrome/navidrome/utils/req"
|
2016-03-03 12:08:44 -05:00
|
|
|
)
|
|
|
|
|
|
2022-11-21 12:57:56 -05:00
|
|
|
func (api *Router) GetAvatar(w http.ResponseWriter, r *http.Request) (*responses.Subsonic, error) {
|
2020-11-13 14:57:49 -05:00
|
|
|
if !conf.Server.EnableGravatar {
|
2022-11-21 12:57:56 -05:00
|
|
|
return api.getPlaceHolderAvatar(w, r)
|
2020-11-13 14:57:49 -05:00
|
|
|
}
|
2023-12-21 17:41:09 -05:00
|
|
|
p := req.Params(r)
|
|
|
|
|
username, err := p.String("username")
|
2020-11-13 14:57:49 -05:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
ctx := r.Context()
|
2022-11-21 12:57:56 -05:00
|
|
|
u, err := api.ds.User(ctx).FindByUsername(username)
|
2020-11-13 14:57:49 -05:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
if u.Email == "" {
|
|
|
|
|
log.Warn(ctx, "User needs an email for gravatar to work", "username", username)
|
2022-11-21 12:57:56 -05:00
|
|
|
return api.getPlaceHolderAvatar(w, r)
|
2020-11-13 14:57:49 -05:00
|
|
|
}
|
|
|
|
|
http.Redirect(w, r, gravatar.Url(u.Email, 0), http.StatusFound)
|
|
|
|
|
return nil, nil
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-21 12:57:56 -05:00
|
|
|
func (api *Router) getPlaceHolderAvatar(w http.ResponseWriter, r *http.Request) (*responses.Subsonic, error) {
|
2022-02-10 14:48:41 -05:00
|
|
|
f, err := resources.FS().Open(consts.PlaceholderAvatar)
|
2016-03-21 20:31:28 -04:00
|
|
|
if err != nil {
|
2020-01-08 20:45:07 -05:00
|
|
|
log.Error(r, "Image not found", err)
|
2020-08-13 22:11:18 -04:00
|
|
|
return nil, newError(responses.ErrorDataNotFound, "Avatar image not found")
|
2016-03-21 20:31:28 -04:00
|
|
|
}
|
|
|
|
|
defer f.Close()
|
2020-04-26 12:35:26 -04:00
|
|
|
_, _ = io.Copy(w, f)
|
2020-01-07 14:56:26 -05:00
|
|
|
|
|
|
|
|
return nil, nil
|
2016-03-21 20:31:28 -04:00
|
|
|
}
|
|
|
|
|
|
2022-11-21 12:57:56 -05:00
|
|
|
func (api *Router) GetCoverArt(w http.ResponseWriter, r *http.Request) (*responses.Subsonic, error) {
|
2023-02-02 14:53:28 -05:00
|
|
|
// If context is already canceled, discard request without further processing
|
|
|
|
|
if r.Context().Err() != nil {
|
|
|
|
|
return nil, nil //nolint:nilerr
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-31 16:58:07 -05:00
|
|
|
ctx, cancel := context.WithTimeout(r.Context(), 10*time.Second)
|
|
|
|
|
defer cancel()
|
|
|
|
|
|
2023-12-21 17:41:09 -05:00
|
|
|
p := req.Params(r)
|
|
|
|
|
id, _ := p.String("id")
|
|
|
|
|
size := p.IntOr("size", 0)
|
2024-05-24 20:19:26 -04:00
|
|
|
square := p.BoolOr("square", false)
|
2016-03-03 12:08:44 -05:00
|
|
|
|
2024-05-24 20:19:26 -04:00
|
|
|
imgReader, lastUpdate, err := api.artwork.GetOrPlaceholder(ctx, id, size, square)
|
2022-12-20 18:49:59 -05:00
|
|
|
switch {
|
|
|
|
|
case errors.Is(err, context.Canceled):
|
|
|
|
|
return nil, nil
|
|
|
|
|
case errors.Is(err, model.ErrNotFound):
|
2023-01-31 18:22:49 -05:00
|
|
|
log.Warn(r, "Couldn't find coverArt", "id", id, err)
|
2020-08-13 22:11:18 -04:00
|
|
|
return nil, newError(responses.ErrorDataNotFound, "Artwork not found")
|
2022-12-20 18:49:59 -05:00
|
|
|
case err != nil:
|
2020-01-20 22:30:16 -05:00
|
|
|
log.Error(r, "Error retrieving coverArt", "id", id, err)
|
2020-10-27 15:23:29 -04:00
|
|
|
return nil, err
|
2016-03-03 12:08:44 -05:00
|
|
|
}
|
2020-01-07 14:56:26 -05:00
|
|
|
|
2020-11-17 13:30:37 -05:00
|
|
|
defer imgReader.Close()
|
2025-02-25 08:22:38 -05:00
|
|
|
w.Header().Set("cache-control", "public, max-age=315360000")
|
|
|
|
|
w.Header().Set("last-modified", lastUpdate.Format(time.RFC1123))
|
|
|
|
|
|
2022-12-31 16:58:07 -05:00
|
|
|
cnt, err := io.Copy(w, imgReader)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Warn(ctx, "Error sending image", "count", cnt, err)
|
|
|
|
|
}
|
2020-11-17 13:30:37 -05:00
|
|
|
|
|
|
|
|
return nil, err
|
2016-03-03 12:08:44 -05:00
|
|
|
}
|
2021-10-20 02:03:06 +05:30
|
|
|
|
2022-11-21 12:57:56 -05:00
|
|
|
func (api *Router) GetLyrics(r *http.Request) (*responses.Subsonic, error) {
|
2023-12-21 17:41:09 -05:00
|
|
|
p := req.Params(r)
|
|
|
|
|
artist, _ := p.String("artist")
|
|
|
|
|
title, _ := p.String("title")
|
2021-10-20 02:03:06 +05:30
|
|
|
response := newResponse()
|
2025-04-30 12:10:19 +00:00
|
|
|
lyricsResponse := responses.Lyrics{}
|
|
|
|
|
response.Lyrics = &lyricsResponse
|
2025-06-16 16:04:41 +00:00
|
|
|
mediaFiles, err := api.ds.MediaFile(r.Context()).GetAll(filter.SongsByArtistTitleWithLyricsFirst(artist, title))
|
2021-10-20 02:03:06 +05:30
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-06 19:57:47 -05:00
|
|
|
if len(mediaFiles) == 0 {
|
2021-10-20 02:03:06 +05:30
|
|
|
return response, nil
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-30 12:10:19 +00:00
|
|
|
structuredLyrics, err := lyrics.GetLyrics(r.Context(), &mediaFiles[0])
|
2023-12-28 01:20:29 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(structuredLyrics) == 0 {
|
|
|
|
|
return response, nil
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-30 12:10:19 +00:00
|
|
|
lyricsResponse.Artist = artist
|
|
|
|
|
lyricsResponse.Title = title
|
2021-10-20 02:03:06 +05:30
|
|
|
|
2023-12-28 01:20:29 +00:00
|
|
|
lyricsText := ""
|
|
|
|
|
for _, line := range structuredLyrics[0].Line {
|
|
|
|
|
lyricsText += line.Value + "\n"
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-30 12:10:19 +00:00
|
|
|
lyricsResponse.Value = lyricsText
|
2023-12-28 01:20:29 +00:00
|
|
|
|
|
|
|
|
return response, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (api *Router) GetLyricsBySongId(r *http.Request) (*responses.Subsonic, error) {
|
|
|
|
|
id, err := req.Params(r).String("id")
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mediaFile, err := api.ds.MediaFile(r.Context()).Get(id)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-30 12:10:19 +00:00
|
|
|
structuredLyrics, err := lyrics.GetLyrics(r.Context(), mediaFile)
|
2023-12-28 01:20:29 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
2021-10-20 02:03:06 +05:30
|
|
|
}
|
|
|
|
|
|
2023-12-28 01:20:29 +00:00
|
|
|
response := newResponse()
|
2025-04-30 12:10:19 +00:00
|
|
|
response.LyricsList = buildLyricsList(mediaFile, structuredLyrics)
|
2023-12-28 01:20:29 +00:00
|
|
|
|
2021-10-20 02:03:06 +05:30
|
|
|
return response, nil
|
|
|
|
|
}
|