From 4013a66b04c476618f8b1091850290a00600f990 Mon Sep 17 00:00:00 2001 From: marcobaobao Date: Mon, 3 Jun 2024 11:03:16 +0200 Subject: [PATCH] stream downloads zip archive --- server/handlers/archive.go | 34 ++++++++++------------------------ 1 file changed, 10 insertions(+), 24 deletions(-) diff --git a/server/handlers/archive.go b/server/handlers/archive.go index f2e6643..552add7 100644 --- a/server/handlers/archive.go +++ b/server/handlers/archive.go @@ -2,7 +2,6 @@ package handlers import ( "archive/zip" - "bytes" "encoding/base64" "encoding/json" "io" @@ -193,14 +192,8 @@ func DownloadFile(w http.ResponseWriter, r *http.Request) { root := config.Instance().DownloadPath if strings.Contains(filepath.Dir(filename), root) { - w.Header().Add( - "Content-Disposition", - "inline; filename=\""+filepath.Base(filename)+"\"", - ) - w.Header().Set( - "Content-Type", - "application/octet-stream", - ) + w.Header().Add("Content-Disposition", "inline; filename=\""+filepath.Base(filename)+"\"") + w.Header().Set("Content-Type", "application/octet-stream") fd, err := os.Open(filename) if err != nil { @@ -224,10 +217,13 @@ func BulkDownload(mdb *internal.MemoryDB) http.HandlerFunc { return } - var ( - buff bytes.Buffer - zipWriter = zip.NewWriter(&buff) + zipWriter := zip.NewWriter(w) + + w.Header().Add( + "Content-Disposition", + "inline; filename=download-archive-"+time.Now().Format(time.RFC3339)+".zip", ) + w.Header().Set("Content-Type", "application/zip") for _, p := range ps { wr, err := zipWriter.Create(filepath.Base(p.Output.SavedFilePath)) @@ -242,25 +238,15 @@ func BulkDownload(mdb *internal.MemoryDB) http.HandlerFunc { return } - _, err = io.Copy(wr, fd) - if err != nil { + if _, err := io.Copy(wr, fd); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } } - err := zipWriter.Close() - if err != nil { + if err := zipWriter.Close(); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } - - w.Header().Add( - "Content-Disposition", - "inline; filename=download-archive-"+time.Now().Format(time.RFC3339)+".zip", - ) - w.Header().Set("Content-Type", "application/zip") - - io.Copy(w, &buff) } }