quintodrome/server/subsonic/media_retrieval.go

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

113 lines
3 KiB
Go
Raw Normal View History

package subsonic
2016-03-03 08:08:44 -09:00
import (
2022-09-30 14:54:25 -08:00
"errors"
2016-03-21 16:31:28 -08:00
"io"
"net/http"
"regexp"
2016-03-21 16:31:28 -08:00
2020-11-13 10:57:49 -09:00
"github.com/navidrome/navidrome/conf"
"github.com/navidrome/navidrome/consts"
2020-01-23 15:44:08 -09: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-23 15:44:08 -09:00
"github.com/navidrome/navidrome/server/subsonic/responses"
"github.com/navidrome/navidrome/utils"
"github.com/navidrome/navidrome/utils/gravatar"
2016-03-03 08:08:44 -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 {
return api.getPlaceHolderAvatar(w, r)
2020-11-13 10:57:49 -09:00
}
username, err := requiredParamString(r, "username")
if err != nil {
return nil, err
}
ctx := r.Context()
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)
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
}
func (api *Router) getPlaceHolderAvatar(w http.ResponseWriter, r *http.Request) (*responses.Subsonic, error) {
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)
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)
return nil, nil
2016-03-21 16:31:28 -08:00
}
func (api *Router) GetCoverArt(w http.ResponseWriter, r *http.Request) (*responses.Subsonic, error) {
id := utils.ParamString(r, "id")
size := utils.ParamInt(r, "size", 0)
2016-03-03 08:08:44 -09:00
2020-06-04 10:45:00 -08:00
w.Header().Set("cache-control", "public, max-age=315360000")
2016-03-03 08:08:44 -09:00
imgReader, err := api.artwork.Get(r.Context(), id, size)
2022-09-30 14:54:25 -08:00
if errors.Is(err, model.ErrNotFound) {
log.Error(r, "Couldn't find coverArt", "id", id, err)
return nil, newError(responses.ErrorDataNotFound, "Artwork not found")
2022-09-30 14:54:25 -08:00
}
if err != nil {
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-11-17 09:30:37 -09:00
defer imgReader.Close()
_, err = io.Copy(w, imgReader)
return nil, err
2016-03-03 08:08:44 -09: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) {
artist := utils.ParamString(r, "artist")
title := utils.ParamString(r, "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
}