feat: refactor task module

This commit is contained in:
Andy Hsu
2023-11-20 18:01:51 +08:00
parent de9647a5fa
commit 11a30c5044
14 changed files with 405 additions and 377 deletions

View File

@ -74,9 +74,10 @@ func OfflineDownloadTools(c *gin.Context) {
}
type AddOfflineDownloadReq struct {
Urls []string `json:"urls"`
Path string `json:"path"`
Tool string `json:"tool"`
Urls []string `json:"urls"`
Path string `json:"path"`
Tool string `json:"tool"`
DeletePolicy string `json:"delete_policy"`
}
func AddOfflineDownload(c *gin.Context) {
@ -98,9 +99,10 @@ func AddOfflineDownload(c *gin.Context) {
}
for _, url := range req.Urls {
err := tool.AddURL(c, &tool.AddURLArgs{
URL: url,
DstDirPath: reqPath,
Tool: req.Tool,
URL: url,
DstDirPath: reqPath,
Tool: req.Tool,
DeletePolicy: tool.DeletePolicy(req.DeletePolicy),
})
if err != nil {
common.ErrorResp(c, err, 500)

View File

@ -1,122 +1,77 @@
package handles
import (
"strconv"
"github.com/alist-org/alist/v3/internal/fs"
"github.com/alist-org/alist/v3/internal/offline_download/tool"
"github.com/alist-org/alist/v3/pkg/task"
"github.com/alist-org/alist/v3/server/common"
"github.com/gin-gonic/gin"
"github.com/xhofe/tache"
)
type TaskInfo struct {
ID string `json:"id"`
Name string `json:"name"`
State string `json:"state"`
Status string `json:"status"`
Progress float64 `json:"progress"`
Error string `json:"error"`
ID string `json:"id"`
Name string `json:"name"`
State tache.State `json:"state"`
Status string `json:"status"`
Progress float64 `json:"progress"`
Error string `json:"error"`
}
type K2Str[K comparable] func(k K) string
func uint64K2Str(k uint64) string {
return strconv.FormatUint(k, 10)
}
func strK2Str(str string) string {
return str
}
func getTaskInfo[K comparable](task *task.Task[K], k2Str K2Str[K]) TaskInfo {
func getTaskInfo[T tache.TaskWithInfo](task T) TaskInfo {
return TaskInfo{
ID: k2Str(task.ID),
Name: task.Name,
ID: task.GetID(),
Name: task.GetName(),
State: task.GetState(),
Status: task.GetStatus(),
Progress: task.GetProgress(),
Error: task.GetErrMsg(),
Error: task.GetErr().Error(),
}
}
func getTaskInfos[K comparable](tasks []*task.Task[K], k2Str K2Str[K]) []TaskInfo {
func getTaskInfos[T tache.TaskWithInfo](tasks []T) []TaskInfo {
var infos []TaskInfo
for _, t := range tasks {
infos = append(infos, getTaskInfo(t, k2Str))
infos = append(infos, getTaskInfo(t))
}
return infos
}
type Str2K[K comparable] func(str string) (K, error)
func str2Uint64K(str string) (uint64, error) {
return strconv.ParseUint(str, 10, 64)
}
func str2StrK(str string) (string, error) {
return str, nil
}
func taskRoute[K comparable](g *gin.RouterGroup, manager *task.Manager[K], k2Str K2Str[K], str2K Str2K[K]) {
func taskRoute[T tache.TaskWithInfo](g *gin.RouterGroup, manager *tache.Manager[T]) {
g.GET("/undone", func(c *gin.Context) {
common.SuccessResp(c, getTaskInfos(manager.ListUndone(), k2Str))
common.SuccessResp(c, getTaskInfos(manager.GetByState(tache.StatePending, tache.StateRunning,
tache.StateCanceling, tache.StateErrored, tache.StateFailing, tache.StateWaitingRetry, tache.StateBeforeRetry)))
})
g.GET("/done", func(c *gin.Context) {
common.SuccessResp(c, getTaskInfos(manager.ListDone(), k2Str))
common.SuccessResp(c, getTaskInfos(manager.GetByState(tache.StateCanceled, tache.StateFailed, tache.StateSucceeded)))
})
g.POST("/cancel", func(c *gin.Context) {
tid := c.Query("tid")
id, err := str2K(tid)
if err != nil {
common.ErrorResp(c, err, 400)
return
}
if err := manager.Cancel(id); err != nil {
common.ErrorResp(c, err, 500)
} else {
common.SuccessResp(c)
}
manager.Cancel(tid)
common.SuccessResp(c)
})
g.POST("/delete", func(c *gin.Context) {
tid := c.Query("tid")
id, err := str2K(tid)
if err != nil {
common.ErrorResp(c, err, 400)
return
}
if err := manager.Remove(id); err != nil {
common.ErrorResp(c, err, 500)
} else {
common.SuccessResp(c)
}
manager.Remove(tid)
common.SuccessResp(c)
})
g.POST("/retry", func(c *gin.Context) {
tid := c.Query("tid")
id, err := str2K(tid)
if err != nil {
common.ErrorResp(c, err, 400)
return
}
if err := manager.Retry(id); err != nil {
common.ErrorResp(c, err, 500)
} else {
common.SuccessResp(c)
}
manager.Retry(tid)
common.SuccessResp(c)
})
g.POST("/clear_done", func(c *gin.Context) {
manager.ClearDone()
manager.RemoveByState(tache.StateCanceled, tache.StateFailed, tache.StateSucceeded)
common.SuccessResp(c)
})
g.POST("/clear_succeeded", func(c *gin.Context) {
manager.ClearSucceeded()
manager.RemoveByState(tache.StateSucceeded)
common.SuccessResp(c)
})
}
func SetupTaskRoute(g *gin.RouterGroup) {
taskRoute(g.Group("/upload"), fs.UploadTaskManager, uint64K2Str, str2Uint64K)
taskRoute(g.Group("/copy"), fs.CopyTaskManager, uint64K2Str, str2Uint64K)
taskRoute(g.Group("/offline_download"), tool.DownTaskManager, strK2Str, str2StrK)
taskRoute(g.Group("/offline_download_transfer"), tool.TransferTaskManager, uint64K2Str, str2Uint64K)
taskRoute(g.Group("/upload"), fs.UploadTaskManager)
taskRoute(g.Group("/copy"), fs.CopyTaskManager)
taskRoute(g.Group("/offline_download"), tool.DownloadTaskManager)
taskRoute(g.Group("/offline_download_transfer"), tool.TransferTaskManager)
}