Skip to content

Commit

Permalink
fix(qbittorrent): fix multiple bugs for qbittorrent download (close A…
Browse files Browse the repository at this point in the history
…listGo#3413 in AlistGo#3427)

* fix(qbittorrent): wait for qbittorrent to parse torrent and create task

AlistGo#3413

* fix(qbittorrent): check task state correctly

* fix(qbittorrent): fix path sent to `op.Put()`
  • Loading branch information
kdxcxs authored Feb 15, 2023
1 parent 6659f6d commit d92c10d
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 24 deletions.
15 changes: 14 additions & 1 deletion internal/qbittorrent/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,19 @@ type TorrentInfo struct {
Upspeed int `json:"upspeed"` // 上传速度(字节/秒)
}

type InfoNotFoundError struct {
Id string
Err error
}

func (i InfoNotFoundError) Error() string {
return "there should be exactly one task with tag \"alist-" + i.Id + "\""
}

func NewInfoNotFoundError(id string) InfoNotFoundError {
return InfoNotFoundError{Id: id}
}

func (c *client) GetInfo(id string) (TorrentInfo, error) {
var infos []TorrentInfo

Expand All @@ -266,7 +279,7 @@ func (c *client) GetInfo(id string) (TorrentInfo, error) {
return TorrentInfo{}, err
}
if len(infos) != 1 {
return TorrentInfo{}, errors.New("there should be exactly one task with tag \"alist-" + id + "\"")
return TorrentInfo{}, NewInfoNotFoundError(id)
}
return infos[0], nil
}
Expand Down
52 changes: 29 additions & 23 deletions internal/qbittorrent/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"os"
"path"
"path/filepath"
"sync"
"sync/atomic"
Expand All @@ -29,6 +28,30 @@ func (m *Monitor) Loop() error {
completed bool
)
m.finish = make(chan struct{})

// wait for qbittorrent to parse torrent and create task
m.tsk.SetStatus("waiting for qbittorrent to parse torrent and create task")
waitCount := 0
for {
_, err := qbclient.GetInfo(m.tsk.ID)
if err == nil {
break
}
switch err.(type) {
case InfoNotFoundError:
break
default:
return err
}

waitCount += 1
if waitCount >= 60 {
return errors.New("torrent parse timeout")
}
timer := time.NewTimer(time.Second)
<-timer.C
}

outer:
for {
select {
Expand Down Expand Up @@ -61,29 +84,13 @@ func (m *Monitor) update() (bool, error) {
progress := float64(info.Completed) / float64(info.Size) * 100
m.tsk.SetProgress(int(progress))
switch info.State {
case UPLOADING:
case PAUSEDUP:
case QUEUEDUP:
case STALLEDUP:
case FORCEDUP:
case CHECKINGUP:
case UPLOADING, PAUSEDUP, QUEUEDUP, STALLEDUP, FORCEDUP, CHECKINGUP:
err = m.complete()
return true, errors.WithMessage(err, "failed to transfer file")
case ALLOCATING:
case DOWNLOADING:
case METADL:
case PAUSEDDL:
case QUEUEDDL:
case STALLEDDL:
case CHECKINGDL:
case FORCEDDL:
case CHECKINGRESUMEDATA:
case MOVING:
case UNKNOWN: // or maybe should return an error for UNKNOWN?
case ALLOCATING, DOWNLOADING, METADL, PAUSEDDL, QUEUEDDL, STALLEDDL, CHECKINGDL, FORCEDDL, CHECKINGRESUMEDATA, MOVING:
m.tsk.SetStatus("qbittorrent downloading")
return false, nil
case ERROR:
case MISSINGFILES:
case ERROR, MISSINGFILES, UNKNOWN:
return true, errors.Errorf("failed to download %s, error: %s", m.tsk.ID, info.State)
}
return true, errors.New("unknown error occurred downloading qbittorrent") // should never happen
Expand Down Expand Up @@ -130,16 +137,15 @@ func (m *Monitor) complete() error {
}
stream := &model.FileStream{
Obj: &model.Object{
Name: path.Base(filePath),
Name: file.Name,
Size: size,
Modified: time.Now(),
IsFolder: false,
},
ReadCloser: f,
Mimetype: mimetype,
}
newDistDir := filepath.Join(dstDirActualPath, file.Name)
return op.Put(tsk.Ctx, storage, newDistDir, stream, tsk.SetProgress)
return op.Put(tsk.Ctx, storage, dstDirActualPath, stream, tsk.SetProgress)
},
}))
}
Expand Down

0 comments on commit d92c10d

Please sign in to comment.