This repository has been archived by the owner on Mar 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Downloader.go
81 lines (64 loc) · 1.74 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 (
"errors"
"fmt"
"github.com/mitchellh/ioprogress"
"io"
"net/http"
"os"
"path/filepath"
"runtime"
"time"
)
const downloadServer = "https://dl.gitea.io/gitea/"
func DownloadGitea(version string) (path string, err error) {
// Creating the destination file
file, err := os.Create("gitea-" + version)
if err != nil {
return "", err
}
defer file.Close()
// Creating the url by the version and the running system
url := downloadServer + version + "/gitea-" + version + "-" + runtime.GOOS + "-" + runtime.GOARCH
// Downloading the content
resp, err := http.Get(url)
if err != nil {
return "", err
}
defer resp.Body.Close()
// If the file doesn't exists we return an error
if resp.StatusCode == 404 {
return "", errors.New("File " + resp.Request.URL.String() + " could not be found (404)!")
}
// Showing the progress
progress := downloadProgress(resp.Body, resp.ContentLength)
// Writing it to the file
_, err = io.Copy(file, progress)
if err != nil {
return "", err
}
// Getting the full path of the file
path, err = filepath.Abs(file.Name())
if err != nil {
return "", err
}
// Returning the full path
return path, nil
}
func downloadProgress(reader io.ReadCloser, size int64) (progress io.Reader) {
duration, _ := time.ParseDuration("20ms")
drawFunc := ioprogress.DrawTerminalf(os.Stdout, func(progress, total int64) string {
// Dividing to get to MegaBytes
downloaded := float64(progress) / 1024.0 / 1024.0
size := float64(total) / 1024.0 / 1024.0
// Display the values
return fmt.Sprintf("Downloading: %.2f MB/%.2f MB", downloaded, size)
})
progress = &ioprogress.Reader{
Reader: reader,
Size: size,
DrawInterval: duration,
DrawFunc: drawFunc,
}
return progress
}