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