From 7edf4ed9c5b114ceee7942fe39857f88e02bcc74 Mon Sep 17 00:00:00 2001 From: Joren Date: Fri, 6 Sep 2024 19:05:57 +0200 Subject: [PATCH] properly use createtemp for the uploaded files --- handlers.go | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/handlers.go b/handlers.go index 04ad95d..d7df463 100644 --- a/handlers.go +++ b/handlers.go @@ -7,7 +7,6 @@ import ( "net/http" "os" "path/filepath" - "time" ) func handleRoot(w http.ResponseWriter, r *http.Request) { @@ -37,32 +36,31 @@ func handleUpload(w http.ResponseWriter, r *http.Request) { } defer file.Close() - filename := fmt.Sprintf("%d_%s", time.Now().UnixNano(), header.Filename) - filepath := filepath.Join(uploadDir, filename) - - newFile, err := os.Create(filepath) + tempFile, err := os.CreateTemp(uploadDir, header.Filename) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } - defer newFile.Close() + defer tempFile.Close() - _, err = io.Copy(newFile, file) + _, err = io.Copy(tempFile, file) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } + tempFilename := filepath.Base(tempFile.Name()) + go func() { - err := processInputFile(filepath) + err := processInputFile(tempFile.Name()) if err != nil { fmt.Printf("Error processing file: %v\n", err) } - os.Remove(filepath) + os.Remove(tempFile.Name()) }() - http.Redirect(w, r, "/progress?filename="+filename, http.StatusSeeOther) + http.Redirect(w, r, "/progress?filename="+tempFilename, http.StatusSeeOther) } func handleProgress(w http.ResponseWriter, r *http.Request) {