Fix: tempFile not needed anymore

This commit is contained in:
Joaquín de Santiago 2024-12-05 15:58:55 +01:00
parent fad9c0fe78
commit ea741c17e2
2 changed files with 34 additions and 30 deletions

View File

@ -1,7 +1,6 @@
package web package web
import ( import (
"io"
"net/http" "net/http"
"os" "os"
"strconv" "strconv"
@ -34,33 +33,33 @@ func handleUpload(w http.ResponseWriter, r *http.Request) {
defer file.Close() defer file.Close()
// Save temp file // Save temp file
uploadDir := os.Getenv("UPLOAD_DIR") // uploadDir := os.Getenv("UPLOAD_DIR")
if uploadDir == "" { // if uploadDir == "" {
uploadDir = "uploads" // uploadDir = "uploads"
} // }
os.MkdirAll(uploadDir, os.ModePerm) // os.MkdirAll(uploadDir, os.ModePerm)
tempFile, err := os.CreateTemp(uploadDir, "history-*.txt") // tempFile, err := os.CreateTemp(uploadDir, "history-*.txt")
if err != nil { // if err != nil {
http.Error(w, "Error saving file", http.StatusInternalServerError) // http.Error(w, "Error saving file", http.StatusInternalServerError)
return // return
} // }
defer tempFile.Close() // defer tempFile.Close()
_, err = io.Copy(tempFile, file) // _, err = io.Copy(tempFile, file)
if err != nil { // if err != nil {
http.Error(w, "Error copying file", http.StatusInternalServerError) // http.Error(w, "Error copying file", http.StatusInternalServerError)
return // return
} // }
commandCounts, categories, pipeRedirectionCounts, commonPatterns := ProcessHistory(tempFile.Name()) commandCounts, categories, pipeRedirectionCounts, commonPatterns := ProcessHistory(file)
// THE UPLOAD FILE IS INMEDIATELY REMOVED // THE UPLOAD FILE IS INMEDIATELY REMOVED
// ONCE THE STATS ARE GENERATED // ONCE THE STATS ARE GENERATED
err = os.Remove(tempFile.Name()) // err = os.Remove(tempFile.Name())
if err != nil { // if err != nil {
http.Error(w, "Error deleting temporary file", http.StatusInternalServerError) // http.Error(w, "Error deleting temporary file", http.StatusInternalServerError)
return // return
} // }
limit := os.Getenv("TOP_N_COMMANDS") limit := os.Getenv("TOP_N_COMMANDS")
limitInt, err := strconv.Atoi(limit) limitInt, err := strconv.Atoi(limit)

View File

@ -2,8 +2,9 @@ package web
import ( import (
"bufio" "bufio"
"log" "bytes"
"os" "io"
"mime/multipart"
"regexp" "regexp"
"strings" "strings"
) )
@ -13,7 +14,7 @@ type CommandStat struct {
Count int Count int
} }
func ProcessHistory(fileName string) (map[string]int, map[string]int, map[string]int, map[string]int) { func ProcessHistory(file multipart.File) (map[string]int, map[string]int, map[string]int, map[string]int) {
commandCounts := make(map[string]int) commandCounts := make(map[string]int)
categories := make(map[string]int) categories := make(map[string]int)
@ -21,11 +22,15 @@ func ProcessHistory(fileName string) (map[string]int, map[string]int, map[string
commonPatterns := make(map[string]int) commonPatterns := make(map[string]int)
re := regexp.MustCompile(`\S+\.(log|txt|conf|json)`) re := regexp.MustCompile(`\S+\.(log|txt|conf|json)`)
file, err := os.Open(fileName) buf := bytes.NewBuffer(nil)
if err != nil { if _, err := io.Copy(buf, file); err != nil {
log.Fatal(err) return nil, nil, nil, nil
}
// Reset the file pointer to the beginning (if needed)
if seeker, ok := file.(multipart.File); ok {
seeker.Seek(0, 0)
} }
defer file.Close()
scanner := bufio.NewScanner(file) scanner := bufio.NewScanner(file)
for scanner.Scan() { for scanner.Scan() {