Skip to content

Commit

Permalink
Close stream when downloading files, fix fd leak
Browse files Browse the repository at this point in the history
  • Loading branch information
deluan committed Feb 7, 2023
1 parent 05c6cde commit b8c5e49
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion server/subsonic/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,17 @@ func (api *Router) Download(w http.ResponseWriter, r *http.Request) (*responses.
switch v := entity.(type) {
case *model.MediaFile:
stream, err := api.streamer.NewStream(ctx, id, format, maxBitRate)

if err != nil {
return nil, err
}

// Make sure the stream will be closed at the end, to avoid leakage
defer func() {
if err := stream.Close(); err != nil && log.CurrentLevel() >= log.LevelDebug {
log.Error("Error closing stream", "id", id, "file", stream.Name(), err)
}
}()

disposition := fmt.Sprintf("attachment; filename=\"%s\"", stream.Name())
w.Header().Set("Content-Disposition", disposition)

Expand Down

0 comments on commit b8c5e49

Please sign in to comment.