Skip to content

Commit 4fa2621

Browse files
committed
sanitize filename to prevent 0kb files (fixes #3)
1 parent 378e503 commit 4fa2621

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ go 1.18
44

55
require (
66
github.com/PuerkitoBio/goquery v1.8.0
7+
github.com/kennygrant/sanitize v1.2.4
78
github.com/manifoldco/promptui v0.9.0
89
github.com/schollz/progressbar/v3 v3.8.6
910
github.com/spf13/cobra v1.4.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
1515
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
1616
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
1717
github.com/k0kubun/go-ansi v0.0.0-20180517002512-3bf9e2903213/go.mod h1:vNUNkEQ1e29fT/6vq2aBdFsgNPmy8qMdSay1npru+Sw=
18+
github.com/kennygrant/sanitize v1.2.4 h1:gN25/otpP5vAsO2djbMhF/LQX6R7+O1TB4yv8NzpJ3o=
19+
github.com/kennygrant/sanitize v1.2.4/go.mod h1:LGsjYYtgxbetdg5owWB2mpgUL6e2nfw2eObZ0u0qvak=
1820
github.com/manifoldco/promptui v0.9.0 h1:3V4HzJk1TtXW1MTZMP7mdlwbBpIinw3HztaIlYthEiA=
1921
github.com/manifoldco/promptui v0.9.0/go.mod h1:ka04sppxSGFAtxX0qhlYQjISsg9mR4GWtQEhdbn6Pgg=
2022
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=

pkg/api/libgen.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package api
33
import (
44
"fmt"
55
"github.com/PuerkitoBio/goquery"
6+
"github.com/kennygrant/sanitize"
67
"github.com/schollz/progressbar/v3"
78
"io"
89
"log"
@@ -248,8 +249,8 @@ func SearchBookByTitle(query string, limit int, libgenSite Site) ([]Book, error)
248249

249250
// DownloadSelection Downloads the file to current working directory
250251
func DownloadSelection(selectedBook Book, libgenType Site) {
251-
log.Println("Initializing download")
252252
link := getDirectDownloadLink(selectedBook.Mirrors[0], libgenType)
253+
log.Println("Initializing download " + link)
253254
req, _ := http.NewRequest("GET", link, nil)
254255
resp, error := http.DefaultClient.Do(req)
255256

@@ -258,14 +259,21 @@ func DownloadSelection(selectedBook Book, libgenType Site) {
258259
}
259260

260261
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)
262265
defer f.Close()
263266

264267
bar := progressbar.DefaultBytes(
265268
resp.ContentLength,
266269
"Downloading",
267270
)
268-
io.Copy(io.MultiWriter(f, bar), resp.Body)
269271

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+
}
271279
}

0 commit comments

Comments
 (0)