use servefile for streaming

This commit is contained in:
sentriz
2019-08-01 13:57:16 +01:00
parent 0b929bd326
commit c75732868d
4 changed files with 9 additions and 8 deletions

View File

@@ -107,6 +107,7 @@ func processAssets(c *config, files []string) error {
if err != nil {
return errors.Wrap(err, "opening asset")
}
defer data.Close()
processAsset(
c,
&file{

View File

@@ -83,6 +83,10 @@ type subsonicHandler func(r *http.Request) *spec.Response
func (c *Controller) H(h subsonicHandler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
response := h(r)
if response == nil {
log.Println("error: non raw subsonic handler returned a nil response\n")
return
}
if err := writeResp(w, r, response); err != nil {
log.Printf("error writing subsonic response (normal handler): %v\n", err)
}
@@ -93,8 +97,10 @@ type subsonicHandlerRaw func(w http.ResponseWriter, r *http.Request) *spec.Respo
func (c *Controller) HR(h subsonicHandlerRaw) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// TODO: log if both response writer written and non nil spec return
response := h(w, r)
if response == nil {
return
}
if err := writeResp(w, r, response); err != nil {
log.Printf("error writing subsonic response (raw handler): %v\n", err)
}

View File

@@ -2,7 +2,6 @@ package ctrlsubsonic
import (
"net/http"
"os"
"path"
"time"
@@ -65,12 +64,7 @@ func (c *Controller) ServeStream(w http.ResponseWriter, r *http.Request) *spec.R
track.Album.RightPath,
track.Filename,
)
file, err := os.Open(absPath)
if err != nil {
return spec.NewError(0, "error while streaming media: %v", err)
}
stat, _ := file.Stat()
http.ServeContent(w, r, absPath, stat.ModTime(), file)
http.ServeFile(w, r, absPath)
//
// after we've served the file, mark the album as played
user := r.Context().Value(key.User).(*model.User)

Binary file not shown.