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.
This commit is contained in:
Óscar M. Lage 2024-12-05 16:43:51 +01:00
parent ea741c17e2
commit f7cf197b4e
7 changed files with 10 additions and 42 deletions

View File

@ -16,7 +16,6 @@ $ cp env-sample .env
DOCKER_ENV=production DOCKER_ENV=production
BASE_URL=https://wrap.sh BASE_URL=https://wrap.sh
SERVER_PORT=8080 SERVER_PORT=8080
UPLOAD_DIR=uploads
TOP_N_COMMANDS=10 TOP_N_COMMANDS=10
MASTODON_INSTANCE_FOR_SHARING=mastodon.social MASTODON_INSTANCE_FOR_SHARING=mastodon.social
``` ```
@ -61,7 +60,6 @@ $ go mod tidy
├── internal/ ├── internal/
│ ├── web/ # Web logic (routes, controller...) │ ├── web/ # Web logic (routes, controller...)
├── result/ # Place for storing the results ├── result/ # Place for storing the results
├── upload/ # Temporal place for storing the uploads (inmediately removed)
├── .env.sample # Sample environment file ├── .env.sample # Sample environment file
└── README.md # This file └── README.md # This file
└── Dockerfile # Requirement for the docker container to be built └── Dockerfile # Requirement for the docker container to be built

View File

@ -1,6 +1,5 @@
DOCKER_ENV=production DOCKER_ENV=production
BASE_URL=https://wrap.sh BASE_URL=https://wrap.sh
SERVER_PORT=8080 SERVER_PORT=8080
UPLOAD_DIR=uploads
TOP_N_COMMANDS=10 TOP_N_COMMANDS=10
MASTODON_INSTANCE_FOR_SHARING=mastodon.social MASTODON_INSTANCE_FOR_SHARING=mastodon.social

View File

@ -32,35 +32,8 @@ func handleUpload(w http.ResponseWriter, r *http.Request) {
} }
defer file.Close() 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) 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") limit := os.Getenv("TOP_N_COMMANDS")
limitInt, err := strconv.Atoi(limit) limitInt, err := strconv.Atoi(limit)
if err != nil { if err != nil {

View File

@ -2,8 +2,6 @@ package web
import ( import (
"bufio" "bufio"
"bytes"
"io"
"mime/multipart" "mime/multipart"
"regexp" "regexp"
"strings" "strings"
@ -22,11 +20,6 @@ func ProcessHistory(file multipart.File) (map[string]int, map[string]int, map[st
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)`)
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) // Reset the file pointer to the beginning (if needed)
if seeker, ok := file.(multipart.File); ok { if seeker, ok := file.(multipart.File); ok {
seeker.Seek(0, 0) seeker.Seek(0, 0)

View File

@ -16,10 +16,9 @@
</button> </button>
</div> </div>
</form> </form>
<p class="mb-8 mt-8"><small>Warning: Be sure to edit it beforehand to remove any passwords <p class="mb-6 mt-8"><small>⚠️ <strong>Warning</strong>:⚠️ Be sure to edit it beforehand to remove any passwords
or commands that might leak sensitive information. While the file is or commands that might leak sensitive information.</small><p>
deleted immediately after being uploaded, double-check it before <p><small>The file is not uploaded anywhere; it is processed in memory to generate the statistics and is automatically discarded.</small></p>
submitting.</small></p>
</div> </div>
</div> </div>

View File

@ -103,7 +103,13 @@ func createBarChart(stats []CommandStat) *charts.Bar {
} }
bar.SetXAxis(commands). bar.SetXAxis(commands).
AddSeries("Freq", generateBarItems(counts)) AddSeries("Freq", generateBarItems(counts)).
SetSeriesOptions(
charts.WithLabelOpts(opts.Label{
Color: "white",
Position: "top",
}),
)
return bar return bar
} }

View File