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"
|
2021-10-20 02:03:06 +05:30
|
|
|
"regexp"
|
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"
|
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"
|
2020-02-06 18:55:38 -05:00
|
|
|
"github.com/navidrome/navidrome/utils"
|
2021-02-09 11:33:26 -05:00
|
|
|
"github.com/navidrome/navidrome/utils/gravatar"
|
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
|
|
|
}
|
|
|
|
|
username, err := requiredParamString(r, "username")
|
|
|
|
|
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) {
|
2022-12-31 16:58:07 -05:00
|
|
|
ctx, cancel := context.WithTimeout(r.Context(), 10*time.Second)
|
|
|
|
|
defer cancel()
|
|
|
|
|
|
2022-12-20 11:34:01 -05:00
|
|
|
id := utils.ParamString(r, "id")
|
2020-02-06 18:55:38 -05:00
|
|
|
size := utils.ParamInt(r, "size", 0)
|
2016-03-03 12:08:44 -05:00
|
|
|
|
2022-12-31 16:58:07 -05:00
|
|
|
imgReader, lastUpdate, err := api.artwork.Get(ctx, id, size)
|
2020-06-04 14:45:00 -04:00
|
|
|
w.Header().Set("cache-control", "public, max-age=315360000")
|
2022-12-27 12:54:51 -05:00
|
|
|
w.Header().Set("last-modified", lastUpdate.Format(time.RFC1123))
|
2016-03-03 12:08:44 -05:00
|
|
|
|
2022-12-20 18:49:59 -05:00
|
|
|
switch {
|
|
|
|
|
case errors.Is(err, context.Canceled):
|
|
|
|
|
return nil, nil
|
|
|
|
|
case errors.Is(err, model.ErrNotFound):
|
2020-01-20 22:30:16 -05:00
|
|
|
log.Error(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()
|
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-12-06 19:57:47 -05:00
|
|
|
const timeStampRegex string = `(\[([0-9]{1,2}:)?([0-9]{1,2}:)([0-9]{1,2})(\.[0-9]{1,2})?\])`
|
2021-10-20 02:03:06 +05:30
|
|
|
|
|
|
|
|
func isSynced(rawLyrics string) bool {
|
2022-12-06 19:57:47 -05:00
|
|
|
r := regexp.MustCompile(timeStampRegex)
|
2021-10-20 02:03:06 +05:30
|
|
|
// Eg: [04:02:50.85]
|
|
|
|
|
// [02:50.85]
|
|
|
|
|
// [02:50]
|
|
|
|
|
return r.MatchString(rawLyrics)
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-21 12:57:56 -05:00
|
|
|
func (api *Router) GetLyrics(r *http.Request) (*responses.Subsonic, error) {
|
2021-10-20 02:03:06 +05:30
|
|
|
artist := utils.ParamString(r, "artist")
|
|
|
|
|
title := utils.ParamString(r, "title")
|
|
|
|
|
response := newResponse()
|
|
|
|
|
lyrics := responses.Lyrics{}
|
|
|
|
|
response.Lyrics = &lyrics
|
2022-12-06 19:57:47 -05:00
|
|
|
mediaFiles, err := api.ds.MediaFile(r.Context()).GetAll(filter.SongsWithLyrics(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
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
lyrics.Artist = artist
|
|
|
|
|
lyrics.Title = title
|
|
|
|
|
|
2022-12-06 19:57:47 -05:00
|
|
|
if isSynced(mediaFiles[0].Lyrics) {
|
|
|
|
|
r := regexp.MustCompile(timeStampRegex)
|
|
|
|
|
lyrics.Value = r.ReplaceAllString(mediaFiles[0].Lyrics, "")
|
2021-10-20 02:03:06 +05:30
|
|
|
} else {
|
2022-12-06 19:57:47 -05:00
|
|
|
lyrics.Value = mediaFiles[0].Lyrics
|
2021-10-20 02:03:06 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return response, nil
|
|
|
|
|
}
|