diff --git a/domain.go b/domain.go index 2d3cc93..7b83a5f 100644 --- a/domain.go +++ b/domain.go @@ -273,6 +273,7 @@ type TorrentAddOptions struct { LimitRatio float64 LimitSeedTime int64 Rename string + FirstLastPiecePrio bool } func (o *TorrentAddOptions) Prepare() map[string]string { @@ -329,6 +330,8 @@ func (o *TorrentAddOptions) Prepare() map[string]string { options["rename"] = o.Rename } + options["firstLastPiecePrio"] = strconv.FormatBool(o.FirstLastPiecePrio) + return options } diff --git a/domain_test.go b/domain_test.go index 7782f1c..8c03a57 100644 --- a/domain_test.go +++ b/domain_test.go @@ -20,6 +20,7 @@ func TestTorrentAddOptions_Prepare(t *testing.T) { LimitRatio float64 LimitSeedTime int64 Rename string + FirstLastPiecePrio bool } tests := []struct { name string @@ -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", }, }, { @@ -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", }, }, { @@ -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", }, }, { @@ -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", }, }, { @@ -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", }, }, } @@ -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() diff --git a/methods.go b/methods.go index 832ae94..028cff9 100644 --- a/methods.go +++ b/methods.go @@ -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()) }