Skip to content

Commit

Permalink
feat(qbittorrent): add offline download seed time (#3842 close #3588)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rektyfikowany authored Mar 14, 2023
1 parent 4741a75 commit f3db23a
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 20 deletions.
1 change: 1 addition & 0 deletions internal/bootstrap/data/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
3 changes: 2 additions & 1 deletion internal/conf/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ const (
SSOLoginplatform = "sso_login_platform"

// qbittorrent
QbittorrentUrl = "qbittorrent_url"
QbittorrentUrl = "qbittorrent_url"
QbittorrentSeedtime = "qbittorrent_seedtime"
)

const (
Expand Down
2 changes: 2 additions & 0 deletions internal/qbittorrent/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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()
},
Expand Down
35 changes: 24 additions & 11 deletions internal/qbittorrent/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
}

Expand Down Expand Up @@ -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())
}
Expand All @@ -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)
Expand Down
14 changes: 6 additions & 8 deletions server/handles/qbittorrent.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
}
Expand Down

0 comments on commit f3db23a

Please sign in to comment.