Skip to content

Commit

Permalink
sanitize filename to prevent 0kb files (fixes #3)
Browse files Browse the repository at this point in the history
  • Loading branch information
laureanray committed Jul 17, 2022
1 parent 378e503 commit 4fa2621
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
16 changes: 12 additions & 4 deletions pkg/api/libgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package api
import (
"fmt"
"github.com/PuerkitoBio/goquery"
"github.com/kennygrant/sanitize"
"github.com/schollz/progressbar/v3"
"io"
"log"
Expand Down Expand Up @@ -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)

Expand All @@ -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())
}
}

0 comments on commit 4fa2621

Please sign in to comment.