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