-
Notifications
You must be signed in to change notification settings - Fork 3
/
downloader.go
81 lines (67 loc) · 1.5 KB
/
downloader.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package main
import (
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"time"
)
func DownloadFile(url, dest string) error {
// If there's a file, don't download it twice
if _, err := os.Stat(dest); err == nil {
return nil
}
dir, _ := filepath.Split(dest)
os.MkdirAll(dir, 0755)
out, err := os.Create(dest)
if err != nil {
return err
}
defer out.Close()
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return err
}
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("bad status: %s", resp.Status)
}
// Get file size
size := resp.ContentLength
progress := &ProgressReader{Reader: resp.Body, Total: size}
// Enable progressbar goroutine
go progress.PrintProgress()
_, err = io.Copy(out, progress)
if err != nil {
return err
}
return nil
}
type ProgressReader struct {
io.Reader
Total int64
Current int64
}
func (pr *ProgressReader) Read(p []byte) (int, error) {
n, err := pr.Reader.Read(p)
pr.Current += int64(n)
return n, err
}
func (pr *ProgressReader) PrintProgress() {
for {
percentage := float64(pr.Current) / float64(pr.Total) * 100
log.Infof("Downloading... %.2f%% complete", percentage)
if pr.Current >= pr.Total {
log.Info("Download complete")
break
}
time.Sleep(1 * time.Second)
}
}