Close stream when downloading files, fix fd leak

This commit is contained in:
Deluan 2023-02-06 21:41:25 -05:00 committed by Deluan Quintão
parent 05c6cdea1a
commit b8c5e49dd3
1 changed files with 7 additions and 1 deletions

View File

@ -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)