stream downloads zip archive

This commit is contained in:
2024-06-03 11:03:16 +02:00
parent 4cc1ed681a
commit 4013a66b04

View File

@@ -2,7 +2,6 @@ package handlers
import ( import (
"archive/zip" "archive/zip"
"bytes"
"encoding/base64" "encoding/base64"
"encoding/json" "encoding/json"
"io" "io"
@@ -193,14 +192,8 @@ func DownloadFile(w http.ResponseWriter, r *http.Request) {
root := config.Instance().DownloadPath root := config.Instance().DownloadPath
if strings.Contains(filepath.Dir(filename), root) { if strings.Contains(filepath.Dir(filename), root) {
w.Header().Add( w.Header().Add("Content-Disposition", "inline; filename=\""+filepath.Base(filename)+"\"")
"Content-Disposition", w.Header().Set("Content-Type", "application/octet-stream")
"inline; filename=\""+filepath.Base(filename)+"\"",
)
w.Header().Set(
"Content-Type",
"application/octet-stream",
)
fd, err := os.Open(filename) fd, err := os.Open(filename)
if err != nil { if err != nil {
@@ -224,10 +217,13 @@ func BulkDownload(mdb *internal.MemoryDB) http.HandlerFunc {
return return
} }
var ( zipWriter := zip.NewWriter(w)
buff bytes.Buffer
zipWriter = zip.NewWriter(&buff) 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 { for _, p := range ps {
wr, err := zipWriter.Create(filepath.Base(p.Output.SavedFilePath)) wr, err := zipWriter.Create(filepath.Base(p.Output.SavedFilePath))
@@ -242,25 +238,15 @@ func BulkDownload(mdb *internal.MemoryDB) http.HandlerFunc {
return return
} }
_, err = io.Copy(wr, fd) if _, err := io.Copy(wr, fd); err != nil {
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)
return return
} }
} }
err := zipWriter.Close() if err := zipWriter.Close(); err != nil {
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)
return 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)
} }
} }