2020-01-20 00:21:44 +01:00
|
|
|
package subsonic
|
2016-03-03 20:46:19 +01:00
|
|
|
|
|
|
|
import (
|
2020-01-07 20:56:26 +01:00
|
|
|
"net/http"
|
2020-01-22 16:19:13 +01:00
|
|
|
"strconv"
|
2020-01-07 20:56:26 +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"
|
|
|
|
"github.com/deluan/navidrome/server/subsonic/responses"
|
|
|
|
"github.com/deluan/navidrome/utils"
|
2016-03-03 20:46:19 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type StreamController struct {
|
2020-01-11 20:08:53 +01:00
|
|
|
browser engine.Browser
|
2016-03-04 19:12:56 +01:00
|
|
|
}
|
|
|
|
|
2020-01-11 20:08:53 +01:00
|
|
|
func NewStreamController(browser engine.Browser) *StreamController {
|
|
|
|
return &StreamController{browser: browser}
|
2020-01-07 20:56:26 +01:00
|
|
|
}
|
2016-03-03 20:46:19 +01:00
|
|
|
|
2020-01-14 21:34:18 +01:00
|
|
|
func (c *StreamController) getMediaFile(r *http.Request) (mf *engine.Entry, err error) {
|
|
|
|
id, err := RequiredParamString(r, "id", "id parameter required")
|
2020-01-07 20:56:26 +01:00
|
|
|
if err != nil {
|
2020-01-14 21:34:18 +01:00
|
|
|
return nil, err
|
2020-01-07 20:56:26 +01:00
|
|
|
}
|
2016-03-03 20:46:19 +01:00
|
|
|
|
2020-01-22 05:01:43 +01:00
|
|
|
mf, err = c.browser.GetSong(r.Context(), id)
|
2016-03-18 16:33:50 +01:00
|
|
|
switch {
|
2020-01-15 04:22:34 +01:00
|
|
|
case err == model.ErrNotFound:
|
2020-01-11 17:58:21 +01:00
|
|
|
log.Error(r, "Mediafile not found", "id", id)
|
2020-01-14 21:34:18 +01:00
|
|
|
return nil, NewError(responses.ErrorDataNotFound)
|
2016-03-18 16:33:50 +01:00
|
|
|
case err != nil:
|
2020-01-11 17:58:21 +01:00
|
|
|
log.Error(r, "Error reading mediafile from DB", "id", id, err)
|
2020-01-14 21:34:18 +01:00
|
|
|
return nil, NewError(responses.ErrorGeneric, "Internal error")
|
2016-03-04 19:12:56 +01:00
|
|
|
}
|
2020-01-11 17:58:21 +01:00
|
|
|
return
|
2016-03-04 19:12:56 +01:00
|
|
|
}
|
|
|
|
|
2016-03-06 01:59:51 +01:00
|
|
|
// TODO Still getting the "Conn.Write wrote more than the declared Content-Length" error.
|
|
|
|
// Don't know if this causes any issues
|
2020-01-07 20:56:26 +01:00
|
|
|
func (c *StreamController) Stream(w http.ResponseWriter, r *http.Request) (*responses.Subsonic, error) {
|
2020-01-14 21:34:18 +01:00
|
|
|
mf, err := c.getMediaFile(r)
|
2020-01-07 20:56:26 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
maxBitRate := ParamInt(r, "maxBitRate", 0)
|
2020-01-11 17:58:21 +01:00
|
|
|
maxBitRate = utils.MinInt(mf.BitRate, maxBitRate)
|
2016-03-04 19:12:56 +01:00
|
|
|
|
2020-01-14 21:34:18 +01:00
|
|
|
log.Debug(r, "Streaming file", "id", mf.Id, "path", mf.AbsolutePath, "bitrate", mf.BitRate, "maxBitRate", maxBitRate)
|
2016-03-04 19:12:56 +01:00
|
|
|
|
2016-03-10 17:14:47 +01:00
|
|
|
// TODO Send proper estimated content-length
|
2020-01-11 17:58:21 +01:00
|
|
|
//contentLength := mf.Size
|
2016-03-10 17:14:47 +01:00
|
|
|
//if maxBitRate > 0 {
|
2020-01-11 17:58:21 +01:00
|
|
|
// contentLength = strconv.Itoa((mf.Duration + 1) * maxBitRate * 1000 / 8)
|
2016-03-10 17:14:47 +01:00
|
|
|
//}
|
2020-01-07 20:56:26 +01:00
|
|
|
h := w.Header()
|
2020-01-22 16:19:13 +01:00
|
|
|
h.Set("Content-Length", strconv.Itoa(mf.Size))
|
2020-01-07 20:56:26 +01:00
|
|
|
h.Set("Content-Type", "audio/mpeg")
|
|
|
|
h.Set("Expires", "0")
|
|
|
|
h.Set("Cache-Control", "must-revalidate")
|
|
|
|
h.Set("Pragma", "public")
|
2016-03-04 19:12:56 +01:00
|
|
|
|
2020-01-07 20:56:26 +01:00
|
|
|
if r.Method == "HEAD" {
|
2020-01-11 20:08:53 +01:00
|
|
|
log.Debug(r, "Just a HEAD. Not streaming", "path", mf.AbsolutePath)
|
2020-01-07 20:56:26 +01:00
|
|
|
return nil, nil
|
2016-03-10 17:14:47 +01:00
|
|
|
}
|
|
|
|
|
2020-01-11 20:08:53 +01:00
|
|
|
err = engine.Stream(r.Context(), mf.AbsolutePath, mf.BitRate, maxBitRate, w)
|
2016-03-03 20:46:19 +01:00
|
|
|
if err != nil {
|
2020-01-14 21:34:18 +01:00
|
|
|
log.Error(r, "Error streaming file", "id", mf.Id, err)
|
2016-03-03 20:46:19 +01:00
|
|
|
}
|
|
|
|
|
2020-01-11 20:08:53 +01:00
|
|
|
log.Debug(r, "Finished streaming", "path", mf.AbsolutePath)
|
2020-01-07 20:56:26 +01:00
|
|
|
return nil, nil
|
2016-03-04 19:12:56 +01:00
|
|
|
}
|
|
|
|
|
2020-01-07 20:56:26 +01:00
|
|
|
func (c *StreamController) Download(w http.ResponseWriter, r *http.Request) (*responses.Subsonic, error) {
|
2020-01-14 21:34:18 +01:00
|
|
|
mf, err := c.getMediaFile(r)
|
2020-01-07 20:56:26 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-01-11 20:08:53 +01:00
|
|
|
log.Debug(r, "Sending file", "path", mf.AbsolutePath)
|
2016-03-04 19:12:56 +01:00
|
|
|
|
2020-01-11 20:08:53 +01:00
|
|
|
err = engine.Stream(r.Context(), mf.AbsolutePath, 0, 0, w)
|
2020-01-07 20:56:26 +01:00
|
|
|
if err != nil {
|
2020-01-11 20:08:53 +01:00
|
|
|
log.Error(r, "Error downloading file", "path", mf.AbsolutePath, err)
|
2020-01-07 20:56:26 +01:00
|
|
|
}
|
2016-03-03 20:46:19 +01:00
|
|
|
|
2020-01-11 20:08:53 +01:00
|
|
|
log.Debug(r, "Finished sending", "path", mf.AbsolutePath)
|
2020-01-07 20:56:26 +01:00
|
|
|
|
|
|
|
return nil, nil
|
2016-03-03 20:46:19 +01:00
|
|
|
}
|