diff --git a/rtorrent/rtorrent.go b/rtorrent/rtorrent.go index c55890d..67348b2 100644 --- a/rtorrent/rtorrent.go +++ b/rtorrent/rtorrent.go @@ -82,7 +82,16 @@ func (r *RTorrent) WithHTTPClient(client *http.Client) *RTorrent { return r } -// Add adds a new torrent by URL +// AddStopped adds a new torrent by URL in a stopped state +func (r *RTorrent) AddStopped(url string) error { + _, err := r.xmlrpcClient.Call("load.normal", "", url) + if err != nil { + return errors.Wrap(err, "load.normal XMLRPC call failed") + } + return nil +} + +// Add adds a new torrent by URL and starts the torrent func (r *RTorrent) Add(url string) error { _, err := r.xmlrpcClient.Call("load.start", "", url) if err != nil { @@ -91,7 +100,16 @@ func (r *RTorrent) Add(url string) error { return nil } -// AddTorrent adds a new torrent by the torrent files data +// AddTorrentStopped adds a new torrent by the torrent files data but does not start the torrent +func (r *RTorrent) AddTorrentStopped(data []byte) error { + _, err := r.xmlrpcClient.Call("load.raw", "", data) + if err != nil { + return errors.Wrap(err, "load.raw XMLRPC call failed") + } + return nil +} + +// AddTorrent adds a new torrent by the torrent files data and starts the torrent func (r *RTorrent) AddTorrent(data []byte) error { _, err := r.xmlrpcClient.Call("load.raw_start", "", data) if err != nil { diff --git a/rtorrent/rtorrent_test.go b/rtorrent/rtorrent_test.go index e691d37..bdacbed 100644 --- a/rtorrent/rtorrent_test.go +++ b/rtorrent/rtorrent_test.go @@ -158,6 +158,81 @@ func TestRTorrent(t *testing.T) { //require.NotZero(t, status.Ratio) }) + t.Run("delete torrent", func(t *testing.T) { + err := client.Delete(torrents[0]) + require.NoError(t, err) + + torrents, err := client.GetTorrents(ViewMain) + require.NoError(t, err) + require.Empty(t, torrents) + + t.Run("get torrent", func(t *testing.T) { + // It will take some time to disappear, so retry a few times + tries := 0 + var torrents []Torrent + var err error + for { + <-time.After(time.Second) + torrents, err = client.GetTorrents(ViewMain) + require.NoError(t, err) + if len(torrents) == 0 { + break + } + if tries > 10 { + require.NoError(t, errors.Errorf("torrent did not delete in time")) + } + tries++ + } + require.Empty(t, torrents) + }) + }) + + }) + }) + + t.Run("by url (stopped)", func(t *testing.T) { + err := client.AddStopped("http://releases.ubuntu.com/19.04/ubuntu-19.04-live-server-amd64.iso.torrent") + require.NoError(t, err) + + t.Run("get torrent", func(t *testing.T) { + // It will take some time to appear, so retry a few times + tries := 0 + var torrents []Torrent + var err error + for { + <-time.After(time.Second) + torrents, err = client.GetTorrents(ViewStopped) + require.NoError(t, err) + if len(torrents) > 0 { + break + } + if tries > 10 { + require.NoError(t, errors.Errorf("torrent did not show up in time")) + } + tries++ + } + require.NotEmpty(t, torrents) + require.Len(t, torrents, 1) + require.Equal(t, "B7B0FBAB74A85D4AC170662C645982A862826455", torrents[0].Hash) + require.Equal(t, "ubuntu-19.04-live-server-amd64.iso", torrents[0].Name) + require.Equal(t, "", torrents[0].Label) + require.Equal(t, 784334848, torrents[0].Size) + //no path yet since the torrent is stopped + require.Equal(t, "", torrents[0].Path) + require.False(t, torrents[0].Completed) + + t.Run("get status", func(t *testing.T) { + <-time.After(time.Second) + status, err := client.GetStatus(torrents[0]) + require.NoError(t, err) + t.Logf("Status = %+v", status) + + require.False(t, status.Completed) + require.Zero(t, status.CompletedBytes) + require.Zero(t, status.DownRate) + require.NotZero(t, status.Size) + }) + t.Run("delete torrent", func(t *testing.T) { err := client.Delete(torrents[0]) require.NoError(t, err) @@ -264,6 +339,58 @@ func TestRTorrent(t *testing.T) { }) }) }) + + t.Run("with data (stopped)", func(t *testing.T) { + b, err := ioutil.ReadFile("testdata/ubuntu-20.04.1-live-server-amd64.iso.torrent") + require.NoError(t, err) + require.NotEmpty(t, b) + + err = client.AddTorrentStopped(b) + require.NoError(t, err) + + t.Run("get torrent", func(t *testing.T) { + // It will take some time to appear, so retry a few times + <-time.After(time.Second) + torrents, err := client.GetTorrents(ViewMain) + require.NoError(t, err) + + require.NotEmpty(t, torrents) + require.Len(t, torrents, 1) + require.Equal(t, "36C67464C37A83478CEFF54932B5A9BDDEA636F3", torrents[0].Hash) + require.Equal(t, "ubuntu-20.04.1-live-server-amd64.iso", torrents[0].Name) + require.Equal(t, "", torrents[0].Label) + require.Equal(t, 958398464, torrents[0].Size) + + t.Run("delete torrent", func(t *testing.T) { + err := client.Delete(torrents[0]) + require.NoError(t, err) + + torrents, err := client.GetTorrents(ViewMain) + require.NoError(t, err) + require.Empty(t, torrents) + + t.Run("get torrent", func(t *testing.T) { + // It will take some time to disappear, so retry a few times + tries := 0 + var torrents []Torrent + var err error + for { + <-time.After(time.Second) + torrents, err = client.GetTorrents(ViewMain) + require.NoError(t, err) + if len(torrents) == 0 { + break + } + if tries > 10 { + require.NoError(t, errors.Errorf("torrent did not delete in time")) + } + tries++ + } + require.Empty(t, torrents) + }) + }) + }) + }) }) t.Run("down total post activity", func(t *testing.T) {