Clear jobs

This commit is contained in:
Joren 2024-09-13 22:57:55 +02:00
parent 37c390f911
commit 8aa915e6dc
Signed by: Joren
GPG Key ID: 280E33DFBC0F1B55
4 changed files with 82 additions and 8 deletions

View File

@ -209,3 +209,26 @@ func handleAbort(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Abort signal sent for %s", filename) fmt.Fprintf(w, "Abort signal sent for %s", filename)
} }
func handleClearCompleted(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
clearCompletedJobs()
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(map[string]bool{"success": true})
}
func clearCompletedJobs() {
progressMutex.Lock()
defer progressMutex.Unlock()
for filename, info := range progress {
if info.Percentage >= 100 {
delete(progress, filename)
}
}
}

View File

@ -81,6 +81,7 @@ func startWebServer() {
http.HandleFunc("/abort", handleAbort) http.HandleFunc("/abort", handleAbort)
http.HandleFunc("/pause", handlePause) http.HandleFunc("/pause", handlePause)
http.HandleFunc("/resume", handleResume) http.HandleFunc("/resume", handleResume)
http.HandleFunc("/clear-completed", handleClearCompleted)
fmt.Println("Starting web server on http://0.0.0.0:8080") fmt.Println("Starting web server on http://0.0.0.0:8080")
http.ListenAndServe(":8080", nil) http.ListenAndServe(":8080", nil)

View File

@ -45,6 +45,7 @@
ul { ul {
list-style-type: none; list-style-type: none;
padding: 0; padding: 0;
margin-bottom: 10px;
} }
li { li {
background-color: #2d2d2d; background-color: #2d2d2d;
@ -83,6 +84,25 @@
input[type="file"], input[type="submit"] { input[type="file"], input[type="submit"] {
font-size: 16px; font-size: 16px;
} }
input[type="submit"], #clear-completed {
font-size: 16px;
}
}
input[type="submit"], #clear-completed {
cursor: pointer;
color: white;
border: 1px solid #444;
padding: 8px 12px;
border-radius: 4px;
margin-bottom: 10px;
max-width: 100%;
width: 100%;
}
#clear-completed {
background-color: #f44336;
}
#clear-completed:hover {
background-color: #d32f2f;
} }
</style> </style>
</head> </head>
@ -107,5 +127,19 @@
<li>No active jobs</li> <li>No active jobs</li>
{{end}} {{end}}
</ul> </ul>
<button id="clear-completed" onclick="clearCompleted()">Clear Completed Jobs</button>
<script>
function clearCompleted() {
fetch('/clear-completed', { method: 'POST' })
.then(response => response.json())
.then(data => {
if (data.success) {
location.reload();
} else {
alert('Failed to clear completed jobs');
}
});
}
</script>
</body> </body>
</html> </html>

View File

@ -69,7 +69,7 @@
background-color: #d32f2f; background-color: #d32f2f;
} }
#pause-button, #resume-button { #pause-button, #resume-button {
background-color: #2196F3; background-color: #4CAF50;
color: white; color: white;
border: none; border: none;
padding: 10px 15px; padding: 10px 15px;
@ -78,11 +78,24 @@
cursor: pointer; cursor: pointer;
} }
#pause-button:hover, #resume-button:hover { #pause-button:hover, #resume-button:hover {
background-color: #1976D2; background-color: #45a049;
} }
#resume-button { #resume-button {
display: none; display: none;
} }
#back-button {
background-color: #2196F3;
color: white;
border: none;
padding: 10px 15px;
margin-top: 10px;
border-radius: 4px;
cursor: pointer;
float: right;
}
#back-button:hover {
background-color: #1976D2;
}
@media (max-width: 600px) { @media (max-width: 600px) {
body { body {
padding: 10px; padding: 10px;
@ -111,9 +124,12 @@
</div> </div>
<div id="currentFile"></div> <div id="currentFile"></div>
</div> </div>
<button id="abort-button" onclick="abortDownload()">Abort Download</button> <div>
<button id="pause-button" onclick="pauseDownload()">Pause Download</button> <button id="abort-button" onclick="abortDownload()">Abort Download</button>
<button id="resume-button" onclick="resumeDownload()">Resume Download</button> <button id="pause-button" onclick="pauseDownload()">Pause 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> <script>
function updateProgress() { function updateProgress() {
fetch('/progress?filename={{.Filename}}', { fetch('/progress?filename={{.Filename}}', {
@ -138,7 +154,7 @@
fetch('/abort?filename={{.Filename}}', { method: 'POST' }) fetch('/abort?filename={{.Filename}}', { method: 'POST' })
.then(response => { .then(response => {
if (response.ok) { if (response.ok) {
alert('Abort signal sent. The download will stop soon.'); console.log('Abort signal sent. The download will stop soon.');
} else { } else {
alert('Failed to abort the download.'); alert('Failed to abort the download.');
} }
@ -149,7 +165,7 @@
fetch('/pause?filename={{.Filename}}', { method: 'POST' }) fetch('/pause?filename={{.Filename}}', { method: 'POST' })
.then(response => { .then(response => {
if (response.ok) { if (response.ok) {
alert('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'; document.getElementById('pause-button').style.display = 'none';
document.getElementById('resume-button').style.display = 'inline-block'; document.getElementById('resume-button').style.display = 'inline-block';
} else { } else {
@ -162,7 +178,7 @@
fetch('/resume?filename={{.Filename}}', { method: 'POST' }) fetch('/resume?filename={{.Filename}}', { method: 'POST' })
.then(response => { .then(response => {
if (response.ok) { if (response.ok) {
alert('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'; document.getElementById('resume-button').style.display = 'none';
document.getElementById('pause-button').style.display = 'inline-block'; document.getElementById('pause-button').style.display = 'inline-block';
} else { } else {