Skip to content

Commit

Permalink
feat: firstLastPiecePrio (#25)
Browse files Browse the repository at this point in the history
* add(methods): ToggleFirstLastPiecePrio

* add(domain): ToggleFirstLastPiecePrio

* fix(domain): use correct property name

* fix(domain): change from toggleFirstLastPiecePrio to firstLastPiecePrio

* remove from AppPreferences

couldnt find info about it being present
  • Loading branch information
s0up4200 authored Apr 18, 2024
1 parent 3049cd3 commit 44528cc
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 55 deletions.
3 changes: 3 additions & 0 deletions domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ type TorrentAddOptions struct {
LimitRatio float64
LimitSeedTime int64
Rename string
FirstLastPiecePrio bool
}

func (o *TorrentAddOptions) Prepare() map[string]string {
Expand Down Expand Up @@ -329,6 +330,8 @@ func (o *TorrentAddOptions) Prepare() map[string]string {
options["rename"] = o.Rename
}

options["firstLastPiecePrio"] = strconv.FormatBool(o.FirstLastPiecePrio)

return options
}

Expand Down
149 changes: 94 additions & 55 deletions domain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func TestTorrentAddOptions_Prepare(t *testing.T) {
LimitRatio float64
LimitSeedTime int64
Rename string
FirstLastPiecePrio bool
}
tests := []struct {
name string
Expand All @@ -42,16 +43,17 @@ func TestTorrentAddOptions_Prepare(t *testing.T) {
LimitSeedTime: 100,
},
want: map[string]string{
"paused": "false",
"skip_checking": "true",
"autoTMM": "false",
"ratioLimit": "2.00",
"savepath": "/home/test/torrents",
"seedingTimeLimit": "100",
"category": "test",
"tags": "limited,slow",
"upLimit": "102400000",
"dlLimit": "102400000",
"paused": "false",
"skip_checking": "true",
"autoTMM": "false",
"firstLastPiecePrio": "false",
"ratioLimit": "2.00",
"savepath": "/home/test/torrents",
"seedingTimeLimit": "100",
"category": "test",
"tags": "limited,slow",
"upLimit": "102400000",
"dlLimit": "102400000",
},
},
{
Expand All @@ -70,18 +72,19 @@ func TestTorrentAddOptions_Prepare(t *testing.T) {
LimitSeedTime: 100,
},
want: map[string]string{
"paused": "false",
"skip_checking": "true",
"root_folder": "true",
"contentLayout": "Subfolder",
"autoTMM": "false",
"ratioLimit": "2.00",
"savepath": "/home/test/torrents",
"seedingTimeLimit": "100",
"category": "test",
"tags": "limited,slow",
"upLimit": "102400000",
"dlLimit": "102400000",
"paused": "false",
"skip_checking": "true",
"root_folder": "true",
"contentLayout": "Subfolder",
"autoTMM": "false",
"firstLastPiecePrio": "false",
"ratioLimit": "2.00",
"savepath": "/home/test/torrents",
"seedingTimeLimit": "100",
"category": "test",
"tags": "limited,slow",
"upLimit": "102400000",
"dlLimit": "102400000",
},
},
{
Expand All @@ -100,18 +103,19 @@ func TestTorrentAddOptions_Prepare(t *testing.T) {
LimitSeedTime: 100,
},
want: map[string]string{
"paused": "false",
"skip_checking": "true",
"root_folder": "false",
"contentLayout": "NoSubfolder",
"autoTMM": "false",
"ratioLimit": "2.00",
"savepath": "/home/test/torrents",
"seedingTimeLimit": "100",
"category": "test",
"tags": "limited,slow",
"upLimit": "102400000",
"dlLimit": "102400000",
"paused": "false",
"skip_checking": "true",
"root_folder": "false",
"contentLayout": "NoSubfolder",
"autoTMM": "false",
"firstLastPiecePrio": "false",
"ratioLimit": "2.00",
"savepath": "/home/test/torrents",
"seedingTimeLimit": "100",
"category": "test",
"tags": "limited,slow",
"upLimit": "102400000",
"dlLimit": "102400000",
},
},
{
Expand All @@ -130,16 +134,17 @@ func TestTorrentAddOptions_Prepare(t *testing.T) {
LimitSeedTime: 100,
},
want: map[string]string{
"paused": "false",
"skip_checking": "true",
"autoTMM": "false",
"ratioLimit": "2.00",
"savepath": "/home/test/torrents",
"seedingTimeLimit": "100",
"category": "test",
"tags": "limited,slow",
"upLimit": "102400000",
"dlLimit": "102400000",
"paused": "false",
"skip_checking": "true",
"autoTMM": "false",
"firstLastPiecePrio": "false",
"ratioLimit": "2.00",
"savepath": "/home/test/torrents",
"seedingTimeLimit": "100",
"category": "test",
"tags": "limited,slow",
"upLimit": "102400000",
"dlLimit": "102400000",
},
},
{
Expand All @@ -159,17 +164,50 @@ func TestTorrentAddOptions_Prepare(t *testing.T) {
Rename: "test-torrent-rename",
},
want: map[string]string{
"paused": "false",
"skip_checking": "true",
"autoTMM": "false",
"ratioLimit": "2.00",
"savepath": "/home/test/torrents",
"seedingTimeLimit": "100",
"category": "test",
"tags": "limited,slow",
"upLimit": "102400000",
"dlLimit": "102400000",
"rename": "test-torrent-rename",
"paused": "false",
"skip_checking": "true",
"autoTMM": "false",
"firstLastPiecePrio": "false",
"ratioLimit": "2.00",
"savepath": "/home/test/torrents",
"seedingTimeLimit": "100",
"category": "test",
"tags": "limited,slow",
"upLimit": "102400000",
"dlLimit": "102400000",
"rename": "test-torrent-rename",
},
},
{
name: "test_06",
fields: fields{
Paused: false,
SkipHashCheck: true,
ContentLayout: ContentLayoutOriginal,
SavePath: "/home/test/torrents",
AutoTMM: false,
FirstLastPiecePrio: true,
Category: "test",
Tags: "limited,slow",
LimitUploadSpeed: 100000,
LimitDownloadSpeed: 100000,
LimitRatio: 2.0,
LimitSeedTime: 100,
Rename: "test-torrent-rename",
},
want: map[string]string{
"paused": "false",
"skip_checking": "true",
"autoTMM": "false",
"firstLastPiecePrio": "true",
"ratioLimit": "2.00",
"savepath": "/home/test/torrents",
"seedingTimeLimit": "100",
"category": "test",
"tags": "limited,slow",
"upLimit": "102400000",
"dlLimit": "102400000",
"rename": "test-torrent-rename",
},
},
}
Expand All @@ -188,6 +226,7 @@ func TestTorrentAddOptions_Prepare(t *testing.T) {
LimitRatio: tt.fields.LimitRatio,
LimitSeedTime: tt.fields.LimitSeedTime,
Rename: tt.fields.Rename,
FirstLastPiecePrio: tt.fields.FirstLastPiecePrio,
}

