fix: resolve critical issues and improve task management
- Fix path traversal vulnerability in downloadVideo handler by adding download directory whitelist validation - Add graceful shutdown with signal handling for task persistence - Fix division by zero panic in progressBar.percent() when total <= 0 - Add GetDB() function that returns error instead of using log.Fatal - Change deleteTask to only remove database records, preserve downloaded files - Add paused status for interrupted tasks on shutdown Co-Authored-By: Claude
This commit is contained in:
@@ -108,6 +108,18 @@ func CancelTask(taskID int64) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// MarkAllTasksPaused 标记所有活跃任务为暂停状态,用于程序退出时保存状态
|
||||
func MarkAllTasksPaused() {
|
||||
GlobalTaskMux.Lock()
|
||||
defer GlobalTaskMux.Unlock()
|
||||
for _, task := range GlobalTaskList {
|
||||
if task.Status == "waiting" || task.Status == "running" {
|
||||
task.Cancelled = true
|
||||
task.Status = "paused"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (task *Task) Create(db *sql.DB) error {
|
||||
util.SqliteLock.Lock()
|
||||
result, err := db.Exec(`INSERT INTO "task" ("bvid", "cid", "format", "title", "owner", "cover", "status", "folder", "duration", "download_type")
|
||||
@@ -463,6 +475,10 @@ func (p *progressBar) add(n int) {
|
||||
}
|
||||
|
||||
func (p *progressBar) percent() float64 {
|
||||
// 防止除零错误:total 为 0 或负数时返回 0
|
||||
if p.total <= 0 {
|
||||
return 0
|
||||
}
|
||||
return float64(p.current) / float64(p.total)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user