@@ -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
145152func 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
0 commit comments