2020-01-20 00:21:44 +01:00
|
|
|
package subsonic
|
2016-03-03 20:46:19 +01:00
|
|
|
|
|
|
|
import (
|
2020-02-24 19:31:05 +01:00
|
|
|
"io"
|
2020-01-07 20:56:26 +01:00
|
|
|
"net/http"
|
2020-02-24 19:31:05 +01:00
|
|
|
"strconv"
|
2020-01-07 20:56:26 +01:00
|
|
|
|
2020-01-24 01:44:08 +01:00
|
|
|
"github.com/deluan/navidrome/engine"
|
2020-02-24 19:31:05 +01:00
|
|
|
"github.com/deluan/navidrome/log"
|
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 20:46:19 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type StreamController struct {
|
2020-02-03 17:54:59 +01:00
|
|
|
streamer engine.MediaStreamer
|
2016-03-04 19:12:56 +01:00
|
|
|
}
|
|
|
|
|
2020-02-03 17:54:59 +01:00
|
|
|
func NewStreamController(streamer engine.MediaStreamer) *StreamController {
|
|
|
|
return &StreamController{streamer: streamer}
|
2020-01-07 20:56:26 +01:00
|
|
|
}
|
2016-03-03 20:46:19 +01:00
|
|
|
|
2020-01-07 20:56:26 +01:00
|
|
|
func (c *StreamController) Stream(w http.ResponseWriter, r *http.Request) (*responses.Subsonic, error) {
|
2020-02-03 17:54:59 +01: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
|
|
|
maxBitRate := utils.ParamInt(r, "maxBitRate", 0)
|
|
|
|
format := utils.ParamString(r, "format")
|
2020-04-03 06:24:40 +02:00
|
|
|
estimateContentLength := utils.ParamBool(r, "estimateContentLength", false)
|
2016-03-04 19:12:56 +01:00
|
|
|
|
2020-03-16 19:28:13 +01:00
|
|
|
stream, err := c.streamer.NewStream(r.Context(), id, format, maxBitRate)
|
2016-03-03 20:46:19 +01:00
|
|
|
if err != nil {
|
2020-02-03 17:54:59 +01:00
|
|
|
return nil, err
|
2016-03-03 20:46:19 +01:00
|
|
|
}
|
2020-02-24 19:31:05 +01:00
|
|
|
defer func() {
|
|
|
|
if err := stream.Close(); err != nil {
|
|
|
|
log.Error("Error closing stream", "id", id, "file", stream.Name(), err)
|
|
|
|
}
|
|
|
|
}()
|
2016-03-03 20:46:19 +01:00
|
|
|
|
2020-02-24 19:31:05 +01:00
|
|
|
w.Header().Set("X-Content-Type-Options", "nosniff")
|
|
|
|
w.Header().Set("X-Content-Duration", strconv.FormatFloat(float64(stream.Duration()), 'G', -1, 32))
|
|
|
|
|
|
|
|
if stream.Seekable() {
|
|
|
|
http.ServeContent(w, r, stream.Name(), stream.ModTime(), stream)
|
|
|
|
} else {
|
|
|
|
// If the stream doesn't provide a size (i.e. is not seekable), we can't support ranges/content-length
|
|
|
|
w.Header().Set("Accept-Ranges", "none")
|
|
|
|
w.Header().Set("Content-Type", stream.ContentType())
|
2020-04-03 06:24:40 +02:00
|
|
|
|
|
|
|
// if Client requests the estimated content-length, send it
|
|
|
|
if estimateContentLength {
|
|
|
|
length := strconv.Itoa(stream.EstimatedContentLength())
|
|
|
|
log.Trace(r.Context(), "Estimated content-length", "contentLength", length)
|
|
|
|
w.Header().Set("Content-Length", length)
|
|
|
|
}
|
|
|
|
|
2020-02-24 19:31:05 +01:00
|
|
|
if c, err := io.Copy(w, stream); err != nil {
|
|
|
|
log.Error(r.Context(), "Error sending transcoded file", "id", id, err)
|
|
|
|
} else {
|
|
|
|
log.Trace(r.Context(), "Success sending transcode file", "id", id, "size", c)
|
|
|
|
}
|
|
|
|
}
|
2020-02-19 20:53:35 +01:00
|
|
|
|
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-02-03 17:54:59 +01:00
|
|
|
id, err := RequiredParamString(r, "id", "id parameter required")
|
2020-01-07 20:56:26 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-03-04 19:12:56 +01:00
|
|
|
|
2020-03-16 19:28:13 +01:00
|
|
|
stream, err := c.streamer.NewStream(r.Context(), id, "raw", 0)
|
2020-01-07 20:56:26 +01:00
|
|
|
if err != nil {
|
2020-02-03 17:54:59 +01:00
|
|
|
return nil, err
|
2020-01-07 20:56:26 +01:00
|
|
|
}
|
2016-03-03 20:46:19 +01:00
|
|
|
|
2020-02-24 19:31:05 +01:00
|
|
|
http.ServeContent(w, r, stream.Name(), stream.ModTime(), stream)
|
2020-01-07 20:56:26 +01:00
|
|
|
return nil, nil
|
2016-03-03 20:46:19 +01:00
|
|
|
}
|