Implement correct pause state
This commit is contained in:
parent
9f2677485e
commit
142c09e624
83
handlers.go
83
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)
|
||||
}
|
||||
|
15
main.go
15
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()
|
||||
|
@ -74,6 +74,9 @@
|
||||
display: inline-block;
|
||||
width: 5em;
|
||||
}
|
||||
.paused {
|
||||
color: #ffa500;
|
||||
}
|
||||
@media (max-width: 600px) {
|
||||
body {
|
||||
padding: 10px;
|
||||
@ -120,7 +123,11 @@
|
||||
<a href="/progress?filename={{$filename}}">{{$filename}}</a>
|
||||
</div>
|
||||
<div class="job-info">
|
||||
Progress: <span class="progress-text">{{printf "%5.1f%%" $info.Percentage}}</span> Current file: {{$info.CurrentFile}}
|
||||
Progress: <span class="progress-text">{{printf "%5.1f%%" $info.Percentage}}</span>
|
||||
Current file: {{$info.CurrentFile}}
|
||||
{{if $info.Paused}}
|
||||
<span class="paused">(Paused)</span>
|
||||
{{end}}
|
||||
</div>
|
||||
</li>
|
||||
{{else}}
|
||||
|
@ -127,10 +127,12 @@
|
||||
<div>
|
||||
<button id="abort-button" onclick="abortDownload()">Abort Download</button>
|
||||
<button id="pause-button" onclick="pauseDownload()">Pause Download</button>
|
||||
<button id="resume-button" onclick="resumeDownload()">Resume Download</button>
|
||||
<button id="resume-button" onclick="resumeDownload()" style="display: none;">Resume Download</button>
|
||||
<button id="back-button" onclick="window.location.href='/'">Back to Index</button>
|
||||
</div>
|
||||
<script>
|
||||
let isPaused = false;
|
||||
|
||||
function updateProgress() {
|
||||
fetch('/progress?filename={{.Filename}}', {
|
||||
headers: {
|
||||
@ -143,12 +145,25 @@
|
||||
document.getElementById('progress-bar').style.width = progress + '%';
|
||||
document.getElementById('progress-text').innerText = progress + '%';
|
||||
document.getElementById('currentFile').innerText = 'Current file: ' + (data.CurrentFile || 'None');
|
||||
if (progress < 100) {
|
||||
|
||||
isPaused = data.Paused;
|
||||
updatePauseResumeButtons();
|
||||
|
||||
if (progress < 100 && !isPaused) {
|
||||
setTimeout(updateProgress, 1000);
|
||||
}
|
||||
});
|
||||
}
|
||||
updateProgress();
|
||||
|
||||
function updatePauseResumeButtons() {
|
||||
if (isPaused) {
|
||||
document.getElementById('pause-button').style.display = 'none';
|
||||
document.getElementById('resume-button').style.display = 'inline-block';
|
||||
} else {
|
||||
document.getElementById('pause-button').style.display = 'inline-block';
|
||||
document.getElementById('resume-button').style.display = 'none';
|
||||
}
|
||||
}
|
||||
|
||||
function abortDownload() {
|
||||
fetch('/abort?filename={{.Filename}}', { method: 'POST' })
|
||||
@ -166,8 +181,8 @@
|
||||
.then(response => {
|
||||
if (response.ok) {
|
||||
console.log('Pause signal sent. The download will pause soon.');
|
||||
document.getElementById('pause-button').style.display = 'none';
|
||||
document.getElementById('resume-button').style.display = 'inline-block';
|
||||
isPaused = true;
|
||||
updatePauseResumeButtons();
|
||||
} else {
|
||||
alert('Failed to pause the download.');
|
||||
}
|
||||
@ -179,13 +194,16 @@
|
||||
.then(response => {
|
||||
if (response.ok) {
|
||||
console.log('Resume signal sent. The download will resume soon.');
|
||||
document.getElementById('resume-button').style.display = 'none';
|
||||
document.getElementById('pause-button').style.display = 'inline-block';
|
||||
isPaused = false;
|
||||
updatePauseResumeButtons();
|
||||
updateProgress();
|
||||
} else {
|
||||
alert('Failed to resume the download.');
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
updateProgress();
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
|
Loading…
Reference in New Issue
Block a user