package jellyfin
import (
"context"
"net/http"
"time"
"github.com/navidrome/navidrome/log"
"github.com/navidrome/navidrome/model"
"github.com/navidrome/navidrome/server/jellyfin/dto"
)
const lyricsLoadTimeout = time.Minute
func (api *Router) cachedLyrics(ctx context.Context, mf *model.MediaFile) model.LyricList {
loadCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), lyricsLoadTimeout)
defer cancel()
list, err := api.lyricsCache.GetWithLoader(mf.ID, func(string) (model.LyricList, time.Duration, error) {
l, err := api.lyrics.GetLyrics(loadCtx, mf)
return l, 0, err
})
if err != nil {
log.Error(ctx, "Error getting lyrics", "id", mf.ID, "title", mf.Title, err)
return nil
}
return list
}
func (api *Router) getLyrics(w http.ResponseWriter, r *http.Request) {
mf, ok := api.mediaFileForRequest(w, r)
if !ok {
return
}
main, found := servableLyric(api.cachedLyrics(r.Context(), mf))
if !found {
http.Error(w, "Not Found", http.StatusNotFound)
return
}
api.ok(w, r, dto.LyricDtoFromLyrics(*mf, main))
}
func servableLyric(list model.LyricList) (model.Lyrics, bool) {
main, found := list.Main()
return main, found && !main.IsEmpty()
}