From 2091dead3f9e8091aa4a2caf8d9400c4d01beeac Mon Sep 17 00:00:00 2001 From: Alexey Khit Date: Sun, 20 Aug 2023 18:43:42 +0300 Subject: [PATCH] Refactoring for MP4 file handler --- internal/mp4/mp4.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/internal/mp4/mp4.go b/internal/mp4/mp4.go index 15c51f71..9144a497 100644 --- a/internal/mp4/mp4.go +++ b/internal/mp4/mp4.go @@ -120,15 +120,6 @@ func handlerMP4(w http.ResponseWriter, r *http.Request) { return } - defer stream.RemoveConsumer(cons) - - header := w.Header() - header.Set("Content-Type", mp4.ContentType(cons.Codecs())) - - if filename := query.Get("filename"); filename != "" { - header.Set("Content-Disposition", `attachment; filename="`+filename+`"`) - } - if rotate := query.Get("rotate"); rotate != "" { cons.Rotate = core.Atoi(rotate) } @@ -140,6 +131,13 @@ func handlerMP4(w http.ResponseWriter, r *http.Request) { } } + header := w.Header() + header.Set("Content-Type", mp4.ContentType(cons.Codecs())) + + if filename := query.Get("filename"); filename != "" { + header.Set("Content-Disposition", `attachment; filename="`+filename+`"`) + } + var duration *time.Timer if s := query.Get("duration"); s != "" { if i, _ := strconv.Atoi(s); i > 0 { @@ -151,6 +149,8 @@ func handlerMP4(w http.ResponseWriter, r *http.Request) { _, _ = cons.WriteTo(w) + stream.RemoveConsumer(cons) + if duration != nil { duration.Stop() }