got := o.Prepare()
Expand Down
27 changes: 27 additions & 0 deletions methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -1102,6 +1102,33 @@ func (c *Client) IncreasePriorityCtx(ctx context.Context, hashes []string) error
return nil
}

// ToggleFirstLastPiecePrio toggles the priority of the first and last pieces of torrents specified by hashes
func (c *Client) ToggleFirstLastPiecePrio(hashes []string) error {
return c.ToggleFirstLastPiecePrioCtx(context.Background(), hashes)
}

// ToggleFirstLastPiecePrioCtx toggles the priority of the first and last pieces of torrents specified by hashes
func (c *Client) ToggleFirstLastPiecePrioCtx(ctx context.Context, hashes []string) error {
hv := strings.Join(hashes, "|")

opts := map[string]string{
"hashes": hv,
}

resp, err := c.postCtx(ctx, "torrents/toggleFirstLastPiecePrio", opts)
if err != nil {
return errors.Wrap(err, "could not toggle first/last piece priority for torrents: %v", hashes)
}

defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return errors.New("unexpected status while toggling first/last piece priority for torrents: %v, status: %d", hashes, resp.StatusCode)
}

return nil
}

func (c *Client) GetAppVersion() (string, error) {
return c.GetAppVersionCtx(context.Background())
}
Expand Down

0 comments on commit 44528cc

Please sign in to comment.