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-01-24 01:44:08 +01:00
|
|
|
"github.com/deluan/navidrome/engine"
|
|
|
|
"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 {
|
2016-03-09 05:05:54 +01:00
|
|
|
cover engine.Cover
|
2016-03-03 18:08:44 +01:00
|
|
|
}
|
|
|
|
|
2020-01-07 20:56:26 +01:00
|
|
|
func NewMediaRetrievalController(cover engine.Cover) *MediaRetrievalController {
|
|
|
|
return &MediaRetrievalController{cover: cover}
|
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-05-01 20:35:57 +02:00
|
|
|
f, err := resources.AssetFile().Open("navidrome-310x310.png")
|
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-01-07 20:56:26 +01: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) {
|
|
|
|
id, err := RequiredParamString(r, "id", "id parameter required")
|
|
|
|
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-02-15 20:20:04 +01:00
|
|
|
w.Header().Set("cache-control", "public, max-age=300")
|
2020-01-22 14:32:31 +01:00
|
|
|
err = c.cover.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-01-07 20:56:26 +01:00
|
|
|
return nil, NewError(responses.ErrorDataNotFound, "Cover 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-01-07 20:56:26 +01: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
|
|
|
}
|