Skip to content

Commit

Permalink
feat(qbittorrent): delete tags when deleting qbittorrent tasks (#3546)
Browse files Browse the repository at this point in the history
* feat & refactor(qbittorrent/client): support `deleteFiles` arg for `Client.Delete()` method

* feat(qbittorrent/client): also delete tags in `Client.Delete()`
  • Loading branch information
kdxcxs authored Feb 21, 2023
1 parent 658cf36 commit d1ab244
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 13 deletions.
22 changes: 18 additions & 4 deletions internal/qbittorrent/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type Client interface {
AddFromLink(link string, savePath string, id string) error
GetInfo(id string) (TorrentInfo, error)
GetFiles(id string) ([]FileInfo, error)
Delete(id string) error
Delete(id string, deleteFiles bool) error
}

type client struct {
Expand Down Expand Up @@ -326,7 +326,7 @@ func (c *client) GetFiles(id string) ([]FileInfo, error) {
return infos, nil
}

func (c *client) Delete(id string) error {
func (c *client) Delete(id string, deleteFiles bool) error {
err := c.checkAuthorization()
if err != nil {
return err
Expand All @@ -338,13 +338,27 @@ func (c *client) Delete(id string) error {
}
v := url.Values{}
v.Set("hashes", info.Hash)
v.Set("deleteFiles", "false")
if deleteFiles {
v.Set("deleteFiles", "true")
} else {
v.Set("deleteFiles", "false")
}
response, err := c.post("/api/v2/torrents/delete", v)
if err != nil {
return err
}
if response.StatusCode != 200 {
return errors.New("failed")
return errors.New("failed to delete qbittorrent task")
}

v = url.Values{}
v.Set("tags", "alist-"+id)
response, err = c.post("/api/v2/torrents/deleteTags", v)
if err != nil {
return err
}
if response.StatusCode != 200 {
return errors.New("failed to delete qbittorrent tag")
}
return nil
}
16 changes: 9 additions & 7 deletions internal/qbittorrent/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,6 @@ func TestAdd(t *testing.T) {
if err != nil {
t.Error(err)
}

// test add
err = c.login()
if err != nil {
t.Error(err)
}
err = c.AddFromLink(
"https://releases.ubuntu.com/22.04/ubuntu-22.04.1-desktop-amd64.iso.torrent",
"D:\\qBittorrentDownload\\alist",
Expand Down Expand Up @@ -145,7 +139,15 @@ func TestDelete(t *testing.T) {
if err != nil {
t.Error(err)
}
err = c.Delete("uuid-2")
err = c.AddFromLink(
"https://releases.ubuntu.com/22.04/ubuntu-22.04.1-desktop-amd64.iso.torrent",
"D:\\qBittorrentDownload\\alist",
"uuid-1",
)
if err != nil {
t.Error(err)
}
err = c.Delete("uuid-1", true)
if err != nil {
t.Error(err)
}
Expand Down
5 changes: 3 additions & 2 deletions internal/qbittorrent/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ outer:
for {
select {
case <-m.tsk.Ctx.Done():
return qbclient.Delete(m.tsk.ID)
// delete qbittorrent task and downloaded files when the task exits with error
return qbclient.Delete(m.tsk.ID, true)
case <-time.After(time.Second * 2):
completed, err = m.update()
if completed {
Expand Down Expand Up @@ -113,7 +114,7 @@ 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)
err = qbclient.Delete(m.tsk.ID, false)
if err != nil {
return err
}
Expand Down

0 comments on commit d1ab244

Please sign in to comment.