Compare commits
3 Commits
0b3797dc19
...
64a6eb20a0
Author | SHA1 | Date | |
---|---|---|---|
64a6eb20a0 | |||
67b17c1df7 | |||
142c09e624 |
83
handlers.go
83
handlers.go
@ -9,20 +9,41 @@ 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) {
|
||||||
if r.URL.Path != "/" {
|
|
||||||
http.NotFound(w, r)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
progressMutex.Lock()
|
progressMutex.Lock()
|
||||||
jobs := make(map[string]*ProgressInfo)
|
defer progressMutex.Unlock()
|
||||||
for k, v := range progress {
|
|
||||||
jobs[k] = v
|
|
||||||
}
|
|
||||||
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 {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
@ -157,6 +178,12 @@ 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)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -179,6 +206,13 @@ 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)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -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"
|
const uploadDir = "uploads"
|
||||||
|
|
||||||
type ProgressInfo struct {
|
|
||||||
Percentage float64
|
|
||||||
CurrentFile string
|
|
||||||
}
|
|
||||||
|
|
||||||
var templates *template.Template
|
var templates *template.Template
|
||||||
|
|
||||||
//go:embed templates
|
//go:embed templates
|
||||||
@ -87,16 +82,6 @@ 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()
|
||||||
|
@ -74,6 +74,9 @@
|
|||||||
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;
|
||||||
@ -120,7 +123,11 @@
|
|||||||
<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> 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>
|
</div>
|
||||||
</li>
|
</li>
|
||||||
{{else}}
|
{{else}}
|
||||||
|
@ -127,10 +127,12 @@
|
|||||||
<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()">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>
|
<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: {
|
||||||
@ -143,12 +145,25 @@
|
|||||||
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' })
|
||||||
@ -166,8 +181,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.');
|
||||||
document.getElementById('pause-button').style.display = 'none';
|
isPaused = true;
|
||||||
document.getElementById('resume-button').style.display = 'inline-block';
|
updatePauseResumeButtons();
|
||||||
} else {
|
} else {
|
||||||
alert('Failed to pause the download.');
|
alert('Failed to pause the download.');
|
||||||
}
|
}
|
||||||
@ -179,13 +194,16 @@
|
|||||||
.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.');
|
||||||
document.getElementById('resume-button').style.display = 'none';
|
isPaused = false;
|
||||||
document.getElementById('pause-button').style.display = 'inline-block';
|
updatePauseResumeButtons();
|
||||||
|
updateProgress();
|
||||||
} else {
|
} else {
|
||||||
alert('Failed to resume the download.');
|
alert('Failed to resume the download.');
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
updateProgress();
|
||||||
</script>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
Loading…
Reference in New Issue
Block a user