navidrome/api/stream.go

45 lines
1.1 KiB
Go
Raw Normal View History

2016-03-03 20:46:19 +01:00
package api
import (
"github.com/deluan/gosonic/utils"
"github.com/karlkfi/inject"
"github.com/deluan/gosonic/domain"
"github.com/deluan/gosonic/api/responses"
"github.com/astaxie/beego"
"io"
"os"
)
type StreamController struct {
BaseAPIController
repo domain.MediaFileRepository
}
func (c *StreamController) Prepare() {
inject.ExtractAssignable(utils.Graph, &c.repo)
}
// For realtime transcoding, see : http://stackoverflow.com/questions/19292113/not-buffered-http-responsewritter-in-golang
func (c *StreamController) Get() {
id := c.GetParameter("id", "id parameter required")
2016-03-03 20:46:19 +01:00
mf, err := c.repo.Get(id)
if err != nil {
beego.Error("Error reading mediafile", id, "from the database", ":", err)
c.SendError(responses.ERROR_GENERIC, "Internal error")
}
beego.Debug("Streaming file", mf.Path)
f, err := os.Open(mf.Path)
if err != nil {
beego.Warn("Error opening file", mf.Path, "-", err)
c.SendError(responses.ERROR_DATA_NOT_FOUND, "cover art not available")
}
c.Ctx.Output.ContentType(mf.ContentType())
io.Copy(c.Ctx.ResponseWriter, f)
beego.Debug("Finished streaming of", mf.Path)
}