From dc6b812587845b388186ab34151c83a15c4f6615 Mon Sep 17 00:00:00 2001 From: Deluan Date: Sat, 11 Jan 2020 11:58:21 -0500 Subject: [PATCH] Remove state from StreamController --- api/stream.go | 50 ++++++++++++++++++++++++-------------------------- 1 file changed, 24 insertions(+), 26 deletions(-) diff --git a/api/stream.go b/api/stream.go index 3c84a2b1..aaecd22f 100644 --- a/api/stream.go +++ b/api/stream.go @@ -12,83 +12,81 @@ import ( type StreamController struct { repo domain.MediaFileRepository - id string - mf *domain.MediaFile } func NewStreamController(repo domain.MediaFileRepository) *StreamController { return &StreamController{repo: repo} } -func (c *StreamController) Prepare(r *http.Request) (err error) { - c.id, err = RequiredParamString(r, "id", "id parameter required") +func (c *StreamController) Prepare(r *http.Request) (id string, mf *domain.MediaFile, err error) { + id, err = RequiredParamString(r, "id", "id parameter required") if err != nil { - return err + return "", nil, err } - c.mf, err = c.repo.Get(c.id) + mf, err = c.repo.Get(id) switch { case err == domain.ErrNotFound: - log.Error(r, "Mediafile not found", "id", c.id) - return NewError(responses.ErrorDataNotFound) + log.Error(r, "Mediafile not found", "id", id) + return "", nil, NewError(responses.ErrorDataNotFound) case err != nil: - log.Error(r, "Error reading mediafile from DB", "id", c.id, err) - return NewError(responses.ErrorGeneric, "Internal error") + log.Error(r, "Error reading mediafile from DB", "id", id, err) + return "", nil, NewError(responses.ErrorGeneric, "Internal error") } - return nil + return } // TODO Still getting the "Conn.Write wrote more than the declared Content-Length" error. // Don't know if this causes any issues 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 { return nil, err } 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 - //contentLength := c.mf.Size + //contentLength := mf.Size //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.Set("Content-Length", c.mf.Size) + h.Set("Content-Length", mf.Size) h.Set("Content-Type", "audio/mpeg") h.Set("Expires", "0") h.Set("Cache-Control", "must-revalidate") h.Set("Pragma", "public") 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 } - 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 { - 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 } 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 { 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 { - 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 }