wrappd.sh/internal/web/controller.go
Óscar M. Lage f7cf197b4e 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.
2024-12-05 16:43:51 +01:00

108 lines
2.4 KiB
Go

package web
import (
"net/http"
"os"
"strconv"
"strings"
"github.com/google/uuid"
)
func handleIndex(w http.ResponseWriter, r *http.Request) {
tmpl, err := loadTemplates("index.html")
if err != nil {
http.Error(w, "Error loading template", http.StatusInternalServerError)
return
}
tmpl.Execute(w, nil)
}
func handleUpload(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Invalid request method", http.StatusMethodNotAllowed)
return
}
file, _, err := r.FormFile("file")
if err != nil {
http.Error(w, "Error reading file", http.StatusBadRequest)
return
}
defer file.Close()
commandCounts, categories, pipeRedirectionCounts, commonPatterns := ProcessHistory(file)
limit := os.Getenv("TOP_N_COMMANDS")
limitInt, err := strconv.Atoi(limit)
if err != nil {
limitInt = 5
}
// Stats
commandStats := ConvertAndSort(commandCounts, limitInt)
// Graph generation
bar := createBarChart(commandStats)
var chartHTML strings.Builder
if err := bar.Render(&chartHTML); err != nil {
http.Error(w, "Error generating chart", http.StatusInternalServerError)
return
}
// More stats
stats := GenerateStats(commandCounts, categories, commonPatterns)
stats["chartHTML"] = chartHTML.String()
// Categories
categoryStats := ConvertAndSort(categories, limitInt)
stats["categoryStats"] = categoryStats
// Top commands
topCommands := ConvertAndSort(commandCounts, limitInt)
stats["topCommands"] = topCommands
// Common patterns
commonPatternStats := ConvertAndSort(commonPatterns, limitInt)
stats["commonPatternStats"] = commonPatternStats
// Pipe counts
stats["pipeRedirectionCounts"] = pipeRedirectionCounts
// Save results
uniqueID := uuid.New().String()
saveResults(uniqueID, stats)
http.Redirect(w, r, "/results/"+uniqueID, http.StatusSeeOther)
}
func handleResults(w http.ResponseWriter, r *http.Request) {
uniqueID := strings.TrimPrefix(r.URL.Path, "/results/")
stats, err := loadResults(uniqueID)
if err != nil {
http.Error(w, "Results not found", http.StatusNotFound)
return
}
stats["uniqueID"] = uniqueID
baseURL := os.Getenv("BASE_URL")
if baseURL == "" {
baseURL = "https://wrappd.oscarmlage.com"
}
stats["baseURL"] = baseURL
mastodon := os.Getenv("MASTODON_INSTANCE_FOR_SHARING")
stats["mastodon"] = mastodon
tmpl, err := loadTemplates("result.html")
if err != nil {
http.Error(w, "Error loading template", http.StatusInternalServerError)
return
}
tmpl.Execute(w, stats)
}