diff --git a/internal/bootstrap/data/setting.go b/internal/bootstrap/data/setting.go index ccf147d7..b6ce3ef5 100644 --- a/internal/bootstrap/data/setting.go +++ b/internal/bootstrap/data/setting.go @@ -158,6 +158,7 @@ func InitialSettings() []model.SettingItem { // qbittorrent settings {Key: conf.QbittorrentUrl, Value: "http://admin:adminadmin@localhost:8080/", Type: conf.TypeString, Group: model.SINGLE, Flag: model.PRIVATE}, + {Key: conf.QbittorrentSeedtime, Value: "0", Type: conf.TypeNumber, Group: model.SINGLE, Flag: model.PRIVATE}, } if flags.Dev { initialSettingItems = append(initialSettingItems, []model.SettingItem{ diff --git a/internal/conf/const.go b/internal/conf/const.go index 53fef61b..24fc41de 100644 --- a/internal/conf/const.go +++ b/internal/conf/const.go @@ -61,7 +61,8 @@ const ( SSOLoginplatform = "sso_login_platform" // qbittorrent - QbittorrentUrl = "qbittorrent_url" + QbittorrentUrl = "qbittorrent_url" + QbittorrentSeedtime = "qbittorrent_seedtime" ) const ( diff --git a/internal/qbittorrent/add.go b/internal/qbittorrent/add.go index f6a9f9f1..f552a9ec 100644 --- a/internal/qbittorrent/add.go +++ b/internal/qbittorrent/add.go @@ -8,6 +8,7 @@ import ( "github.com/alist-org/alist/v3/internal/conf" "github.com/alist-org/alist/v3/internal/errs" "github.com/alist-org/alist/v3/internal/op" + "github.com/alist-org/alist/v3/internal/setting" "github.com/alist-org/alist/v3/pkg/task" "github.com/google/uuid" "github.com/pkg/errors" @@ -50,6 +51,7 @@ func AddURL(ctx context.Context, url string, dstDirPath string) error { tsk: tsk, tempDir: tempDir, dstDirPath: dstDirPath, + seedtime: setting.GetInt(conf.QbittorrentSeedtime, 0), } return m.Loop() }, diff --git a/internal/qbittorrent/monitor.go b/internal/qbittorrent/monitor.go index 972a773c..f1b01efa 100644 --- a/internal/qbittorrent/monitor.go +++ b/internal/qbittorrent/monitor.go @@ -2,23 +2,26 @@ package qbittorrent import ( "fmt" + "io" + "os" + "path/filepath" + "sync" + "sync/atomic" + "time" + "github.com/alist-org/alist/v3/internal/model" "github.com/alist-org/alist/v3/internal/op" "github.com/alist-org/alist/v3/pkg/task" "github.com/alist-org/alist/v3/pkg/utils" "github.com/pkg/errors" log "github.com/sirupsen/logrus" - "os" - "path/filepath" - "sync" - "sync/atomic" - "time" ) type Monitor struct { tsk *task.Task[string] tempDir string dstDirPath string + seedtime int finish chan struct{} } @@ -114,17 +117,27 @@ func (m *Monitor) complete() error { log.Debugf("files len: %d", len(files)) // delete qbittorrent task but do not delete the files before transferring to avoid qbittorrent // accessing downloaded files and throw `cannot access the file because it is being used by another process` error - err = qbclient.Delete(m.tsk.ID, false) - if err != nil { - return err - } + // err = qbclient.Delete(m.tsk.ID, false) + // if err != nil { + // return err + // } // upload files var wg sync.WaitGroup wg.Add(len(files)) go func() { wg.Wait() - err := os.RemoveAll(m.tempDir) m.finish <- struct{}{} + if m.seedtime < 0 { + log.Debugf("do not delete qb task %s", m.tsk.ID) + return + } + log.Debugf("delete qb task %s after %d minutes", m.tsk.ID, m.seedtime) + <-time.After(time.Duration(m.seedtime) * time.Minute) + err := qbclient.Delete(m.tsk.ID, true) + if err != nil { + log.Errorln(err.Error()) + } + err = os.RemoveAll(m.tempDir) if err != nil { log.Errorf("failed to remove qbittorrent temp dir: %+v", err.Error()) } @@ -151,7 +164,7 @@ func (m *Monitor) complete() error { Modified: time.Now(), IsFolder: false, }, - ReadCloser: f, + ReadCloser: struct{ io.ReadSeekCloser }{f}, Mimetype: mimetype, } return op.Put(tsk.Ctx, storage, dstDir, stream, tsk.SetProgress) diff --git a/server/handles/qbittorrent.go b/server/handles/qbittorrent.go index aa55a70f..e99007dc 100644 --- a/server/handles/qbittorrent.go +++ b/server/handles/qbittorrent.go @@ -10,7 +10,8 @@ import ( ) type SetQbittorrentReq struct { - Url string `json:"url" form:"url"` + Url string `json:"url" form:"url"` + Seedtime string `json:"seedtime" form:"seedtime"` } func SetQbittorrent(c *gin.Context) { @@ -19,14 +20,11 @@ func SetQbittorrent(c *gin.Context) { common.ErrorResp(c, err, 400) return } - item := &model.SettingItem{ - Key: conf.QbittorrentUrl, - Value: req.Url, - Type: conf.TypeString, - Group: model.SINGLE, - Flag: model.PRIVATE, + items := []model.SettingItem{ + {Key: conf.QbittorrentUrl, Value: req.Url, Type: conf.TypeString, Group: model.SINGLE, Flag: model.PRIVATE}, + {Key: conf.QbittorrentSeedtime, Value: req.Seedtime, Type: conf.TypeNumber, Group: model.SINGLE, Flag: model.PRIVATE}, } - if err := op.SaveSettingItem(item); err != nil { + if err := op.SaveSettingItems(items); err != nil { common.ErrorResp(c, err, 500) return }