quintodrome/server/subsonic/media_retrieval.go

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

57 lines
1.6 KiB
Go
Raw Normal View History

package subsonic
2016-03-03 08:08:44 -09:00
import (
2016-03-21 16:31:28 -08:00
"io"
"net/http"
2016-03-21 16:31:28 -08:00
"github.com/deluan/navidrome/consts"
"github.com/deluan/navidrome/core"
2020-01-23 15:44:08 -09:00
"github.com/deluan/navidrome/log"
"github.com/deluan/navidrome/model"
"github.com/deluan/navidrome/resources"
2020-01-23 15:44:08 -09:00
"github.com/deluan/navidrome/server/subsonic/responses"
"github.com/deluan/navidrome/utils"
2016-03-03 08:08:44 -09:00
)
2016-03-21 16:31:28 -08:00
type MediaRetrievalController struct {
artwork core.Artwork
2016-03-03 08:08:44 -09:00
}
func NewMediaRetrievalController(artwork core.Artwork) *MediaRetrievalController {
return &MediaRetrievalController{artwork: artwork}
2016-03-03 08:08:44 -09:00
}
func (c *MediaRetrievalController) GetAvatar(w http.ResponseWriter, r *http.Request) (*responses.Subsonic, error) {
2020-10-23 17:37:53 -08:00
f, err := resources.AssetFile().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, err := requiredParamString(r, "id", "id parameter required")
if err != nil {
return nil, err
}
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")
err = c.artwork.Get(r.Context(), id, size, w)
2016-03-03 08:08:44 -09:00
switch {
2020-01-14 18:22:34 -09:00
case err == model.ErrNotFound:
log.Error(r, "Couldn't find coverArt", "id", id, err)
return nil, newError(responses.ErrorDataNotFound, "Artwork not found")
case err != nil:
log.Error(r, "Error retrieving coverArt", "id", id, err)
return nil, newError(responses.ErrorGeneric, "Internal Error")
2016-03-03 08:08:44 -09:00
}
return nil, nil
2016-03-03 08:08:44 -09:00
}