mirror of https://github.com/Xhofe/alist
feat: allow keep files in offline download (close #4678)
parent
d455a232ef
commit
b6134dc515
|
@ -16,6 +16,7 @@ const (
|
|||
DeleteOnUploadSucceed DeletePolicy = "delete_on_upload_succeed"
|
||||
DeleteOnUploadFailed DeletePolicy = "delete_on_upload_failed"
|
||||
DeleteNever DeletePolicy = "delete_never"
|
||||
DeleteAlways DeletePolicy = "delete_always"
|
||||
)
|
||||
|
||||
type AddURLArgs struct {
|
||||
|
|
|
@ -125,10 +125,11 @@ func (t *DownloadTask) Complete() error {
|
|||
for i, _ := range files {
|
||||
file := files[i]
|
||||
TransferTaskManager.Add(&TransferTask{
|
||||
file: file,
|
||||
dstDirPath: t.DstDirPath,
|
||||
wg: &wg,
|
||||
tempDir: t.TempDir,
|
||||
file: file,
|
||||
dstDirPath: t.DstDirPath,
|
||||
wg: &wg,
|
||||
tempDir: t.TempDir,
|
||||
deletePolicy: t.DeletePolicy,
|
||||
})
|
||||
}
|
||||
return nil
|
||||
|
|
|
@ -9,16 +9,18 @@ import (
|
|||
"github.com/pkg/errors"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/xhofe/tache"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type TransferTask struct {
|
||||
tache.Base
|
||||
file File
|
||||
dstDirPath string
|
||||
wg *sync.WaitGroup
|
||||
tempDir string
|
||||
file File
|
||||
dstDirPath string
|
||||
wg *sync.WaitGroup
|
||||
tempDir string
|
||||
deletePolicy DeletePolicy
|
||||
}
|
||||
|
||||
func (t *TransferTask) Run() error {
|
||||
|
@ -61,6 +63,24 @@ func (t *TransferTask) GetStatus() string {
|
|||
return "transferring"
|
||||
}
|
||||
|
||||
func (t *TransferTask) OnSucceeded() {
|
||||
if t.deletePolicy == DeleteOnUploadSucceed || t.deletePolicy == DeleteAlways {
|
||||
err := os.Remove(t.file.Path)
|
||||
if err != nil {
|
||||
log.Errorf("failed to delete file %s, error: %s", t.file.Path, err.Error())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (t *TransferTask) OnFailed() {
|
||||
if t.deletePolicy == DeleteOnUploadFailed || t.deletePolicy == DeleteAlways {
|
||||
err := os.Remove(t.file.Path)
|
||||
if err != nil {
|
||||
log.Errorf("failed to delete file %s, error: %s", t.file.Path, err.Error())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var (
|
||||
TransferTaskManager *tache.Manager[*TransferTask]
|
||||
)
|
||||
|
|
Loading…
Reference in New Issue