feat: add task info to resp of add task api (close #5579)

This commit is contained in:
Andy Hsu
2023-12-03 14:44:20 +08:00
parent 8bdfc7ac8e
commit 026e944cbb
8 changed files with 79 additions and 50 deletions

View File

@ -2,6 +2,7 @@ package handles
import (
"fmt"
"github.com/xhofe/tache"
"io"
stdpath "path"
@ -120,22 +121,20 @@ func FsCopy(c *gin.Context) {
common.ErrorResp(c, err, 403)
return
}
var addedTask []string
var addedTasks []tache.TaskWithInfo
for i, name := range req.Names {
ok, err := fs.Copy(c, stdpath.Join(srcDir, name), dstDir, len(req.Names) > i+1)
if ok {
addedTask = append(addedTask, name)
t, err := fs.Copy(c, stdpath.Join(srcDir, name), dstDir, len(req.Names) > i+1)
if t != nil {
addedTasks = append(addedTasks, t)
}
if err != nil {
common.ErrorResp(c, err, 500)
return
}
}
if len(addedTask) > 0 {
common.SuccessResp(c, fmt.Sprintf("Added %d tasks", len(addedTask)))
} else {
common.SuccessResp(c)
}
common.SuccessResp(c, gin.H{
"tasks": getTaskInfos(addedTasks),
})
}
type RenameReq struct {

View File

@ -1,6 +1,7 @@
package handles
import (
"github.com/xhofe/tache"
"io"
"net/url"
stdpath "path"
@ -57,8 +58,9 @@ func FsStream(c *gin.Context) {
Mimetype: c.GetHeader("Content-Type"),
WebPutAsTask: asTask,
}
var t tache.TaskWithInfo
if asTask {
err = fs.PutAsTask(dir, s)
t, err = fs.PutAsTask(dir, s)
} else {
err = fs.PutDirectly(c, dir, s, true)
}
@ -67,7 +69,13 @@ func FsStream(c *gin.Context) {
common.ErrorResp(c, err, 500)
return
}
common.SuccessResp(c)
if t == nil {
common.SuccessResp(c)
return
}
common.SuccessResp(c, gin.H{
"task": getTaskInfo(t),
})
}
func FsForm(c *gin.Context) {
@ -115,11 +123,12 @@ func FsForm(c *gin.Context) {
Mimetype: file.Header.Get("Content-Type"),
WebPutAsTask: asTask,
}
var t tache.TaskWithInfo
if asTask {
s.Reader = struct {
io.Reader
}{f}
err = fs.PutAsTask(dir, &s)
t, err = fs.PutAsTask(dir, &s)
} else {
ss, err := stream.NewSeekableStream(s, nil)
if err != nil {
@ -132,5 +141,11 @@ func FsForm(c *gin.Context) {
common.ErrorResp(c, err, 500)
return
}
common.SuccessResp(c)
if t == nil {
common.SuccessResp(c)
return
}
common.SuccessResp(c, gin.H{
"task": getTaskInfo(t),
})
}

View File

@ -7,6 +7,7 @@ import (
"github.com/alist-org/alist/v3/internal/op"
"github.com/alist-org/alist/v3/server/common"
"github.com/gin-gonic/gin"
"github.com/xhofe/tache"
)
type SetAria2Req struct {
@ -97,8 +98,9 @@ func AddOfflineDownload(c *gin.Context) {
common.ErrorResp(c, err, 403)
return
}
var tasks []tache.TaskWithInfo
for _, url := range req.Urls {
err := tool.AddURL(c, &tool.AddURLArgs{
t, err := tool.AddURL(c, &tool.AddURLArgs{
URL: url,
DstDirPath: reqPath,
Tool: req.Tool,
@ -108,6 +110,9 @@ func AddOfflineDownload(c *gin.Context) {
common.ErrorResp(c, err, 500)
return
}
tasks = append(tasks, t)
}
common.SuccessResp(c)
common.SuccessResp(c, gin.H{
"tasks": getTaskInfos(tasks),
})
}

View File

@ -3,6 +3,7 @@ package handles
import (
"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/utils"
"github.com/alist-org/alist/v3/server/common"
"github.com/gin-gonic/gin"
"github.com/xhofe/tache"
@ -33,11 +34,7 @@ func getTaskInfo[T tache.TaskWithInfo](task T) TaskInfo {
}
func getTaskInfos[T tache.TaskWithInfo](tasks []T) []TaskInfo {
var infos []TaskInfo
for _, t := range tasks {
infos = append(infos, getTaskInfo(t))
}
return infos
return utils.MustSliceConvert(tasks, getTaskInfo[T])
}
func taskRoute[T tache.TaskWithInfo](g *gin.RouterGroup, manager *tache.Manager[T]) {
@ -48,6 +45,15 @@ func taskRoute[T tache.TaskWithInfo](g *gin.RouterGroup, manager *tache.Manager[
g.GET("/done", func(c *gin.Context) {
common.SuccessResp(c, getTaskInfos(manager.GetByState(tache.StateCanceled, tache.StateFailed, tache.StateSucceeded)))
})
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))
})
g.POST("/cancel", func(c *gin.Context) {
tid := c.Query("tid")
manager.Cancel(tid)