2022-07-11 09:12:50 +00:00
|
|
|
package handles
|
2022-06-29 10:36:14 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/alist-org/alist/v3/internal/fs"
|
2023-11-06 08:56:55 +00:00
|
|
|
"github.com/alist-org/alist/v3/internal/offline_download/tool"
|
2023-12-03 06:44:20 +00:00
|
|
|
"github.com/alist-org/alist/v3/pkg/utils"
|
2022-06-29 10:36:14 +00:00
|
|
|
"github.com/alist-org/alist/v3/server/common"
|
|
|
|
"github.com/gin-gonic/gin"
|
2023-11-20 10:01:51 +00:00
|
|
|
"github.com/xhofe/tache"
|
2022-06-29 10:36:14 +00:00
|
|
|
)
|
|
|
|
|
2022-06-29 12:28:02 +00:00
|
|
|
type TaskInfo struct {
|
2023-11-20 10:01:51 +00:00
|
|
|
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"`
|
2023-02-14 07:20:45 +00:00
|
|
|
}
|
|
|
|
|
2023-11-20 10:01:51 +00:00
|
|
|
func getTaskInfo[T tache.TaskWithInfo](task T) TaskInfo {
|
2023-11-21 07:51:08 +00:00
|
|
|
errMsg := ""
|
|
|
|
if task.GetErr() != nil {
|
|
|
|
errMsg = task.GetErr().Error()
|
|
|
|
}
|
2022-06-29 12:28:02 +00:00
|
|
|
return TaskInfo{
|
2023-11-20 10:01:51 +00:00
|
|
|
ID: task.GetID(),
|
|
|
|
Name: task.GetName(),
|
2022-06-29 12:28:02 +00:00
|
|
|
State: task.GetState(),
|
|
|
|
Status: task.GetStatus(),
|
|
|
|
Progress: task.GetProgress(),
|
2023-11-21 07:51:08 +00:00
|
|
|
Error: errMsg,
|
2022-06-29 12:28:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-20 10:01:51 +00:00
|
|
|
func getTaskInfos[T tache.TaskWithInfo](tasks []T) []TaskInfo {
|
2023-12-03 06:44:20 +00:00
|
|
|
return utils.MustSliceConvert(tasks, getTaskInfo[T])
|
2022-06-29 12:28:02 +00:00
|
|
|
}
|
|
|
|
|
2023-11-20 10:01:51 +00:00
|
|
|
func taskRoute[T tache.TaskWithInfo](g *gin.RouterGroup, manager *tache.Manager[T]) {
|
2023-02-14 07:20:45 +00:00
|
|
|
g.GET("/undone", func(c *gin.Context) {
|
2023-11-20 10:01:51 +00:00
|
|
|
common.SuccessResp(c, getTaskInfos(manager.GetByState(tache.StatePending, tache.StateRunning,
|
|
|
|
tache.StateCanceling, tache.StateErrored, tache.StateFailing, tache.StateWaitingRetry, tache.StateBeforeRetry)))
|
2023-02-14 07:20:45 +00:00
|
|
|
})
|
|
|
|
g.GET("/done", func(c *gin.Context) {
|
2023-11-20 10:01:51 +00:00
|
|
|
common.SuccessResp(c, getTaskInfos(manager.GetByState(tache.StateCanceled, tache.StateFailed, tache.StateSucceeded)))
|
2023-02-14 07:20:45 +00:00
|
|
|
})
|
2023-12-03 06:44:20 +00:00
|
|
|
g.POST("/info", func(c *gin.Context) {
|
|
|
|
tid := c.Query("tid")
|
|
|
|
task, ok := manager.GetByID(tid)
|
|
|
|
if !ok {
|
|
|
|
common.ErrorStrResp(c, "task not found", 404)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
common.SuccessResp(c, getTaskInfo(task))
|
|
|
|
})
|
2023-02-14 07:20:45 +00:00
|
|
|
g.POST("/cancel", func(c *gin.Context) {
|
|
|
|
tid := c.Query("tid")
|
2023-11-20 10:01:51 +00:00
|
|
|
manager.Cancel(tid)
|
|
|
|
common.SuccessResp(c)
|
2023-02-14 07:20:45 +00:00
|
|
|
})
|
|
|
|
g.POST("/delete", func(c *gin.Context) {
|
|
|
|
tid := c.Query("tid")
|
2023-11-20 10:01:51 +00:00
|
|
|
manager.Remove(tid)
|
|
|
|
common.SuccessResp(c)
|
2023-02-14 07:20:45 +00:00
|
|
|
})
|
2023-03-16 07:56:27 +00:00
|
|
|
g.POST("/retry", func(c *gin.Context) {
|
|
|
|
tid := c.Query("tid")
|
2023-11-20 10:01:51 +00:00
|
|
|
manager.Retry(tid)
|
|
|
|
common.SuccessResp(c)
|
2023-03-16 07:56:27 +00:00
|
|
|
})
|
2023-02-14 07:20:45 +00:00
|
|
|
g.POST("/clear_done", func(c *gin.Context) {
|
2023-11-20 10:01:51 +00:00
|
|
|
manager.RemoveByState(tache.StateCanceled, tache.StateFailed, tache.StateSucceeded)
|
2022-06-29 10:36:14 +00:00
|
|
|
common.SuccessResp(c)
|
2023-02-14 07:20:45 +00:00
|
|
|
})
|
2023-03-16 07:56:27 +00:00
|
|
|
g.POST("/clear_succeeded", func(c *gin.Context) {
|
2023-11-20 10:01:51 +00:00
|
|
|
manager.RemoveByState(tache.StateSucceeded)
|
2023-03-16 07:56:27 +00:00
|
|
|
common.SuccessResp(c)
|
|
|
|
})
|
2023-11-21 07:54:42 +00:00
|
|
|
g.POST("/retry_failed", func(c *gin.Context) {
|
|
|
|
manager.RetryAllFailed()
|
|
|
|
common.SuccessResp(c)
|
|
|
|
})
|
2022-07-31 13:21:54 +00:00
|
|
|
}
|
|
|
|
|
2023-02-14 07:20:45 +00:00
|
|
|
func SetupTaskRoute(g *gin.RouterGroup) {
|
2023-11-20 10:01:51 +00:00
|
|
|
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)
|
2022-07-31 13:21:54 +00:00
|
|
|
}
|