feat: allow keep files in offline download (close #4678)

pull/5581/head
Andy Hsu 2023-11-24 15:02:36 +08:00
parent d455a232ef
commit b6134dc515
3 changed files with 30 additions and 8 deletions

View File

@ -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 {

View File

@ -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

View File

@ -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]
)