diff --git a/internal/web/controller.go b/internal/web/controller.go index 7a03bbc..5eb24cd 100644 --- a/internal/web/controller.go +++ b/internal/web/controller.go @@ -1,7 +1,6 @@ package web import ( - "io" "net/http" "os" "strconv" @@ -34,33 +33,33 @@ func handleUpload(w http.ResponseWriter, r *http.Request) { defer file.Close() // Save temp file - uploadDir := os.Getenv("UPLOAD_DIR") - if uploadDir == "" { - uploadDir = "uploads" - } - os.MkdirAll(uploadDir, os.ModePerm) - tempFile, err := os.CreateTemp(uploadDir, "history-*.txt") - if err != nil { - http.Error(w, "Error saving file", http.StatusInternalServerError) - return - } - defer tempFile.Close() + // uploadDir := os.Getenv("UPLOAD_DIR") + // if uploadDir == "" { + // uploadDir = "uploads" + // } + // os.MkdirAll(uploadDir, os.ModePerm) + // tempFile, err := os.CreateTemp(uploadDir, "history-*.txt") + // if err != nil { + // http.Error(w, "Error saving file", http.StatusInternalServerError) + // return + // } + // defer tempFile.Close() - _, err = io.Copy(tempFile, file) - if err != nil { - http.Error(w, "Error copying file", http.StatusInternalServerError) - return - } + // _, err = io.Copy(tempFile, file) + // if err != nil { + // http.Error(w, "Error copying file", http.StatusInternalServerError) + // return + // } - commandCounts, categories, pipeRedirectionCounts, commonPatterns := ProcessHistory(tempFile.Name()) + commandCounts, categories, pipeRedirectionCounts, commonPatterns := ProcessHistory(file) // THE UPLOAD FILE IS INMEDIATELY REMOVED // ONCE THE STATS ARE GENERATED - err = os.Remove(tempFile.Name()) - if err != nil { - http.Error(w, "Error deleting temporary file", http.StatusInternalServerError) - return - } + // err = os.Remove(tempFile.Name()) + // if err != nil { + // http.Error(w, "Error deleting temporary file", http.StatusInternalServerError) + // return + // } limit := os.Getenv("TOP_N_COMMANDS") limitInt, err := strconv.Atoi(limit) diff --git a/internal/web/model.go b/internal/web/model.go index ce67102..7e1b22f 100644 --- a/internal/web/model.go +++ b/internal/web/model.go @@ -2,8 +2,9 @@ package web import ( "bufio" - "log" - "os" + "bytes" + "io" + "mime/multipart" "regexp" "strings" ) @@ -13,7 +14,7 @@ type CommandStat struct { 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) 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) re := regexp.MustCompile(`\S+\.(log|txt|conf|json)`) - file, err := os.Open(fileName) - if err != nil { - log.Fatal(err) + buf := bytes.NewBuffer(nil) + if _, err := io.Copy(buf, file); err != nil { + 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) for scanner.Scan() {