From f7cf197b4e7b19a00b83396147863607ef7e1f10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?O=CC=81scar=20M=2E=20Lage?= Date: Thu, 5 Dec 2024 16:43:51 +0100 Subject: [PATCH] Imp: The file does not need to be copied in disk anymore File is being proccessed from memory directly. We don't need to tempcopy nor delete it because it's not saved to disk. --- README.md | 2 -- env-sample | 1 - internal/web/controller.go | 27 --------------------------- internal/web/model.go | 7 ------- internal/web/templates/index.html | 7 +++---- internal/web/utils.go | 8 +++++++- uploads/delete.me | 0 7 files changed, 10 insertions(+), 42 deletions(-) delete mode 100644 uploads/delete.me diff --git a/README.md b/README.md index c226e03..ee036dd 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,6 @@ $ cp env-sample .env DOCKER_ENV=production BASE_URL=https://wrap.sh SERVER_PORT=8080 -UPLOAD_DIR=uploads TOP_N_COMMANDS=10 MASTODON_INSTANCE_FOR_SHARING=mastodon.social ``` @@ -61,7 +60,6 @@ $ go mod tidy ├── internal/ │ ├── web/ # Web logic (routes, controller...) ├── result/ # Place for storing the results -├── upload/ # Temporal place for storing the uploads (inmediately removed) ├── .env.sample # Sample environment file └── README.md # This file └── Dockerfile # Requirement for the docker container to be built diff --git a/env-sample b/env-sample index f12a847..bfcb68e 100644 --- a/env-sample +++ b/env-sample @@ -1,6 +1,5 @@ DOCKER_ENV=production BASE_URL=https://wrap.sh SERVER_PORT=8080 -UPLOAD_DIR=uploads TOP_N_COMMANDS=10 MASTODON_INSTANCE_FOR_SHARING=mastodon.social diff --git a/internal/web/controller.go b/internal/web/controller.go index 5eb24cd..8cd0122 100644 --- a/internal/web/controller.go +++ b/internal/web/controller.go @@ -32,35 +32,8 @@ 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() - - // _, err = io.Copy(tempFile, file) - // if err != nil { - // http.Error(w, "Error copying file", http.StatusInternalServerError) - // return - // } - 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 - // } - limit := os.Getenv("TOP_N_COMMANDS") limitInt, err := strconv.Atoi(limit) if err != nil { diff --git a/internal/web/model.go b/internal/web/model.go index 7e1b22f..a8111fe 100644 --- a/internal/web/model.go +++ b/internal/web/model.go @@ -2,8 +2,6 @@ package web import ( "bufio" - "bytes" - "io" "mime/multipart" "regexp" "strings" @@ -22,11 +20,6 @@ func ProcessHistory(file multipart.File) (map[string]int, map[string]int, map[st commonPatterns := make(map[string]int) re := regexp.MustCompile(`\S+\.(log|txt|conf|json)`) - 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) diff --git a/internal/web/templates/index.html b/internal/web/templates/index.html index 9ebdd9e..f55021c 100644 --- a/internal/web/templates/index.html +++ b/internal/web/templates/index.html @@ -16,10 +16,9 @@ -

Warning: Be sure to edit it beforehand to remove any passwords - or commands that might leak sensitive information. While the file is - deleted immediately after being uploaded, double-check it before - submitting.

+

⚠️ Warning:⚠️ Be sure to edit it beforehand to remove any passwords + or commands that might leak sensitive information.

+

The file is not uploaded anywhere; it is processed in memory to generate the statistics and is automatically discarded.

diff --git a/internal/web/utils.go b/internal/web/utils.go index d4ecbda..e8138a7 100644 --- a/internal/web/utils.go +++ b/internal/web/utils.go @@ -103,7 +103,13 @@ func createBarChart(stats []CommandStat) *charts.Bar { } bar.SetXAxis(commands). - AddSeries("Freq", generateBarItems(counts)) + AddSeries("Freq", generateBarItems(counts)). + SetSeriesOptions( + charts.WithLabelOpts(opts.Label{ + Color: "white", + Position: "top", + }), + ) return bar } diff --git a/uploads/delete.me b/uploads/delete.me deleted file mode 100644 index e69de29..0000000