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"
)
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()
defer progressMutex.Unlock()
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,
}
jobs := make(map[string]*ProgressInfo)
for k, v := range progress {
jobs[k] = v
}
progressMutex.Unlock()
err := templates.ExecuteTemplate(w, "index", struct {
Jobs map[string]struct {
Percentage float64
CurrentFile string
Paused bool
}
}{jobsInfo})
err := templates.ExecuteTemplate(w, "index", struct{ Jobs map[string]*ProgressInfo }{jobs})
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
@ -178,12 +157,6 @@ 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)
}
@ -206,13 +179,6 @@ 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)
}
@ -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"
type ProgressInfo struct {
Percentage float64
CurrentFile string
}
var templates *template.Template
//go:embed templates
@ -82,6 +87,16 @@ 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()

View File

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

View File

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