fix: stage zip downloads to temp file and improve file serving headers
Write zip to a temporary file before serving to enable correct content-length, range requests, and proper cache-control headers. Additionally, handle negative object sizes by falling back to file metadata for content-length.
This commit is contained in:
@@ -626,6 +626,7 @@ func (a *App) serveFileContent(w http.ResponseWriter, r *http.Request, box servi
|
||||
defer object.Body.Close()
|
||||
|
||||
w.Header().Set("Content-Type", file.ContentType)
|
||||
w.Header().Set("Cache-Control", "no-transform")
|
||||
disposition := "inline"
|
||||
if attachment {
|
||||
disposition = "attachment"
|
||||
@@ -634,8 +635,12 @@ func (a *App) serveFileContent(w http.ResponseWriter, r *http.Request, box servi
|
||||
if seeker, ok := object.Body.(io.ReadSeeker); ok {
|
||||
http.ServeContent(w, r, file.Name, object.ModTime, seeker)
|
||||
} else {
|
||||
if object.Size > 0 {
|
||||
w.Header().Set("Content-Length", fmt.Sprintf("%d", object.Size))
|
||||
size := object.Size
|
||||
if size < 0 {
|
||||
size = file.Size
|
||||
}
|
||||
if size >= 0 {
|
||||
w.Header().Set("Content-Length", fmt.Sprintf("%d", size))
|
||||
}
|
||||
w.WriteHeader(http.StatusOK)
|
||||
_, _ = io.Copy(w, object.Body)
|
||||
@@ -722,14 +727,44 @@ func (a *App) DownloadZip(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/zip")
|
||||
w.Header().Set("Content-Disposition", contentDisposition("attachment", "warpbox-"+box.ID+".zip"))
|
||||
w.Header().Set("Last-Modified", time.Now().UTC().Format(http.TimeFormat))
|
||||
|
||||
if err := a.uploadService.WriteZip(w, box); err != nil {
|
||||
a.logger.Error("zip download failed", "source", "download", "severity", "error", "code", 5002, "box_id", box.ID, "error", err.Error())
|
||||
tempDir := filepath.Join(a.cfg.DataDir, "tmp", "downloads")
|
||||
if err := os.MkdirAll(tempDir, 0o700); err != nil {
|
||||
a.logger.Error("zip staging directory creation failed", "source", "download", "severity", "error", "code", 5002, "box_id", box.ID, "error", err.Error())
|
||||
http.Error(w, "failed to prepare zip download", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
archive, err := os.CreateTemp(tempDir, "warpbox-*.zip")
|
||||
if err != nil {
|
||||
a.logger.Error("zip staging file creation failed", "source", "download", "severity", "error", "code", 5002, "box_id", box.ID, "error", err.Error())
|
||||
http.Error(w, "failed to prepare zip download", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
archivePath := archive.Name()
|
||||
defer func() {
|
||||
archive.Close()
|
||||
if err := os.Remove(archivePath); err != nil && !errors.Is(err, os.ErrNotExist) {
|
||||
a.logger.Warn("failed to remove staged zip", "source", "download", "severity", "warn", "box_id", box.ID, "error", err.Error())
|
||||
}
|
||||
}()
|
||||
|
||||
if err := a.uploadService.WriteZip(archive, box); err != nil {
|
||||
a.logger.Error("zip download failed", "source", "download", "severity", "error", "code", 5002, "box_id", box.ID, "error", err.Error())
|
||||
http.Error(w, "failed to prepare zip download", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
stat, err := archive.Stat()
|
||||
if err != nil {
|
||||
a.logger.Error("staged zip stat failed", "source", "download", "severity", "error", "code", 5002, "box_id", box.ID, "error", err.Error())
|
||||
http.Error(w, "failed to prepare zip download", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
name := "warpbox-" + box.ID + ".zip"
|
||||
w.Header().Set("Content-Type", "application/zip")
|
||||
w.Header().Set("Cache-Control", "no-transform")
|
||||
w.Header().Set("Content-Disposition", contentDisposition("attachment", name))
|
||||
http.ServeContent(w, r, name, stat.ModTime(), archive)
|
||||
|
||||
if err := a.uploadService.RecordDownload(box.ID); err != nil && !errors.Is(err, os.ErrNotExist) {
|
||||
a.logger.Warn("failed to record zip download", "source", "download", "severity", "warn", "code", 4003, "box_id", box.ID, "error", err.Error())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user