@@ -3,6 +3,7 @@ package api
3
3
import (
4
4
"fmt"
5
5
"github.com/PuerkitoBio/goquery"
6
+ "github.com/kennygrant/sanitize"
6
7
"github.com/schollz/progressbar/v3"
7
8
"io"
8
9
"log"
@@ -248,8 +249,8 @@ func SearchBookByTitle(query string, limit int, libgenSite Site) ([]Book, error)
248
249
249
250
// DownloadSelection Downloads the file to current working directory
250
251
func DownloadSelection (selectedBook Book , libgenType Site ) {
251
- log .Println ("Initializing download" )
252
252
link := getDirectDownloadLink (selectedBook .Mirrors [0 ], libgenType )
253
+ log .Println ("Initializing download " + link )
253
254
req , _ := http .NewRequest ("GET" , link , nil )
254
255
resp , error := http .DefaultClient .Do (req )
255
256
@@ -258,14 +259,21 @@ func DownloadSelection(selectedBook Book, libgenType Site) {
258
259
}
259
260
260
261
defer resp .Body .Close ()
261
- f , _ := os .OpenFile (strings .Trim (selectedBook .Title , " " )+ "." + selectedBook .Extension , os .O_CREATE | os .O_WRONLY , 0644 )
262
+ filename := sanitize .Path (strings .Trim (selectedBook .Title , " " ) + "." + selectedBook .Extension )
263
+
264
+ f , _ := os .OpenFile (filename , os .O_CREATE | os .O_WRONLY , 0666 )
262
265
defer f .Close ()
263
266
264
267
bar := progressbar .DefaultBytes (
265
268
resp .ContentLength ,
266
269
"Downloading" ,
267
270
)
268
- io .Copy (io .MultiWriter (f , bar ), resp .Body )
269
271
270
- log .Println ("File successfully downloaded:" , f .Name ())
272
+ bytes , err := io .Copy (io .MultiWriter (f , bar ), resp .Body )
273
+
274
+ if bytes == 0 || err != nil {
275
+ log .Println (bytes , err )
276
+ } else {
277
+ log .Println ("File successfully downloaded:" , f .Name ())
278
+ }
271
279
}
0 commit comments