Remove state from StreamController
This commit is contained in:
+24
-26
@@ -12,83 +12,81 @@ import (
|
|||||||
|
|
||||||
type StreamController struct {
|
type StreamController struct {
|
||||||
repo domain.MediaFileRepository
|
repo domain.MediaFileRepository
|
||||||
id string
|
|
||||||
mf *domain.MediaFile
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewStreamController(repo domain.MediaFileRepository) *StreamController {
|
func NewStreamController(repo domain.MediaFileRepository) *StreamController {
|
||||||
return &StreamController{repo: repo}
|
return &StreamController{repo: repo}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *StreamController) Prepare(r *http.Request) (err error) {
|
func (c *StreamController) Prepare(r *http.Request) (id string, mf *domain.MediaFile, err error) {
|
||||||
c.id, err = RequiredParamString(r, "id", "id parameter required")
|
id, err = RequiredParamString(r, "id", "id parameter required")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return "", nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
c.mf, err = c.repo.Get(c.id)
|
mf, err = c.repo.Get(id)
|
||||||
switch {
|
switch {
|
||||||
case err == domain.ErrNotFound:
|
case err == domain.ErrNotFound:
|
||||||
log.Error(r, "Mediafile not found", "id", c.id)
|
log.Error(r, "Mediafile not found", "id", id)
|
||||||
return NewError(responses.ErrorDataNotFound)
|
return "", nil, NewError(responses.ErrorDataNotFound)
|
||||||
case err != nil:
|
case err != nil:
|
||||||
log.Error(r, "Error reading mediafile from DB", "id", c.id, err)
|
log.Error(r, "Error reading mediafile from DB", "id", id, err)
|
||||||
return NewError(responses.ErrorGeneric, "Internal error")
|
return "", nil, NewError(responses.ErrorGeneric, "Internal error")
|
||||||
}
|
}
|
||||||
return nil
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO Still getting the "Conn.Write wrote more than the declared Content-Length" error.
|
// TODO Still getting the "Conn.Write wrote more than the declared Content-Length" error.
|
||||||
// Don't know if this causes any issues
|
// Don't know if this causes any issues
|
||||||
func (c *StreamController) Stream(w http.ResponseWriter, r *http.Request) (*responses.Subsonic, error) {
|
func (c *StreamController) Stream(w http.ResponseWriter, r *http.Request) (*responses.Subsonic, error) {
|
||||||
err := c.Prepare(r)
|
id, mf, err := c.Prepare(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
maxBitRate := ParamInt(r, "maxBitRate", 0)
|
maxBitRate := ParamInt(r, "maxBitRate", 0)
|
||||||
maxBitRate = utils.MinInt(c.mf.BitRate, maxBitRate)
|
maxBitRate = utils.MinInt(mf.BitRate, maxBitRate)
|
||||||
|
|
||||||
log.Debug(r, "Streaming file", "id", c.id, "path", c.mf.Path, "bitrate", c.mf.BitRate, "maxBitRate", maxBitRate)
|
log.Debug(r, "Streaming file", "id", id, "path", mf.Path, "bitrate", mf.BitRate, "maxBitRate", maxBitRate)
|
||||||
|
|
||||||
// TODO Send proper estimated content-length
|
// TODO Send proper estimated content-length
|
||||||
//contentLength := c.mf.Size
|
//contentLength := mf.Size
|
||||||
//if maxBitRate > 0 {
|
//if maxBitRate > 0 {
|
||||||
// contentLength = strconv.Itoa((c.mf.Duration + 1) * maxBitRate * 1000 / 8)
|
// contentLength = strconv.Itoa((mf.Duration + 1) * maxBitRate * 1000 / 8)
|
||||||
//}
|
//}
|
||||||
h := w.Header()
|
h := w.Header()
|
||||||
h.Set("Content-Length", c.mf.Size)
|
h.Set("Content-Length", mf.Size)
|
||||||
h.Set("Content-Type", "audio/mpeg")
|
h.Set("Content-Type", "audio/mpeg")
|
||||||
h.Set("Expires", "0")
|
h.Set("Expires", "0")
|
||||||
h.Set("Cache-Control", "must-revalidate")
|
h.Set("Cache-Control", "must-revalidate")
|
||||||
h.Set("Pragma", "public")
|
h.Set("Pragma", "public")
|
||||||
|
|
||||||
if r.Method == "HEAD" {
|
if r.Method == "HEAD" {
|
||||||
log.Debug(r, "Just a HEAD. Not streaming", "path", c.mf.Path)
|
log.Debug(r, "Just a HEAD. Not streaming", "path", mf.Path)
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
err = engine.Stream(r.Context(), c.mf.Path, c.mf.BitRate, maxBitRate, w)
|
err = engine.Stream(r.Context(), mf.Path, mf.BitRate, maxBitRate, w)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error(r, "Error streaming file", "id", c.id, err)
|
log.Error(r, "Error streaming file", "id", id, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Debug(r, "Finished streaming", "path", c.mf.Path)
|
log.Debug(r, "Finished streaming", "path", mf.Path)
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *StreamController) Download(w http.ResponseWriter, r *http.Request) (*responses.Subsonic, error) {
|
func (c *StreamController) Download(w http.ResponseWriter, r *http.Request) (*responses.Subsonic, error) {
|
||||||
err := c.Prepare(r)
|
_, mf, err := c.Prepare(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
log.Debug(r, "Sending file", "path", c.mf.Path)
|
log.Debug(r, "Sending file", "path", mf.Path)
|
||||||
|
|
||||||
err = engine.Stream(r.Context(), c.mf.Path, 0, 0, w)
|
err = engine.Stream(r.Context(), mf.Path, 0, 0, w)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error(r, "Error downloading file", "path", c.mf.Path, err)
|
log.Error(r, "Error downloading file", "path", mf.Path, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Debug(r, "Finished sending", "path", c.mf.Path)
|
log.Debug(r, "Finished sending", "path", mf.Path)
|
||||||
|
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user