Compare commits

..

No commits in common. "64a6eb20a0865411bc6abdd775a106130eececc0" and "0b3797dc196eaea962767d210efd1666642b02b6" have entirely different histories.

4 changed files with 34 additions and 105 deletions

View File

@ -9,41 +9,20 @@ import (
"path/filepath" "path/filepath"
) )
type ProgressInfo struct {
Percentage float64
CurrentFile string
Paused bool
}
func handleRoot(w http.ResponseWriter, r *http.Request) { func handleRoot(w http.ResponseWriter, r *http.Request) {
progressMutex.Lock() if r.URL.Path != "/" {
defer progressMutex.Unlock() http.NotFound(w, r)
return
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 { progressMutex.Lock()
Jobs map[string]struct { jobs := make(map[string]*ProgressInfo)
Percentage float64 for k, v := range progress {
CurrentFile string jobs[k] = v
Paused bool }
} progressMutex.Unlock()
}{jobsInfo})
err := templates.ExecuteTemplate(w, "index", struct{ Jobs map[string]*ProgressInfo }{jobs})
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)
} }
@ -178,12 +157,6 @@ func handlePause(w http.ResponseWriter, r *http.Request) {
jobInfo.Cmd.Process.Kill() 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) fmt.Fprintf(w, "Pause signal sent for %s", filename)
} }
@ -206,13 +179,6 @@ func handleResume(w http.ResponseWriter, r *http.Request) {
jobInfo.Paused = false jobInfo.Paused = false
jobInfo.ResumeChan <- struct{}{} 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) fmt.Fprintf(w, "Resume signal sent for %s", filename)
} }
@ -266,30 +232,3 @@ 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
View File

@ -37,6 +37,11 @@ var progress = make(map[string]*ProgressInfo)
const uploadDir = "uploads" const uploadDir = "uploads"
type ProgressInfo struct {
Percentage float64
CurrentFile string
}
var templates *template.Template var templates *template.Template
//go:embed templates //go:embed templates
@ -82,6 +87,16 @@ func startWebServer() {
http.ListenAndServe(":8080", nil) 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 { func getProgress(filename string) *ProgressInfo {
progressMutex.Lock() progressMutex.Lock()
defer progressMutex.Unlock() defer progressMutex.Unlock()

View File

@ -74,9 +74,6 @@
display: inline-block; display: inline-block;
width: 5em; width: 5em;
} }
.paused {
color: #ffa500;
}
@media (max-width: 600px) { @media (max-width: 600px) {
body { body {
padding: 10px; padding: 10px;
@ -123,11 +120,7 @@
<a href="/progress?filename={{$filename}}">{{$filename}}</a> <a href="/progress?filename={{$filename}}">{{$filename}}</a>
</div> </div>
<div class="job-info"> <div class="job-info">
Progress: <span class="progress-text">{{printf "%5.1f%%" $info.Percentage}}</span> Progress: <span class="progress-text">{{printf "%5.1f%%" $info.Percentage}}</span> Current file: {{$info.CurrentFile}}
Current file: {{$info.CurrentFile}}
{{if $info.Paused}}
<span class="paused">(Paused)</span>
{{end}}
</div> </div>
</li> </li>
{{else}} {{else}}

View File

@ -127,12 +127,10 @@
<div> <div>
<button id="abort-button" onclick="abortDownload()">Abort Download</button> <button id="abort-button" onclick="abortDownload()">Abort Download</button>
<button id="pause-button" onclick="pauseDownload()">Pause Download</button> <button id="pause-button" onclick="pauseDownload()">Pause Download</button>
<button id="resume-button" onclick="resumeDownload()" style="display: none;">Resume Download</button> <button id="resume-button" onclick="resumeDownload()">Resume Download</button>
<button id="back-button" onclick="window.location.href='/'">Back to Index</button> <button id="back-button" onclick="window.location.href='/'">Back to Index</button>
</div> </div>
<script> <script>
let isPaused = false;
function updateProgress() { function updateProgress() {
fetch('/progress?filename={{.Filename}}', { fetch('/progress?filename={{.Filename}}', {
headers: { headers: {
@ -145,25 +143,12 @@
document.getElementById('progress-bar').style.width = progress + '%'; document.getElementById('progress-bar').style.width = progress + '%';
document.getElementById('progress-text').innerText = progress + '%'; document.getElementById('progress-text').innerText = progress + '%';
document.getElementById('currentFile').innerText = 'Current file: ' + (data.CurrentFile || 'None'); document.getElementById('currentFile').innerText = 'Current file: ' + (data.CurrentFile || 'None');
if (progress < 100) {
isPaused = data.Paused;
updatePauseResumeButtons();
if (progress < 100 && !isPaused) {
setTimeout(updateProgress, 1000); 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() { function abortDownload() {
fetch('/abort?filename={{.Filename}}', { method: 'POST' }) fetch('/abort?filename={{.Filename}}', { method: 'POST' })
@ -181,8 +166,8 @@
.then(response => { .then(response => {
if (response.ok) { if (response.ok) {
console.log('Pause signal sent. The download will pause soon.'); console.log('Pause signal sent. The download will pause soon.');
isPaused = true; document.getElementById('pause-button').style.display = 'none';
updatePauseResumeButtons(); document.getElementById('resume-button').style.display = 'inline-block';
} else { } else {
alert('Failed to pause the download.'); alert('Failed to pause the download.');
} }
@ -194,16 +179,13 @@
.then(response => { .then(response => {
if (response.ok) { if (response.ok) {
console.log('Resume signal sent. The download will resume soon.'); console.log('Resume signal sent. The download will resume soon.');
isPaused = false; document.getElementById('resume-button').style.display = 'none';
updatePauseResumeButtons(); document.getElementById('pause-button').style.display = 'inline-block';
updateProgress();
} else { } else {
alert('Failed to resume the download.'); alert('Failed to resume the download.');
} }
}); });
} }
updateProgress();
</script> </script>
</body> </body>
</html> </html>