8000 feat: support general users view and cancel own tasks (#7416 close #7… · anwen-anyi/alist@64ceb5a · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Commit 64ceb5a

Browse files
authored
feat: support general users view and cancel own tasks (AlistGo#7416 close AlistGo#7398)
* feat: support general users view and cancel own tasks Add a creator attribute to the upload, copy and offline download tasks, so that a GENERAL task creator can view and cancel them. BREAKING CHANGE: 1. A new internal package `task` including the struct `TaskWithCreator` which embeds `tache.Base` is created, and the past dependence on `tache.Task` will all be transferred to dependence on this package. 2. The API `/admin/task` can now also be accessed via `/task`, and the old endpoint is retained to ensure compatibility with legacy automation scripts. Closes AlistGo#7398 * fix(deps): update github.com/xhofe/tache to v0.1.3
1 parent 10c7ebb commit 64ceb5a

File tree

15 files changed

+215
-66
lines changed

15 files changed

+215
-66
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ require (
5656
github.com/u2takey/ffmpeg-go v0.5.0
5757
github.com/upyun/go-sdk/v3 v3.0.4
5858
github.com/winfsp/cgofuse v1.5.1-0.20230130140708-f87f5db493b5
59-
github.com/xhofe/tache v0.1.2
59+
github.com/xhofe/tache v0.1.3
6060
github.com/xhofe/wopan-sdk-go v0.1.3
6161
github.com/zzzhr1990/go-common-entity v0.0.0-20221216044934-fd1c571e3a22
6262
golang.org/x/crypto v0.27.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,8 @@ github.com/xhofe/gsync v0.0.0-20230917091818-2111ceb38a25 h1:eDfebW/yfq9DtG9RO3K
514514
github.com/xhofe/gsync v0.0.0-20230917091818-2111ceb38a25/go.mod h1:fH4oNm5F9NfI5dLi0oIMtsLNKQOirUDbEMCIBb/7SU0=
515515
github.com/xhofe/tache v0.1.2 h1:pHrXlrWcbTb4G7hVUDW7Rc+YTUnLJvnLBrdktVE1Fqg=
516516
github.com/xhofe/tache v0.1.2/go.mod h1:iKumPFvywf30FRpAHHCt64G0JHLMzT0K+wyGedHsmTQ=
517+
github.com/xhofe/tache v0.1.3 h1:MipxzlljYX29E1YI/SLC7hVomVF+51iP1OUzlsuq1wE=
518+
github.com/xhofe/tache v0.1.3/go.mod h1:iKumPFvywf30FRpAHHCt64G0JHLMzT0K+wyGedHsmTQ=
517519
github.com/xhofe/wopan-sdk-go v0.1.3 h1:J58X6v+n25ewBZjb05pKOr7AWGohb+Rdll4CThGh6+A=
518520
github.com/xhofe/wopan-sdk-go v0.1.3/go.mod h1:dcY9yA28fnaoZPnXZiVTFSkcd7GnIPTpTIIlfSI5z5Q=
519521
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=

internal/fs/copy.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@ import (
1111
"github.com/alist-org/alist/v3/internal/model"
1212
"github.com/alist-org/alist/v3/internal/op"
1313
"github.com/alist-org/alist/v3/internal/stream"
14+
"github.com/alist-org/alist/v3/internal/task"
1415
"github.com/alist-org/alist/v3/pkg/utils"
1516
"github.com/pkg/errors"
1617
"github.com/xhofe/tache"
1718
)
1819

1920
type CopyTask struct {
20-
tache.Base
21+
task.TaskWithCreator
2122
Status string `json:"-"` //don't save status to save space
2223
SrcObjPath string `json:"src_path"`
2324
DstDirPath string `json:"dst_path"`
@@ -53,7 +54,7 @@ var CopyTaskManager *tache.Manager[*CopyTask]
5354

5455
// Copy if in the same storage, call move method
5556
// if not, add copy task
56-
func _copy(ctx context.Context, srcObjPath, dstDirPath string, lazyCache ...bool) (tache.TaskWithInfo, error) {
57+
func _copy(ctx context.Context, srcObjPath, dstDirPath string, lazyCache ...bool) (task.TaskInfoWithCreator, error) {
5758
srcStorage, srcObjActualPath, err := op.GetStorageAndActualPath(srcObjPath)
5859
if err != nil {
5960
return nil, errors.WithMessage(err, "failed get src storage")
@@ -92,7 +93,11 @@ func _copy(ctx context.Context, srcObjPath, dstDirPath string, lazyCache ...bool
9293
}
9394
}
9495
// not in the same storage
96+
taskCreator, _ := ctx.Value("user").(*model.User) // taskCreator is nil when convert failed
9597
t := &CopyTask{
98+
TaskWithCreator: task.TaskWithCreator{
99+
Creator: taskCreator,
100+
},
96101
srcStorage: srcStorage,
97102
dstStorage: dstStorage,
98103
SrcObjPath: srcObjActualPath,
@@ -123,6 +128,9 @@ func copyBetween2Storages(t *CopyTask, srcStorage, dstStorage driver.Driver, src
123128
srcObjPath := stdpath.Join(srcObjPath, obj.GetName())
124129
dstObjPath := stdpath.Join(dstDirPath, srcObj.GetName())
125130
CopyTaskManager.Add(&CopyTask{
131+
TaskWithCreator: task.TaskWithCreator{
132+
Creator: t.Creator,
133+
},
126134
srcStorage: srcStorage,
127135
dstStorage: dstStorage,
128136
SrcObjPath: srcObjPath,

internal/fs/fs.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import (
55
"github.com/alist-org/alist/v3/internal/driver"
66
"github.com/alist-org/alist/v3/internal/model"
77
"github.com/alist-org/alist/v3/internal/op"
8+
"github.com/alist-org/alist/v3/internal/task"
89
log "github.com/sirupsen/logrus"
9-
"github.com/xhofe/tache"
1010
)
1111

1212
// the param named path of functions in this package is a mount path
@@ -69,7 +69,7 @@ func Move(ctx context.Context, srcPath, dstDirPath string, lazyCache ...bool) er
6969
return err
7070
}
7171

72-
func Copy(ctx context.Context, srcObjPath, dstDirPath string, lazyCache ...bool) (tache.TaskWithInfo, error) {
72+
func Copy(ctx context.Context, srcObjPath, dstDirPath string, lazyCache ...bool) (task.TaskInfoWithCreator, error) {
7373
res, err := _copy(ctx, srcObjPath, dstDirPath, lazyCache...)
7474
if err != nil {
7575
log.Errorf("failed copy %s to %s: %+v", srcObjPath, dstDirPath, err)
@@ -101,8 +101,8 @@ func PutDirectly(ctx context.Context, dstDirPath string, file model.FileStreamer
101101
return err
102102
}
103103

104-
func PutAsTask(dstDirPath string, file model.FileStreamer) (tache.TaskWithInfo, error) {
105-
t, err := putAsTask(dstDirPath, file)
104+
func PutAsTask(ctx context.Context, dstDirPath string, file model.FileStreamer) (task.TaskInfoWithCreator, error) {
105+
t, err := putAsTask(ctx, dstDirPath, file)
106106
if err != nil {
107107
log.Errorf("failed put %s: %+v", dstDirPath, err)
108108
}

internal/fs/put.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@ import (
77
"github.com/alist-org/alist/v3/internal/errs"
88
"github.com/alist-org/alist/v3/internal/model"
99
"github.com/alist-org/alist/v3/internal/op"
10+
"github.com/alist-org/alist/v3/internal/task"
1011
"github.com/pkg/errors"
1112
"github.com/xhofe/tache"
1213
)
1314

1415
type UploadTask struct {
15-
tache.Base
16+
task.TaskWithCreator
1617
storage driver.Driver
1718
dstDirActualPath string
1819
file model.FileStreamer
@@ -33,7 +34,7 @@ func (t *UploadTask) Run() error {
3334
var UploadTaskManager *tache.Manager[*UploadTask]
3435

3536
// putAsTask add as a put task and return immediately
36-
func putAsTask(dstDirPath string, file model.FileStreamer) (tache.TaskWithInfo, error) {
37+
func putAsTask(ctx context.Context, dstDirPath string, file model.FileStreamer) (task.TaskInfoWithCreator, error) {
3738
storage, dstDirActualPath, err := op.GetStorageAndActualPath(dstDirPath)
3839
if err != nil {
3940
return nil, errors.WithMessage(err, "failed get storage")
@@ -49,7 +50,11 @@ func putAsTask(dstDirPath string, file model.FileStreamer) (tache.TaskWithInfo,
4950
//file.SetReader(tempFile)
5051
//file.SetTmpFile(tempFile)
5152
}
53+
taskCreator, _ := ctx.Value("user").(*model.User) // taskCreator is nil when convert failed
5254
t := &UploadTask{
55+
TaskWithCreator: task.TaskWithCreator{
56+
Creator: taskCreator,
57+
},
5358
storage: storage,
5459
dstDirActualPath: dstDirActualPath,
5560
file: file,

internal/offline_download/tool/add.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ package tool
22

33
import (
44
"context"
5+
"github.com/alist-org/alist/v3/internal/model"
6+
"github.com/alist-org/alist/v3/internal/task"
57
"path/filepath"
68

79
"github.com/alist-org/alist/v3/internal/conf"
810
"github.com/alist-org/alist/v3/internal/errs"
911
"github.com/alist-org/alist/v3/internal/op"
1012
"github.com/google/uuid"
1113
"github.com/pkg/errors"
12-
"github.com/xhofe/tache"
1314
)
1415

1516
type DeletePolicy string
@@ -28,7 +29,7 @@ type AddURLArgs struct {
2829
DeletePolicy DeletePolicy
2930
}
3031

31-
func AddURL(ctx context.Context, args *AddURLArgs) (tache.TaskWithInfo, error) {
32+
func AddURL(ctx context.Context, args *AddURLArgs) (task.TaskInfoWithCreator, error) {
3233
// get tool
3334
tool, err := Tools.Get(args.Tool)
3435
if err != nil {
@@ -77,8 +78,12 @@ func AddURL(ctx context.Context, args *AddURLArgs) (tache.TaskWithInfo, error) {
7778
// 防止将下载好的文件删除
7879
deletePolicy = DeleteNever
7980
}
80-
81+
82+
taskCreator, _ := ctx.Value("user").(*model.User) // taskCreator is nil when convert failed
8183
t := &DownloadTask{
84+
TaskWithCreator: task.TaskWithCreator{
85+
Creator: taskCreator,
86+
},
8287
Url: args.URL,
8388
DstDirPath: args.DstDirPath,
8489
TempDir: tempDir,

internal/offline_download/tool/download.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@ import (
77
"github.com/alist-org/alist/v3/internal/conf"
88
"github.com/alist-org/alist/v3/internal/errs"
99
"github.com/alist-org/alist/v3/internal/setting"
10+
"github.com/alist-org/alist/v3/internal/task"
1011
"github.com/pkg/errors"
1112
log "github.com/sirupsen/logrus"
1213
"github.com/xhofe/tache"
1314
)
1415

1516
type DownloadTask struct {
16-
tache.Base
17+
task.TaskWithCreator
1718
Url string `json:"url"`
1819
DstDirPath string `json:"dst_dir_path"`
1920
TempDir string `json:"temp_dir"`
@@ -171,6 +172,9 @@ func (t *DownloadTask) Complete() error {
171172
for i := range files {
172173
file := files[i]
173174
TransferTaskManager.Add(&TransferTask{
175+
TaskWithCreator: task.TaskWithCreator{
176+
Creator: t.Creator,
177+
},
174178
file: file,
175179
DstDirPath: t.DstDirPath,
176180
TempDir: t.TempDir,

internal/offline_download/tool/transfer.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@ import (
88
"github.com/alist-org/alist/v3/internal/model"
99
"github.com/alist-org/alist/v3/internal/op"
1010
"github.com/alist-org/alist/v3/internal/stream"
11+
"github.com/alist-org/alist/v3/internal/task"
1112
"github.com/alist-org/alist/v3/pkg/utils"
1213
"github.com/pkg/errors"
1314
log "github.com/sirupsen/logrus"
1415
"github.com/xhofe/tache"
1516
)
1617

1718
type TransferTask struct {
18-
tache.Base
19+
task.TaskWithCreator
1920
FileDir string `json:"file_dir"`
2021
DstDirPath string `json:"dst_dir_path"`
2122
TempDir string `json:"temp_dir"`

internal/task/base.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package task
2+
3+
import (
4+
"github.com/alist-org/alist/v3/internal/model"
5+
"github.com/xhofe/tache"
6+
)
7+
8+
type TaskWithCreator struct {
9+
tache.Base
10+
Creator *model.User
11+
}
12+
13+
func (t *TaskWithCreator) SetCreator(creator *model.User) {
14+
t.Creator = creator
15+
t.Persist()
16+
}
17+
18+
func (t *TaskWithCreator) GetCreator() *model.User {
19+
return t.Creator
20+
}
21+
22+
type TaskInfoWithCreator interface {
23+
tache.TaskWithInfo
24+
SetCreator(creator *model.User)
25+
GetCreator() *model.User
26+
}

server/handles/fsmanage.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package handles
22

33
import (
44
"fmt"
5-
"github.com/xhofe/tache"
5+
"github.com/alist-org/alist/v3/internal/task"
66
"io"
77
stdpath "path"
88

@@ -121,7 +121,7 @@ func FsCopy(c *gin.Context) {
121121
common.ErrorResp(c, err, 403)
122122
return
123123
}
124-
var addedTasks []tache.TaskWithInfo
124+
var addedTasks []task.TaskInfoWithCreator
125125
for i, name := range req.Names {
126126
t, err := fs.Copy(c, stdpath.Join(srcDir, name), dstDir, len(req.Names) > i+1)
127127
if t != nil {

server/handles/fsup.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
package handles
22

33
import (
4-
"github.com/xhofe/tache"
4+
"github.com/alist-org/alist/v3/internal/task"
55
"io"
66
"net/url"
77
stdpath "path"
88
"strconv"
99
"time"
1010

11-
"github.com/alist-org/alist/v3/internal/stream"
12-
1311
"github.com/alist-org/alist/v3/internal/fs"
1412
"github.com/alist-org/alist/v3/internal/model"
13+
"github.com/alist-org/alist/v3/internal/stream"
1514
"github.com/alist-org/alist/v3/server/common"
1615
"github.com/gin-gonic/gin"
1716
)
@@ -58,9 +57,9 @@ func FsStream(c *gin.Context) {
5857
Mimetype: c.GetHeader("Content-Type"),
5958
WebPutAsTask: asTask,
6059
}
61-
var t tache.TaskWithInfo
60+
var t task.TaskInfoWithCreator
6261
if asTask {
63-
t, err = fs.PutAsTask(dir, s)
62+
t, err = fs.PutAsTask(c, dir, s)
6463
} else {
6564
err = fs.PutDirectly(c, dir, s, true)
6665
}
@@ -123,12 +122,12 @@ func FsForm(c *gin.Context) {
123122
Mimetype: file.Header.Get("Content-Type"),
124123
WebPutAsTask: asTask,
125124
}
126-
var t tache.TaskWithInfo
125+
var t task.TaskInfoWithCreator
127126
if asTask {
128127
s.Reader = struct {
129128
io.Reader
130129
}{f}
131-
t, err = fs.PutAsTask(dir, &s)
130+
t, err = fs.PutAsTask(c, dir, &s)
132131
} else {
133132
ss, err := stream.NewSeekableStream(s, nil)
134133
if err != nil {

server/handles/offline_download.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import (
55
"github.com/alist-org/alist/v3/internal/model"
66
"github.com/alist-org/alist/v3/internal/offline_download/tool"
77
"github.com/alist-org/alist/v3/internal/op"
8+
"github.com/alist-org/alist/v3/internal/task"
89
"github.com/alist-org/alist/v3/server/common"
910
"github.com/gin-gonic/gin"
10-
"github.com/xhofe/tache"
1111
)
1212

1313
type SetAria2Req struct {
@@ -133,7 +133,7 @@ func AddOfflineDownload(c *gin.Context) {
133133
common.ErrorResp(c, err, 403)
134134
return
135135
}
136-
var tasks []tache.TaskWithInfo
136+
var tasks []task.TaskInfoWithCreator
137137
for _, url := range req.Urls {
138138
t, err := tool.AddURL(c, &tool.AddURLArgs{
139139
URL: url,

0 commit comments

Comments
 (0)
0