From 142c09e62497ec867d24eaaa291580d87c088aa2 Mon Sep 17 00:00:00 2001 From: Joren Date: Sat, 14 Sep 2024 00:53:55 +0200 Subject: [PATCH] Implement correct pause state --- handlers.go | 83 ++++++++++++++++++++++++++++++++++++++++------ main.go | 15 --------- templates/index | 9 ++++- templates/progress | 32 ++++++++++++++---- 4 files changed, 105 insertions(+), 34 deletions(-) diff --git a/handlers.go b/handlers.go index 76b9701..11f5ce3 100644 --- a/handlers.go +++ b/handlers.go @@ -9,20 +9,41 @@ import ( "path/filepath" ) +type ProgressInfo struct { + Percentage float64 + CurrentFile string + Paused bool +} + func handleRoot(w http.ResponseWriter, r *http.Request) { - if r.URL.Path != "/" { - http.NotFound(w, r) - return - } - progressMutex.Lock() - jobs := make(map[string]*ProgressInfo) - for k, v := range progress { - jobs[k] = v - } - progressMutex.Unlock() + defer progressMutex.Unlock() - err := templates.ExecuteTemplate(w, "index", struct{ Jobs map[string]*ProgressInfo }{jobs}) + jobsInfo := make(map[string]struct { + Percentage float64 + CurrentFile string + Paused bool + }) + + for filename, info := range progress { + jobsInfo[filename] = struct { + Percentage float64 + CurrentFile string + Paused bool + }{ + Percentage: info.Percentage, + CurrentFile: info.CurrentFile, + Paused: info.Paused, + } + } + + err := templates.ExecuteTemplate(w, "index", struct { + Jobs map[string]struct { + Percentage float64 + CurrentFile string + Paused bool + } + }{jobsInfo}) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } @@ -157,6 +178,12 @@ func handlePause(w http.ResponseWriter, r *http.Request) { jobInfo.Cmd.Process.Kill() } + progressMutex.Lock() + if progressInfo, ok := progress[filename]; ok { + progressInfo.Paused = true + } + progressMutex.Unlock() + fmt.Fprintf(w, "Pause signal sent for %s", filename) } @@ -179,6 +206,13 @@ func handleResume(w http.ResponseWriter, r *http.Request) { jobInfo.Paused = false jobInfo.ResumeChan <- struct{}{} + // Update the progress information + progressMutex.Lock() + if progressInfo, ok := progress[filename]; ok { + progressInfo.Paused = false + } + progressMutex.Unlock() + fmt.Fprintf(w, "Resume signal sent for %s", filename) } @@ -232,3 +266,30 @@ func clearCompletedJobs() { } } } + +func updateProgress(filename string, value float64, currentFile string) { + progressMutex.Lock() + defer progressMutex.Unlock() + + jobsMutex.Lock() + jobInfo, exists := jobs[filename] + jobsMutex.Unlock() + + paused := false + if exists { + paused = jobInfo.Paused + } + + if existingProgress, ok := progress[filename]; ok { + existingProgress.Percentage = value + existingProgress.CurrentFile = currentFile + existingProgress.Paused = paused + } else { + progress[filename] = &ProgressInfo{ + Percentage: value, + CurrentFile: currentFile, + Paused: paused, + } + } + fmt.Printf("Progress updated for %s: %.2f%%, Current file: %s, Paused: %v\n", filename, value, currentFile, paused) +} diff --git a/main.go b/main.go index 1b66682..7feea87 100644 --- a/main.go +++ b/main.go @@ -37,11 +37,6 @@ var progress = make(map[string]*ProgressInfo) const uploadDir = "uploads" -type ProgressInfo struct { - Percentage float64 - CurrentFile string -} - var templates *template.Template //go:embed templates @@ -87,16 +82,6 @@ func startWebServer() { http.ListenAndServe(":8080", nil) } -func updateProgress(filename string, value float64, currentFile string) { - progressMutex.Lock() - defer progressMutex.Unlock() - progress[filename] = &ProgressInfo{ - Percentage: value, - CurrentFile: currentFile, - } - fmt.Printf("Progress updated for %s: %.2f%%, Current file: %s\n", filename, value, currentFile) -} - func getProgress(filename string) *ProgressInfo { progressMutex.Lock() defer progressMutex.Unlock() diff --git a/templates/index b/templates/index index bc57763..cc55ff1 100644 --- a/templates/index +++ b/templates/index @@ -74,6 +74,9 @@ display: inline-block; width: 5em; } + .paused { + color: #ffa500; + } @media (max-width: 600px) { body { padding: 10px; @@ -120,7 +123,11 @@ {{$filename}}
- Progress: {{printf "%5.1f%%" $info.Percentage}} Current file: {{$info.CurrentFile}} + Progress: {{printf "%5.1f%%" $info.Percentage}} + Current file: {{$info.CurrentFile}} + {{if $info.Paused}} + (Paused) + {{end}}
{{else}} diff --git a/templates/progress b/templates/progress index b113c91..f3c710d 100644 --- a/templates/progress +++ b/templates/progress @@ -127,10 +127,12 @@
- +