diff --git a/go.mod b/go.mod index 310304c..c35491d 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.18 require ( github.com/PuerkitoBio/goquery v1.8.0 + github.com/kennygrant/sanitize v1.2.4 github.com/manifoldco/promptui v0.9.0 github.com/schollz/progressbar/v3 v3.8.6 github.com/spf13/cobra v1.4.0 diff --git a/go.sum b/go.sum index e81e92d..32ccd52 100644 --- a/go.sum +++ b/go.sum @@ -15,6 +15,8 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM= github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= github.com/k0kubun/go-ansi v0.0.0-20180517002512-3bf9e2903213/go.mod h1:vNUNkEQ1e29fT/6vq2aBdFsgNPmy8qMdSay1npru+Sw= +github.com/kennygrant/sanitize v1.2.4 h1:gN25/otpP5vAsO2djbMhF/LQX6R7+O1TB4yv8NzpJ3o= +github.com/kennygrant/sanitize v1.2.4/go.mod h1:LGsjYYtgxbetdg5owWB2mpgUL6e2nfw2eObZ0u0qvak= github.com/manifoldco/promptui v0.9.0 h1:3V4HzJk1TtXW1MTZMP7mdlwbBpIinw3HztaIlYthEiA= github.com/manifoldco/promptui v0.9.0/go.mod h1:ka04sppxSGFAtxX0qhlYQjISsg9mR4GWtQEhdbn6Pgg= github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94= diff --git a/pkg/api/libgen.go b/pkg/api/libgen.go index 0824b41..f76c008 100644 --- a/pkg/api/libgen.go +++ b/pkg/api/libgen.go @@ -3,6 +3,7 @@ package api import ( "fmt" "github.com/PuerkitoBio/goquery" + "github.com/kennygrant/sanitize" "github.com/schollz/progressbar/v3" "io" "log" @@ -248,8 +249,8 @@ func SearchBookByTitle(query string, limit int, libgenSite Site) ([]Book, error) // DownloadSelection Downloads the file to current working directory func DownloadSelection(selectedBook Book, libgenType Site) { - log.Println("Initializing download") link := getDirectDownloadLink(selectedBook.Mirrors[0], libgenType) + log.Println("Initializing download " + link) req, _ := http.NewRequest("GET", link, nil) resp, error := http.DefaultClient.Do(req) @@ -258,14 +259,21 @@ func DownloadSelection(selectedBook Book, libgenType Site) { } defer resp.Body.Close() - f, _ := os.OpenFile(strings.Trim(selectedBook.Title, " ")+"."+selectedBook.Extension, os.O_CREATE|os.O_WRONLY, 0644) + filename := sanitize.Path(strings.Trim(selectedBook.Title, " ") + "." + selectedBook.Extension) + + f, _ := os.OpenFile(filename, os.O_CREATE|os.O_WRONLY, 0666) defer f.Close() bar := progressbar.DefaultBytes( resp.ContentLength, "Downloading", ) - io.Copy(io.MultiWriter(f, bar), resp.Body) - log.Println("File successfully downloaded:", f.Name()) + bytes, err := io.Copy(io.MultiWriter(f, bar), resp.Body) + + if bytes == 0 || err != nil { + log.Println(bytes, err) + } else { + log.Println("File successfully downloaded:", f.Name()) + } }