quintodrome/server/subsonic/media_retrieval.go

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

123 lines
3.4 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"
"github.com/navidrome/navidrome/core"
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
)
2016-03-21 16:31:28 -08:00
type MediaRetrievalController struct {
artwork core.Artwork
2020-11-13 10:57:49 -09:00
ds model.DataStore
2016-03-03 08:08:44 -09:00
}
2020-11-13 10:57:49 -09:00
func NewMediaRetrievalController(artwork core.Artwork, ds model.DataStore) *MediaRetrievalController {
return &MediaRetrievalController{artwork: artwork, ds: ds}
2016-03-03 08:08:44 -09:00
}
func (c *MediaRetrievalController) GetAvatar(w http.ResponseWriter, r *http.Request) (*responses.Subsonic, error) {
2020-11-13 10:57:49 -09:00
if !conf.Server.EnableGravatar {
return c.getPlaceHolderAvatar(w, r)
}
username, err := requiredParamString(r, "username")
if err != nil {
return nil, err
}
ctx := r.Context()
u, err := c.ds.User(ctx).FindByUsername(username)
if err != nil {
return nil, err
}
if u.Email == "" {
log.Warn(ctx, "User needs an email for gravatar to work", "username", username)
return c.getPlaceHolderAvatar(w, r)
}
http.Redirect(w, r, gravatar.Url(u.Email, 0), http.StatusFound)
return nil, nil
}
func (c *MediaRetrievalController) 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 (c *MediaRetrievalController) GetCoverArt(w http.ResponseWriter, r *http.Request) (*responses.Subsonic, error) {
id := utils.ParamStringDefault(r, "id", "non-existent")
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
2020-11-17 09:30:37 -09:00
imgReader, err := c.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 TIMESTAMP_REGEX 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(TIMESTAMP_REGEX)
// Eg: [04:02:50.85]
// [02:50.85]
// [02:50]
return r.MatchString(rawLyrics)
}
func (c *MediaRetrievalController) GetLyrics(w http.ResponseWriter, r *http.Request) (*responses.Subsonic, error) {
artist := utils.ParamString(r, "artist")
title := utils.ParamString(r, "title")
response := newResponse()
lyrics := responses.Lyrics{}
response.Lyrics = &lyrics
media_files, err := c.ds.MediaFile(r.Context()).GetAll(filter.SongsWithLyrics(artist, title))
if err != nil {
return nil, err
}
if len(media_files) == 0 {
return response, nil
}
lyrics.Artist = artist
lyrics.Title = title
if isSynced(media_files[0].Lyrics) {
r := regexp.MustCompile(TIMESTAMP_REGEX)
lyrics.Value = r.ReplaceAllString(media_files[0].Lyrics, "")
} else {
lyrics.Value = media_files[0].Lyrics
}
return response, nil
}