feat: implement temporary file creation for log downloads to prevent Content-Length mismatches

This commit is contained in:
Wikid82
2025-11-22 16:35:04 -05:00
parent 2a1e91c50b
commit 933ec88c83
2 changed files with 29 additions and 1 deletions

View File

@@ -1,6 +1,7 @@
package handlers
import (
"io"
"net/http"
"os"
"strconv"
@@ -75,6 +76,30 @@ func (h *LogsHandler) Download(c *gin.Context) {
return
}
// Create a temporary file to serve a consistent snapshot
// This prevents Content-Length mismatches if the live log file grows during download
tmpFile, err := os.CreateTemp("", "cpmp-log-*.log")
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create temp file"})
return
}
defer os.Remove(tmpFile.Name())
srcFile, err := os.Open(path)
if err != nil {
tmpFile.Close()
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to open log file"})
return
}
defer srcFile.Close()
if _, err := io.Copy(tmpFile, srcFile); err != nil {
tmpFile.Close()
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to copy log file"})
return
}
tmpFile.Close()
c.Header("Content-Disposition", "attachment; filename="+filename)
c.File(path)
c.File(tmpFile.Name())
}

3
cookies.txt Normal file
View File

@@ -0,0 +1,3 @@
# Netscape HTTP Cookie File
# https://curl.se/docs/http-cookies.html
# This file was generated by libcurl! Edit at your own risk.