navidrome/server/subsonic/media_retrieval.go

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

132 lines
3.5 KiB
Go
Raw Normal View History

package subsonic
2016-03-03 18:08:44 +01:00
import (
2022-12-21 00:49:59 +01:00
"context"
2022-10-01 00:54:25 +02:00
"errors"
2016-03-22 01:31:28 +01:00
"io"
"net/http"
"regexp"
2022-12-27 18:54:51 +01:00
"time"
2016-03-22 01:31:28 +01:00
2020-11-13 20:57:49 +01:00
"github.com/navidrome/navidrome/conf"
"github.com/navidrome/navidrome/consts"
2020-01-24 01:44:08 +01:00
"github.com/navidrome/navidrome/log"
"github.com/navidrome/navidrome/model"
"github.com/navidrome/navidrome/resources"
"github.com/navidrome/navidrome/server/subsonic/filter"
2020-01-24 01:44:08 +01:00
"github.com/navidrome/navidrome/server/subsonic/responses"
"github.com/navidrome/navidrome/utils/gravatar"
"github.com/navidrome/navidrome/utils/req"
2016-03-03 18:08:44 +01:00
)
func (api *Router) GetAvatar(w http.ResponseWriter, r *http.Request) (*responses.Subsonic, error) {
2020-11-13 20:57:49 +01:00
if !conf.Server.EnableGravatar {
return api.getPlaceHolderAvatar(w, r)
2020-11-13 20:57:49 +01:00
}
p := req.Params(r)
username, err := p.String("username")
2020-11-13 20:57:49 +01:00
if err != nil {
return nil, err
}
ctx := r.Context()
u, err := api.ds.User(ctx).FindByUsername(username)
2020-11-13 20:57:49 +01:00
if err != nil {
return nil, err
}
if u.Email == "" {
log.Warn(ctx, "User needs an email for gravatar to work", "username", username)
return api.getPlaceHolderAvatar(w, r)
2020-11-13 20:57:49 +01:00
}
http.Redirect(w, r, gravatar.Url(u.Email, 0), http.StatusFound)
return nil, nil
}
func (api *Router) getPlaceHolderAvatar(w http.ResponseWriter, r *http.Request) (*responses.Subsonic, error) {
f, err := resources.FS().Open(consts.PlaceholderAvatar)
2016-03-22 01:31:28 +01:00
if err != nil {
2020-01-09 02:45:07 +01:00
log.Error(r, "Image not found", err)
return nil, newError(responses.ErrorDataNotFound, "Avatar image not found")
2016-03-22 01:31:28 +01:00
}
defer f.Close()
2020-04-26 18:35:26 +02:00
_, _ = io.Copy(w, f)
return nil, nil
2016-03-22 01:31:28 +01:00
}
func (api *Router) GetCoverArt(w http.ResponseWriter, r *http.Request) (*responses.Subsonic, error) {
// If context is already canceled, discard request without further processing
if r.Context().Err() != nil {
return nil, nil //nolint:nilerr
}
2022-12-31 22:58:07 +01:00
ctx, cancel := context.WithTimeout(r.Context(), 10*time.Second)
defer cancel()
p := req.Params(r)
id, _ := p.String("id")
size := p.IntOr("size", 0)
2016-03-03 18:08:44 +01:00
imgReader, lastUpdate, err := api.artwork.GetOrPlaceholder(ctx, id, size)
2020-06-04 20:45:00 +02:00
w.Header().Set("cache-control", "public, max-age=315360000")
2022-12-27 18:54:51 +01:00
w.Header().Set("last-modified", lastUpdate.Format(time.RFC1123))
2016-03-03 18:08:44 +01:00
2022-12-21 00:49:59 +01:00
switch {
case errors.Is(err, context.Canceled):
return nil, nil
case errors.Is(err, model.ErrNotFound):
log.Warn(r, "Couldn't find coverArt", "id", id, err)
return nil, newError(responses.ErrorDataNotFound, "Artwork not found")
2022-12-21 00:49:59 +01:00
case err != nil:
log.Error(r, "Error retrieving coverArt", "id", id, err)
2020-10-27 20:23:29 +01:00
return nil, err
2016-03-03 18:08:44 +01:00
}
2020-11-17 19:30:37 +01:00
defer imgReader.Close()
2022-12-31 22:58:07 +01:00
cnt, err := io.Copy(w, imgReader)
if err != nil {
log.Warn(ctx, "Error sending image", "count", cnt, err)
}
2020-11-17 19:30:37 +01:00
return nil, err
2016-03-03 18:08:44 +01:00
}
const timeStampRegex string = `(\[([0-9]{1,2}:)?([0-9]{1,2}:)([0-9]{1,2})(\.[0-9]{1,2})?\])`
func isSynced(rawLyrics string) bool {
r := regexp.MustCompile(timeStampRegex)
// Eg: [04:02:50.85]
// [02:50.85]
// [02:50]
return r.MatchString(rawLyrics)
}
func (api *Router) GetLyrics(r *http.Request) (*responses.Subsonic, error) {
p := req.Params(r)
artist, _ := p.String("artist")
title, _ := p.String("title")
response := newResponse()
lyrics := responses.Lyrics{}
response.Lyrics = &lyrics
mediaFiles, err := api.ds.MediaFile(r.Context()).GetAll(filter.SongsWithLyrics(artist, title))
if err != nil {
return nil, err
}
if len(mediaFiles) == 0 {
return response, nil
}
lyrics.Artist = artist
lyrics.Title = title
if isSynced(mediaFiles[0].Lyrics) {
r := regexp.MustCompile(timeStampRegex)
lyrics.Value = r.ReplaceAllString(mediaFiles[0].Lyrics, "")
} else {
lyrics.Value = mediaFiles[0].Lyrics
}
return response, nil
}