properly use createtemp for the uploaded files
This commit is contained in:
parent
93d262d293
commit
7edf4ed9c5
18
handlers.go
18
handlers.go
@ -7,7 +7,6 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func handleRoot(w http.ResponseWriter, r *http.Request) {
|
func handleRoot(w http.ResponseWriter, r *http.Request) {
|
||||||
@ -37,32 +36,31 @@ func handleUpload(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
|
|
||||||
filename := fmt.Sprintf("%d_%s", time.Now().UnixNano(), header.Filename)
|
tempFile, err := os.CreateTemp(uploadDir, header.Filename)
|
||||||
filepath := filepath.Join(uploadDir, filename)
|
|
||||||
|
|
||||||
newFile, err := os.Create(filepath)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer newFile.Close()
|
defer tempFile.Close()
|
||||||
|
|
||||||
_, err = io.Copy(newFile, file)
|
_, err = io.Copy(tempFile, file)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
tempFilename := filepath.Base(tempFile.Name())
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
err := processInputFile(filepath)
|
err := processInputFile(tempFile.Name())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Error processing file: %v\n", err)
|
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) {
|
func handleProgress(w http.ResponseWriter, r *http.Request) {
|
||||||
|
Loading…
Reference in New Issue
Block a user