Skip to content

Commit 9489bf1

Browse files
committed
fix for incorrect ordering after delete
1 parent c7dcf0f commit 9489bf1

File tree

6 files changed

+53
-31
lines changed

6 files changed

+53
-31
lines changed

internal/actions/actions.go

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -116,30 +116,37 @@ func GetTorrentStatus(g *gocui.Gui, v *gocui.View) error {
116116
return nil
117117
}
118118

119-
func DownloadFile(torrent models.TorrentFileDetailed) bool {
119+
func DownloadFile(torrent models.TorrentFileDetailed) error {
120120
path := filepath.Join(config.DownloadPath(), torrent.Filename)
121121

122+
if _, err := os.Stat(path); err == nil {
123+
logs.LogEvent(fmt.Errorf("file already exists, skipping: %s", path))
124+
return err
125+
}
126+
// TODO
127+
// this request does not require authorization
128+
// but still should use a shared client
122129
resp, err := http.Get(torrent.Download)
123130
if err != nil {
124131
logs.LogEvent(fmt.Errorf("failed to GET %s: %w", torrent.Download, err))
125-
return false
132+
return err
126133
}
127134
defer resp.Body.Close()
128135

129136
out, err := os.Create(path)
130137
if err != nil {
131138
logs.LogEvent(fmt.Errorf("failed to create file %s: %w", path, err))
132-
return false
139+
return err
133140
}
134141
defer out.Close()
135142

136143
if _, err := io.Copy(out, resp.Body); err != nil {
137144
logs.LogEvent(fmt.Errorf("failed to write to file %s: %w", path, err))
138-
return false
145+
return err
139146
}
140147

141148
logs.LogEvent(fmt.Errorf("downloaded: %s", path))
142-
return true
149+
return nil
143150
}
144151

145152
func GetTorrentContents(g *gocui.Gui, v *gocui.View) map[string]models.TorrentFileDetailed {
@@ -159,7 +166,7 @@ func GetTorrentContents(g *gocui.Gui, v *gocui.View) map[string]models.TorrentFi
159166

160167
files := make(map[string]models.TorrentFileDetailed)
161168
var errors []string
162-
169+
success := false
163170
for _, link := range torrent.Links {
164171
file, err := api.UnrestrictLink(link)
165172
if err != nil {
@@ -168,6 +175,12 @@ func GetTorrentContents(g *gocui.Gui, v *gocui.View) map[string]models.TorrentFi
168175
continue
169176
}
170177
files[file.Filename] = file
178+
success = true
179+
}
180+
181+
if !success {
182+
views.UpdateUILog(g, "All unrestrict attempts failed", nil)
183+
return nil
171184
}
172185

173186
data.FilesMap = files
@@ -199,6 +212,12 @@ func GetUserTorrents() map[int]models.Torrent {
199212
return nil
200213
}
201214

215+
// TODO
216+
// necessary for now to have correct item ordering
217+
// after deletes
218+
// needs a better solution than emptying the map
219+
data.UserDownloads = make(map[int]models.Torrent)
220+
202221
// essentially a map of which line number refers to which torrent
203222
// this is necessary because gocui results in changed filenames
204223
// depending on the width of the viewport

internal/api/client.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ func DoRequest(req *http.Request) ([]byte, error) {
4444
return nil, fmt.Errorf("HTTP %d: %s", resp.StatusCode, body)
4545
}
4646

47+
// Check for Real-Debrid-style error in JSON
4748
var rdResp struct {
4849
Error string `json:"error"`
4950
ErrorCode int `json:"error_code"`

internal/handlers/download.go

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,27 @@ import (
1414

1515
const maxConcurrentDownloads = 5
1616

17-
// this should also check for download slot probably
17+
// TODO
18+
// this should also check for download slot
1819
func handleStartDownload(g *gocui.Gui, torrentFile models.TorrentFileDetailed) {
19-
log := func(msg string, success bool, err error) {
20+
startMsg := fmt.Sprintf("Starting download: %s → %s", torrentFile.Filename, config.DownloadPath())
21+
endMsg := fmt.Sprintf("Downloaded: %s", torrentFile.Filename)
22+
23+
g.Update(func(g *gocui.Gui) error {
24+
views.UpdateUILog(g, startMsg, nil)
25+
return nil
26+
})
27+
28+
if err := actions.DownloadFile(torrentFile); err == nil {
2029
g.Update(func(g *gocui.Gui) error {
21-
views.UpdateUILog(g, msg, err)
30+
views.UpdateUILog(g, endMsg, nil)
2231
return nil
2332
})
24-
}
25-
26-
log(fmt.Sprintf("Downloading %s to %s", torrentFile.Filename, config.DownloadPath()), true, nil)
27-
28-
if actions.DownloadFile(torrentFile) {
29-
log(fmt.Sprintf("Downloaded %s to %s", torrentFile.Filename, config.DownloadPath()), true, nil)
3033
} else {
31-
log(fmt.Sprintf("Failed to download %s", torrentFile.Filename), false, fmt.Errorf("download failed"))
34+
g.Update(func(g *gocui.Gui) error {
35+
views.UpdateUILog(g, fmt.Sprintf("Failed to download %s", torrentFile.Filename), err)
36+
return nil
37+
})
3238
}
3339
}
3440

internal/handlers/torrent.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package handlers
33
import (
44
"fmt"
55
"lazydebrid/internal/actions"
6+
"lazydebrid/internal/data"
67
"lazydebrid/internal/models"
78
"lazydebrid/internal/views"
89
"strings"
@@ -12,34 +13,37 @@ import (
1213

1314
func HandleDeleteTorrent(g *gocui.Gui, v *gocui.View) error {
1415

15-
// check for view to handle getting torrent id differently
1616
currentViewName := g.CurrentView().Name()
1717
var torrentID string
1818
var err error
1919
var cy int
2020
var torrent models.Torrent
21+
22+
// check for view to handle getting torrent id differently
2123
switch currentViewName {
2224
case views.ViewActiveTorrents:
2325
torrentID, err = views.GetSelectedActiveDownload(v)
2426
if err != nil || strings.TrimSpace(torrentID) == "" {
25-
return fmt.Errorf("no torrent selected")
27+
views.UpdateUILog(g, "no torrent selected", nil)
28+
// return nil
2629
}
2730
case views.ViewTorrents:
2831
torrent, cy, err = views.GetSelectedTorrent(v)
2932

3033
if err != nil || strings.TrimSpace(torrent.ID) == "" {
31-
return fmt.Errorf("no torrent selected")
34+
views.UpdateUILog(g, "no torrent selected", nil)
35+
// return fmt.Errorf("no torrent selected")
3236
}
3337
torrentID = torrent.ID
34-
3538
}
3639

3740
err = actions.DeleteTorrent(torrentID, cy, currentViewName)
3841
if err != nil {
3942
views.UpdateUILog(g, "Failed to delete torrent:", err)
40-
return err
43+
// return err
4144
}
4245

46+
data.UserDownloads = actions.GetUserTorrents()
4347
g.Update(func(g *gocui.Gui) error {
4448

4549
views.UpdateUILog(g, fmt.Sprintf("Deleted torrent: %s", torrentID), nil)

internal/views/layout.go

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,6 @@ func Layout(g *gocui.Gui) error {
5757
torrentsView.Highlight = true
5858
torrentsView.Wrap = false
5959
torrentsView.SelFgColor = gocui.ColorGreen
60-
61-
// Populate later in main.go to avoid import cycle
62-
//
63-
//for _, item := range actions.UserDownloads {
64-
// _, err := fmt.Fprintln(torrentsView, item.Filename)
65-
// if err != nil {
66-
// return err
67-
// }
68-
//}
6960
err = torrentsView.SetCursor(0, 0)
7061
if err != nil {
7162
return err
@@ -84,7 +75,7 @@ func Layout(g *gocui.Gui) error {
8475
if activeTorrentsView, err := g.SetView(ViewActiveTorrents, splitX+1, activeTop, maxX-1, activeBottom); err != nil && err != gocui.ErrUnknownView {
8576
return err
8677
} else if err == nil {
87-
activeTorrentsView.Title = "Active Downloads"
78+
activeTorrentsView.Title = "Active Debrid Downloads"
8879
activeTorrentsView.Highlight = true
8980
activeTorrentsView.Wrap = false
9081
activeTorrentsView.SelFgColor = gocui.ColorGreen

internal/views/viewsUtils.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ func GetSelectedTorrent(v *gocui.View) (torrent models.Torrent, cursorPosition i
111111
return data.UserDownloads[cy], cy, nil
112112
}
113113

114+
// TODO
114115
// this wont work if the viewport is too small to show the entire id
115116
func GetSelectedActiveDownload(v *gocui.View) (string, error) {
116117
_, cy := v.Cursor()

0 commit comments

Comments
 (0)