navidrome/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 18:08:44 +01:00
import (
2016-03-22 01:31:28 +01:00
"io"
"net/http"
2016-03-22 01:31:28 +01:00
"github.com/deluan/navidrome/consts"
"github.com/deluan/navidrome/core"
2020-01-24 01:44:08 +01:00
"github.com/deluan/navidrome/log"
"github.com/deluan/navidrome/model"
"github.com/deluan/navidrome/resources"
2020-01-24 01:44:08 +01:00
"github.com/deluan/navidrome/server/subsonic/responses"
"github.com/deluan/navidrome/utils"
2016-03-03 18:08:44 +01:00
)
2016-03-22 01:31:28 +01:00
type MediaRetrievalController struct {
artwork core.Artwork
2016-03-03 18:08:44 +01:00
}
func NewMediaRetrievalController(artwork core.Artwork) *MediaRetrievalController {
return &MediaRetrievalController{artwork: artwork}
2016-03-03 18:08:44 +01:00
}
func (c *MediaRetrievalController) GetAvatar(w http.ResponseWriter, r *http.Request) (*responses.Subsonic, error) {
2020-10-24 03:37:53 +02:00
f, err := resources.AssetFile().Open(consts.PlaceholderAvatar)
2016-03-22 01:31:28 +01:00
if err != nil {
2020-01-09 02:45:07 +01:00
log.Error(r, "Image not found", err)
return nil, newError(responses.ErrorDataNotFound, "Avatar image not found")
2016-03-22 01:31:28 +01:00
}
defer f.Close()
2020-04-26 18:35:26 +02:00
_, _ = io.Copy(w, f)
return nil, nil
2016-03-22 01:31:28 +01: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 18:08:44 +01:00
2020-06-04 20:45:00 +02:00
w.Header().Set("cache-control", "public, max-age=315360000")
err = c.artwork.Get(r.Context(), id, size, w)
2016-03-03 18:08:44 +01:00
switch {
2020-01-15 04:22:34 +01: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 18:08:44 +01:00
}
return nil, nil
2016-03-03 18:08:44 +01:00
}