@@ -6,11 +6,9 @@ import (
6
6
"fmt"
7
7
"io"
8
8
"math/rand"
9
- "mime"
10
9
"os"
11
10
stdpath "path"
12
11
"path/filepath"
13
- "strconv"
14
12
"strings"
15
13
"time"
16
14
@@ -21,30 +19,22 @@ import (
21
19
"github.com/OpenListTeam/OpenList/v4/internal/op"
22
20
"github.com/OpenListTeam/OpenList/v4/internal/stream"
23
21
"github.com/OpenListTeam/OpenList/v4/internal/task"
22
+ "github.com/OpenListTeam/OpenList/v4/internal/task_group"
23
+ "github.com/OpenListTeam/OpenList/v4/pkg/utils"
24
+ "github.com/OpenListTeam/OpenList/v4/server/common"
24
25
"github.com/OpenListTeam/tache"
25
26
"github.com/pkg/errors"
26
27
log "github.com/sirupsen/logrus"
27
28
)
28
29
29
30
type ArchiveDownloadTask struct {
30
- task. TaskExtension
31
+ TaskData
31
32
model.ArchiveDecompressArgs
32
- status string
33
- SrcObjPath string
34
- DstDirPath string
35
- srcStorage driver.Driver
36
- dstStorage driver.Driver
37
- SrcStorageMp string
38
- DstStorageMp string
39
33
}
40
34
41
35
func (t * ArchiveDownloadTask ) GetName () string {
42
- return fmt .Sprintf ("decompress [%s](%s)[%s] to [%s](%s) with password <%s>" , t .SrcStorageMp , t .SrcObjPath ,
43
- t .InnerPath , t .DstStorageMp , t .DstDirPath , t .Password )
44
- }
45
-
46
- func (t * ArchiveDownloadTask ) GetStatus () string {
47
- return t .status
36
+ return fmt .Sprintf ("decompress [%s](%s)[%s] to [%s](%s) with password <%s>" , t .SrcStorageMp , t .SrcActualPath ,
37
+ t .InnerPath , t .DstStorageMp , t .DstActualPath , t .Password )
48
38
}
49
39
50
40
func (t * ArchiveDownloadTask ) Run () error {
@@ -58,16 +48,21 @@ func (t *ArchiveDownloadTask) Run() error {
58
48
if err != nil {
59
49
return err
60
50
}
51
+ uploadTask .groupID = stdpath .Join (uploadTask .DstStorageMp , uploadTask .DstActualPath )
52
+ task_group .TransferCoordinator .AddTask (uploadTask .groupID , nil )
61
53
ArchiveContentUploadTaskManager .Add (uploadTask )
62
54
return nil
63
55
}
64
56
65
57
func (t * ArchiveDownloadTask ) RunWithoutPushUploadTask () (* ArchiveContentUploadTask , error ) {
66
58
var err error
67
- if t .srcStorage == nil {
68
- t .srcStorage , err = op .GetStorageByMountPath (t .SrcStorageMp )
59
+ if t .SrcStorage == nil {
60
+ t .SrcStorage , err = op .GetStorageByMountPath (t .SrcStorageMp )
61
+ if err != nil {
62
+ return nil , err
63
+ }
69
64
}
70
- srcObj , tool , ss , err := op .GetArchiveToolAndStream (t .Ctx (), t .srcStorage , t .SrcObjPath , model.LinkArgs {})
65
+ srcObj , tool , ss , err := op .GetArchiveToolAndStream (t .Ctx (), t .SrcStorage , t .SrcActualPath , model.LinkArgs {})
71
66
if err != nil {
72
67
return nil , err
73
68
}
@@ -87,7 +82,7 @@ func (t *ArchiveDownloadTask) RunWithoutPushUploadTask() (*ArchiveContentUploadT
87
82
total += s .GetSize ()
88
83
}
89
84
t .SetTotalBytes (total )
90
- t .status = "getting src object"
85
+ t .Status = "getting src object"
91
86
for _ , s := range ss {
92
87
if s .GetFile () == nil {
93
88
_ , err = stream .CacheFullInTempFileAndWriter (s , func (p float64 ) {
@@ -104,7 +99,7 @@ func (t *ArchiveDownloadTask) RunWithoutPushUploadTask() (*ArchiveContentUploadT
104
99
} else {
105
100
decompressUp = t .SetProgress
106
101
}
107
- t .status = "walking and decompressing"
102
+ t .Status = "walking and decompressing"
108
103
dir , err := os .MkdirTemp (conf .Conf .TempDir , "dir-*" )
109
104
if err != nil {
110
105
return nil , err
@@ -117,13 +112,14 @@ func (t *ArchiveDownloadTask) RunWithoutPushUploadTask() (*ArchiveContentUploadT
117
112
uploadTask := & ArchiveContentUploadTask {
118
113
TaskExtension : task.TaskExtension {
119
114
Creator : t .GetCreator (),
115
+ ApiUrl : t .ApiUrl ,
120
116
},
121
- ObjName : baseName ,
122
- InPlace : ! t .PutIntoNewDir ,
123
- FilePath : dir ,
124
- DstDirPath : t . DstDirPath ,
125
- dstStorage : t . dstStorage ,
126
- DstStorageMp : t .DstStorageMp ,
117
+ ObjName : baseName ,
118
+ InPlace : ! t .PutIntoNewDir ,
119
+ FilePath : dir ,
120
+ DstActualPath : t . DstActualPath ,
121
+ dstStorage : t . DstStorage ,
122
+ DstStorageMp : t .DstStorageMp ,
127
123
}
128
124
return uploadTask , nil
129
125
}
@@ -132,18 +128,19 @@ var ArchiveDownloadTaskManager *tache.Manager[*ArchiveDownloadTask]
132
128
133
129
type ArchiveContentUploadTask struct {
134
130
task.TaskExtension
135
- status string
136
- ObjName string
137
- InPlace bool
138
- FilePath string
139
- DstDirPath string
140
- dstStorage driver.Driver
141
- DstStorageMp string
142
- finalized bool
131
+ status string
132
+ ObjName string
133
+ InPlace bool
134
+ FilePath string
135
+ DstActualPath string
136
+ dstStorage driver.Driver
137
+ DstStorageMp string
138
+ finalized bool
139
+ groupID string
143
140
}
144
141
145
142
func (t * ArchiveContentUploadTask ) GetName () string {
146
- return fmt .Sprintf ("upload %s to [%s](%s)" , t .ObjName , t .DstStorageMp , t .DstDirPath )
143
+ return fmt .Sprintf ("upload %s to [%s](%s)" , t .ObjName , t .DstStorageMp , t .DstActualPath )
147
144
}
148
145
149
146
func (t * ArchiveContentUploadTask ) GetStatus () string {
@@ -163,21 +160,42 @@ func (t *ArchiveContentUploadTask) Run() error {
163
160
})
164
161
}
165
162
166
- func (t * ArchiveContentUploadTask ) RunWithNextTaskCallback (f func (nextTsk * ArchiveContentUploadTask ) error ) error {
163
+ func (t * ArchiveContentUploadTask ) OnSucceeded () {
164
+ task_group .TransferCoordinator .Done (t .groupID , true )
165
+ }
166
+
167
+ func (t * ArchiveContentUploadTask ) OnFailed () {
168
+ task_group .TransferCoordinator .Done (t .groupID , false )
169
+ }
170
+
171
+ func (t * ArchiveContentUploadTask ) SetRetry (retry int , maxRetry int ) {
172
+ t .TaskExtension .SetRetry (retry , maxRetry )
173
+ if retry == 0 &&
174
+ (len (t .groupID ) == 0 || // 重启恢复
175
+ (t .GetErr () == nil && t .GetState () != tache .StatePending )) { // 手动重试
176
+ t .groupID = stdpath .Join (t .DstStorageMp , t .DstActualPath )
177
+ task_group .TransferCoordinator .AddTask (t .groupID , nil )
178
+ }
179
+ }
180
+
181
+ func (t * ArchiveContentUploadTask ) RunWithNextTaskCallback (f func (nextTask * ArchiveContentUploadTask ) error ) error {
167
182
var err error
168
183
if t .dstStorage == nil {
169
184
t .dstStorage , err = op .GetStorageByMountPath (t .DstStorageMp )
185
+ if err != nil {
186
+ return err
187
+ }
170
188
}
171
189
info , err := os .Stat (t .FilePath )
172
190
if err != nil {
173
191
return err
174
192
}
175
193
if info .IsDir () {
176
194
t .status = "src object is dir, listing objs"
177
- nextDstPath := t .DstDirPath
195
+ nextDstActualPath := t .DstActualPath
178
196
if ! t .InPlace {
179
- nextDstPath = stdpath .Join (nextDstPath , t .ObjName )
180
- err = op .MakeDir (t .Ctx (), t .dstStorage , nextDstPath )
197
+ nextDstActualPath = stdpath .Join (nextDstActualPath , t .ObjName )
198
+ err = op .MakeDir (t .Ctx (), t .dstStorage , nextDstActualPath )
181
199
if err != nil {
182
200
return err
183
201
}
@@ -186,6 +204,9 @@ func (t *ArchiveContentUploadTask) RunWithNextTaskCallback(f func(nextTsk *Archi
186
204
if err != nil {
187
205
return err
188
206
}
207
+ if ! t .InPlace && len (t .groupID ) > 0 {
208
+ task_group .TransferCoordinator .AppendPayload (t .groupID , task_group .DstPathToRefresh (nextDstActualPath ))
209
+ }
189
210
var es error
190
211
for _ , entry := range entries {
191
212
var nextFilePath string
@@ -198,16 +219,21 @@ func (t *ArchiveContentUploadTask) RunWithNextTaskCallback(f func(nextTsk *Archi
198
219
es = stderrors .Join (es , err )
199
220
continue
200
221
}
222
+ if len (t .groupID ) > 0 {
223
+ task_group .TransferCoordinator .AddTask (t .groupID , nil )
224
+ }
201
225
err = f (& ArchiveContentUploadTask {
202
226
TaskExtension : task.TaskExtension {
203
227
Creator : t .GetCreator (),
228
+ ApiUrl : t .ApiUrl ,
204
229
},
205
- ObjName : entry .Name (),
206
- InPlace : false ,
207
- FilePath : nextFilePath ,
208
- DstDirPath : nextDstPath ,
209
- dstStorage : t .dstStorage ,
210
- DstStorageMp : t .DstStorageMp ,
230
+ ObjName : entry .Name (),
231
+ InPlace : false ,
232
+ FilePath : nextFilePath ,
233
+ DstActualPath : nextDstActualPath ,
234
+ dstStorage : t .dstStorage ,
235
+ DstStorageMp : t .DstStorageMp ,
236
+ groupID : t .groupID ,
211
237
})
212
238
if err != nil {
213
239
es = stderrors .Join (es , err )
@@ -228,13 +254,13 @@ func (t *ArchiveContentUploadTask) RunWithNextTaskCallback(f func(nextTsk *Archi
228
254
Size : info .Size (),
229
255
Modified : time .Now (),
230
256
},
231
- Mimetype : mime . TypeByExtension ( filepath .Ext (t .ObjName )),
257
+ Mimetype : utils . GetMimeType ( stdpath .Ext (t .ObjName )),
232
258
WebPutAsTask : true ,
233
259
Reader : file ,
234
260
}
235
261
fs .Closers .Add (file )
236
262
t .status = "uploading"
237
- err = op .Put (t .Ctx (), t .dstStorage , t .DstDirPath , fs , t .SetProgress , true )
263
+ err = op .Put (t .Ctx (), t .dstStorage , t .DstActualPath , fs , t .SetProgress , true )
238
264
if err != nil {
239
265
return err
240
266
}
@@ -271,8 +297,9 @@ func moveToTempPath(path, prefix string) (string, error) {
271
297
272
298
func genTempFileName (prefix string ) (string , error ) {
273
299
retry := 0
300
+ t := time .Now ().UnixMilli ()
274
301
for retry < 10000 {
275
- newPath := stdpath .Join (conf .Conf .TempDir , prefix + strconv . FormatUint ( uint64 ( rand .Uint32 ()), 10 ))
302
+ newPath := filepath .Join (conf .Conf .TempDir , prefix + fmt . Sprintf ( "%x-%x" , t , rand .Uint32 ()))
276
303
if _ , err := os .Stat (newPath ); err != nil {
277
304
if os .IsNotExist (err ) {
278
305
return newPath , nil
@@ -354,16 +381,19 @@ func archiveDecompress(ctx context.Context, srcObjPath, dstDirPath string, args
354
381
}
355
382
taskCreator , _ := ctx .Value (conf .UserKey ).(* model.User )
356
383
tsk := & ArchiveDownloadTask {
357
- TaskExtension : task.TaskExtension {
358
- Creator : taskCreator ,
384
+ TaskData : TaskData {
385
+ TaskExtension : task.TaskExtension {
386
+ Creator : taskCreator ,
387
+ ApiUrl : common .GetApiUrl (ctx ),
388
+ },
389
+ SrcStorage : srcStorage ,
390
+ DstStorage : dstStorage ,
391
+ SrcActualPath : srcObjActualPath ,
392
+ DstActualPath : dstDirActualPath ,
393
+ SrcStorageMp : srcStorage .GetStorage ().MountPath ,
394
+ DstStorageMp : dstStorage .GetStorage ().MountPath ,
359
395
},
360
396
ArchiveDecompressArgs : args ,
361
- srcStorage : srcStorage ,
362
- dstStorage : dstStorage ,
363
- SrcObjPath : srcObjActualPath ,
364
- DstDirPath : dstDirActualPath ,
365
- SrcStorageMp : srcStorage .GetStorage ().MountPath ,
366
- DstStorageMp : dstStorage .GetStorage ().MountPath ,
367
397
}
368
398
if ctx .Value (conf .NoTaskKey ) != nil {
369
399
uploadTask , err := tsk .RunWithoutPushUploadTask ()
0 commit comments