Skip to content

Commit 9f73c73

Browse files
committed
Removed url validation from package, Added multiple urls support
1 parent c6986e3 commit 9f73c73

File tree

4 files changed

+50
-21
lines changed

4 files changed

+50
-21
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@
1212

1313
# Project-local glide cache, RE: https://github.com/Masterminds/glide/issues/736
1414
.glide/
15+
test

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ There are a lot of tool similar and better than Pluto but most of them have an u
1414

1515
go get github.com/ishanjain28/pluto
1616

17-
2. See the [Releases](https://github.com/ishanjain28/pluto/releases) for Precompiled Binaries
17+
2. See the [Releases](https://github.com/ishanjain28/pluto/releases) section for Precompiled Binaries
1818

1919
### Package Example:
2020

pluto/pluto.go

+2-7
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,12 @@ type FileMeta struct {
2828
// then downloads the file by dividing it into given number of parts and downloading all parts concurrently.
2929
// If any error occurs in the downloading stage of any part, It'll wait for 2 seconds, Discard the existing part and restart it.
3030
// Discarding whatever bytes were downloaded isn't exactly a smart, So, I'll also be implementing a feature where it can skip over what is already downloaded.
31-
func Download(link string, parts int) (*os.File, error) {
31+
func Download(linkp *url.URL, parts int) (*os.File, error) {
3232

33-
if link == "" {
33+
if linkp == nil {
3434
return nil, fmt.Errorf("No URL Provided")
3535
}
3636

37-
linkp, err := url.Parse(link)
38-
if err != nil {
39-
return nil, fmt.Errorf("error in parsing url: %v", err)
40-
}
41-
4237
fmeta, err := FetchMeta(linkp)
4338
if err != nil {
4439
return nil, fmt.Errorf("error in fetching metadata: %v", err)

pluto_cli.go

+46-13
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ import (
55
"fmt"
66
"io"
77
"log"
8+
"net/url"
89
"os"
910
"os/signal"
11+
"path/filepath"
1012
"syscall"
1113
"time"
1214

@@ -15,15 +17,6 @@ import (
1517

1618
func main() {
1719

18-
u := flag.String("url", "", "Download link of a file")
19-
20-
parts := flag.Int("part", 16, "Number of Download parts")
21-
22-
flag.Parse()
23-
if *u == "" {
24-
log.Fatalln("no url provided")
25-
}
26-
2720
sig := make(chan os.Signal, 1)
2821
signal.Notify(sig, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
2922

@@ -33,24 +26,64 @@ func main() {
3326
os.Exit(0)
3427
}()
3528

29+
parts := flag.Int("part", 16, "Number of Download parts")
30+
31+
flag.Parse()
32+
urls := []string{}
33+
u := ""
34+
35+
if len(os.Args) <= 1 {
36+
fmt.Printf("URL: ")
37+
fmt.Scanf("%s\n", &u)
38+
if u == "" {
39+
log.Fatalln("No URL Provided")
40+
}
41+
42+
download(u, *parts)
43+
} else {
44+
45+
if *parts == 0 {
46+
urls = os.Args[1:]
47+
} else {
48+
urls = os.Args[2:]
49+
}
50+
51+
for _, v := range urls {
52+
download(v, *parts)
53+
}
54+
}
55+
56+
}
57+
58+
func download(u string, parts int) {
3659
a := time.Now()
37-
f, err := pluto.Download(*u, *parts)
60+
61+
up, err := url.Parse(u)
62+
if err != nil {
63+
log.Fatalln("Invalid URL")
64+
}
65+
66+
fname := filepath.Base(up.String())
67+
68+
fmt.Printf("Downloading %s\n", up.String())
69+
70+
f, err := pluto.Download(up, parts)
3871
if err != nil {
3972
log.Fatalln(err)
4073
}
4174
defer f.Close()
4275

43-
file, err := os.Create("downloaded_file")
76+
file, err := os.Create(fname)
4477
if err != nil {
4578
log.Fatalln(err.Error())
4679
}
4780

4881
defer file.Close()
82+
defer os.Remove(f.Name())
4983

50-
fmt.Printf("File Downloaded in %s", time.Since(a))
84+
fmt.Printf("Downloaded %s in %s\n", up.String(), time.Since(a))
5185
_, err = io.Copy(file, f)
5286
if err != nil {
5387
log.Fatalln(err.Error())
5488
}
55-
5689
}

0 commit comments

Comments
 (0)