Skip to content

Commit

Permalink
fixes #47
Browse files Browse the repository at this point in the history
respect force state of torrents (automanaged 0)
  • Loading branch information
rumanzo committed Mar 20, 2024
1 parent e41bda5 commit db47054
Show file tree
Hide file tree
Showing 2 changed files with 194 additions and 29 deletions.
37 changes: 20 additions & 17 deletions internal/transfer/transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,28 +85,31 @@ func (transfer *TransferStructure) HandleCaption() {
// HandleState transfer torrents state.
// if torrent has several files, and it doesn't complete downloaded (priority), it will be stopped
func (transfer *TransferStructure) HandleState() {
if transfer.ResumeItem.Started == 0 {
transfer.Fastresume.Paused = 1
transfer.Fastresume.AutoManaged = 0
} else {
if !transfer.TorrentFile.IsSingle() {
var parted bool
for _, prio := range transfer.Fastresume.FilePriority {
if prio == 0 {
parted = true
break
}
}
if parted {
transfer.Fastresume.Paused = 1
transfer.Fastresume.AutoManaged = 0
return
var parted bool = false
if !transfer.TorrentFile.IsSingle() {
for _, prio := range transfer.Fastresume.FilePriority {
if prio == 0 {
parted = true
break
}
}
}

if transfer.ResumeItem.Started == 0 || parted {
transfer.Fastresume.Paused = 1
} else {
transfer.Fastresume.Paused = 0
transfer.Fastresume.AutoManaged = 1
}

if parted {
transfer.Fastresume.AutoManaged = 0 // incomplete torrents always should be stopped
} else {
if transfer.ResumeItem.Started == 0 || transfer.ResumeItem.Started == 2 { // just stoped or started completed torrents
transfer.Fastresume.AutoManaged = 1
} else { // should be 1 - forced
transfer.Fastresume.AutoManaged = 0
}
}
}

func (transfer *TransferStructure) HandleTotalDownloaded() {
Expand Down
186 changes: 174 additions & 12 deletions internal/transfer/transfer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2126,11 +2126,11 @@ func TestTransferStructure_HandleState(t *testing.T) {
},
},
expected: &TransferStructure{
Fastresume: &qBittorrentStructures.QBittorrentFastresume{Paused: 1, AutoManaged: 0},
Fastresume: &qBittorrentStructures.QBittorrentFastresume{Paused: 1, AutoManaged: 1},
},
},
{
name: "003 started resume",
name: "003 forced started resume",
newTransferStructure: &TransferStructure{
Fastresume: &qBittorrentStructures.QBittorrentFastresume{},
ResumeItem: &utorrentStructs.ResumeItem{Started: 1},
Expand All @@ -2139,11 +2139,11 @@ func TestTransferStructure_HandleState(t *testing.T) {
},
},
expected: &TransferStructure{
Fastresume: &qBittorrentStructures.QBittorrentFastresume{Paused: 0, AutoManaged: 1},
Fastresume: &qBittorrentStructures.QBittorrentFastresume{Paused: 0, AutoManaged: 0},
},
},
{
name: "004 started resume with full downloaded files",
name: "004 force started resume with full downloaded files",
newTransferStructure: &TransferStructure{
Fastresume: &qBittorrentStructures.QBittorrentFastresume{},
ResumeItem: &utorrentStructs.ResumeItem{
Expand All @@ -2164,11 +2164,11 @@ func TestTransferStructure_HandleState(t *testing.T) {
},
},
expected: &TransferStructure{
Fastresume: &qBittorrentStructures.QBittorrentFastresume{Paused: 0, AutoManaged: 1},
Fastresume: &qBittorrentStructures.QBittorrentFastresume{Paused: 0, AutoManaged: 0},
},
},
{
name: "005 started resume with parted downloaded files",
name: "005 force started resume with parted downloaded files",
newTransferStructure: &TransferStructure{
Fastresume: &qBittorrentStructures.QBittorrentFastresume{
FilePriority: []int64{1, 0, 1, 1, 1, 6, 6},
Expand Down Expand Up @@ -2199,7 +2199,7 @@ func TestTransferStructure_HandleState(t *testing.T) {
},
},
{
name: "006 started resume with parted downloaded files",
name: "006 force started resume with prioritized parted downloaded files",
newTransferStructure: &TransferStructure{
Fastresume: &qBittorrentStructures.QBittorrentFastresume{
FilePriority: []int64{1, 0, 1, 1, 1, 6, 6},
Expand Down Expand Up @@ -2230,7 +2230,38 @@ func TestTransferStructure_HandleState(t *testing.T) {
},
},
{
name: "007 started resume with full downloaded files",
name: "007 force started resume with prioritized complete downloaded files",
newTransferStructure: &TransferStructure{
Fastresume: &qBittorrentStructures.QBittorrentFastresume{
FilePriority: []int64{1, 1, 1, 1, 1, 6, 6},
},
ResumeItem: &utorrentStructs.ResumeItem{
Started: 1,
},
TorrentFile: &torrentStructures.Torrent{
Info: &torrentStructures.TorrentInfo{
Files: []*torrentStructures.TorrentFile{
&torrentStructures.TorrentFile{Path: []string{`/`}},
&torrentStructures.TorrentFile{Path: []string{`/`}},
&torrentStructures.TorrentFile{Path: []string{`/`}},
&torrentStructures.TorrentFile{Path: []string{`/`}},
&torrentStructures.TorrentFile{Path: []string{`/`}},
&torrentStructures.TorrentFile{Path: []string{`/`}},
&torrentStructures.TorrentFile{Path: []string{`/`}},
},
},
},
},
expected: &TransferStructure{
Fastresume: &qBittorrentStructures.QBittorrentFastresume{
Paused: 0,
AutoManaged: 0,
FilePriority: []int64{1, 1, 1, 1, 1, 6, 6},
},
},
},
{
name: "008 force started resume with parted prioritized downloaded files",
newTransferStructure: &TransferStructure{
Fastresume: &qBittorrentStructures.QBittorrentFastresume{
FilePriority: []int64{1, 0, 1, 1, 1, 6, 6},
Expand Down Expand Up @@ -2261,13 +2292,144 @@ func TestTransferStructure_HandleState(t *testing.T) {
},
},
{
name: "008 started resume with parted downloaded files",
name: "009 started resume",
newTransferStructure: &TransferStructure{
Fastresume: &qBittorrentStructures.QBittorrentFastresume{},
ResumeItem: &utorrentStructs.ResumeItem{Started: 2},
TorrentFile: &torrentStructures.Torrent{
Info: &torrentStructures.TorrentInfo{},
},
},
expected: &TransferStructure{
Fastresume: &qBittorrentStructures.QBittorrentFastresume{Paused: 0, AutoManaged: 1},
},
},
{
name: "010 started resume with fully downloaded files",
newTransferStructure: &TransferStructure{
Fastresume: &qBittorrentStructures.QBittorrentFastresume{},
ResumeItem: &utorrentStructs.ResumeItem{
Started: 2,
},
TorrentFile: &torrentStructures.Torrent{
Info: &torrentStructures.TorrentInfo{
Files: []*torrentStructures.TorrentFile{
&torrentStructures.TorrentFile{Path: []string{`/`}},
&torrentStructures.TorrentFile{Path: []string{`/`}},
&torrentStructures.TorrentFile{Path: []string{`/`}},
&torrentStructures.TorrentFile{Path: []string{`/`}},
&torrentStructures.TorrentFile{Path: []string{`/`}},
&torrentStructures.TorrentFile{Path: []string{`/`}},
&torrentStructures.TorrentFile{Path: []string{`/`}},
},
},
},
},
expected: &TransferStructure{
Fastresume: &qBittorrentStructures.QBittorrentFastresume{Paused: 0, AutoManaged: 1},
},
},
{
name: "011 started resume with parted downloaded files",
newTransferStructure: &TransferStructure{
Fastresume: &qBittorrentStructures.QBittorrentFastresume{
FilePriority: []int64{1, 0, 1, 1, 1, 6, 6},
},
ResumeItem: &utorrentStructs.ResumeItem{
Started: 1,
Started: 2,
},
TorrentFile: &torrentStructures.Torrent{
Info: &torrentStructures.TorrentInfo{
Files: []*torrentStructures.TorrentFile{
&torrentStructures.TorrentFile{Path: []string{`/`}},
&torrentStructures.TorrentFile{Path: []string{`/`}},
&torrentStructures.TorrentFile{Path: []string{`/`}},
&torrentStructures.TorrentFile{Path: []string{`/`}},
&torrentStructures.TorrentFile{Path: []string{`/`}},
&torrentStructures.TorrentFile{Path: []string{`/`}},
&torrentStructures.TorrentFile{Path: []string{`/`}},
},
},
},
},
expected: &TransferStructure{
Fastresume: &qBittorrentStructures.QBittorrentFastresume{
Paused: 1,
AutoManaged: 0,
FilePriority: []int64{1, 0, 1, 1, 1, 6, 6},
},
},
},
{
name: "012 started resume with prioritized parted downloaded files",
newTransferStructure: &TransferStructure{
Fastresume: &qBittorrentStructures.QBittorrentFastresume{
FilePriority: []int64{1, 0, 1, 1, 1, 6, 6},
},
ResumeItem: &utorrentStructs.ResumeItem{
Started: 2,
},
TorrentFile: &torrentStructures.Torrent{
Info: &torrentStructures.TorrentInfo{
Files: []*torrentStructures.TorrentFile{
&torrentStructures.TorrentFile{Path: []string{`/`}},
&torrentStructures.TorrentFile{Path: []string{`/`}},
&torrentStructures.TorrentFile{Path: []string{`/`}},
&torrentStructures.TorrentFile{Path: []string{`/`}},
&torrentStructures.TorrentFile{Path: []string{`/`}},
&torrentStructures.TorrentFile{Path: []string{`/`}},
&torrentStructures.TorrentFile{Path: []string{`/`}},
},
},
},
},
expected: &TransferStructure{
Fastresume: &qBittorrentStructures.QBittorrentFastresume{
Paused: 1,
AutoManaged: 0,
FilePriority: []int64{1, 0, 1, 1, 1, 6, 6},
},
},
},
{
name: "013 started resume with prioritized complete downloaded files",
newTransferStructure: &TransferStructure{
Fastresume: &qBittorrentStructures.QBittorrentFastresume{
FilePriority: []int64{1, 1, 1, 1, 1, 6, 6},
},
ResumeItem: &utorrentStructs.ResumeItem{
Started: 2,
},
TorrentFile: &torrentStructures.Torrent{
Info: &torrentStructures.TorrentInfo{
Files: []*torrentStructures.TorrentFile{
&torrentStructures.TorrentFile{Path: []string{`/`}},
&torrentStructures.TorrentFile{Path: []string{`/`}},
&torrentStructures.TorrentFile{Path: []string{`/`}},
&torrentStructures.TorrentFile{Path: []string{`/`}},
&torrentStructures.TorrentFile{Path: []string{`/`}},
&torrentStructures.TorrentFile{Path: []string{`/`}},
&torrentStructures.TorrentFile{Path: []string{`/`}},
},
},
},
},
expected: &TransferStructure{
Fastresume: &qBittorrentStructures.QBittorrentFastresume{
Paused: 0,
AutoManaged: 1,
FilePriority: []int64{1, 1, 1, 1, 1, 6, 6},
},
},
},
{
name: "014 started resume with prioritized parted downloaded files",
newTransferStructure: &TransferStructure{
Fastresume: &qBittorrentStructures.QBittorrentFastresume{
FilePriority: []int64{1, 0, 1, 1, 1, 6, 6},
},
ResumeItem: &utorrentStructs.ResumeItem{
Started: 2,
},
TorrentFile: &torrentStructures.Torrent{
Info: &torrentStructures.TorrentInfo{
Expand All @@ -2292,7 +2454,7 @@ func TestTransferStructure_HandleState(t *testing.T) {
},
},
{
name: "009 started resume without files",
name: "015 started resume without files",
newTransferStructure: &TransferStructure{
Fastresume: &qBittorrentStructures.QBittorrentFastresume{},
ResumeItem: &utorrentStructs.ResumeItem{
Expand All @@ -2303,7 +2465,7 @@ func TestTransferStructure_HandleState(t *testing.T) {
},
},
expected: &TransferStructure{
Fastresume: &qBittorrentStructures.QBittorrentFastresume{Paused: 1, AutoManaged: 0},
Fastresume: &qBittorrentStructures.QBittorrentFastresume{Paused: 1, AutoManaged: 1},
},
},
}
Expand Down

0 comments on commit db47054

Please sign in to comment.