forked from buildpacks/pack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdownloader.go
164 lines (134 loc) · 3.2 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package pack
import (
"crypto/sha256"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"os"
"path/filepath"
"regexp"
"github.com/pkg/errors"
"github.com/buildpack/pack/internal/paths"
"github.com/buildpack/pack/logging"
)
const (
cacheDirPrefix = "c"
cacheVersion = "1"
)
type Downloader struct {
logger logging.Logger
baseCacheDir string
}
var schemeRegexp = regexp.MustCompile(`^.+://.*`)
func NewDownloader(logger logging.Logger, baseCacheDir string) *Downloader {
return &Downloader{
logger: logger,
baseCacheDir: baseCacheDir,
}
}
func (d *Downloader) Download(pathOrUri string) (string, error) {
hasScheme := schemeRegexp.MatchString(pathOrUri)
if hasScheme {
parsedUrl, err := url.Parse(pathOrUri)
if err != nil {
return "", err
}
switch parsedUrl.Scheme {
case "file":
return paths.UriToFilePath(pathOrUri)
case "http", "https":
return d.handleHTTP(pathOrUri)
default:
return "", fmt.Errorf("unsupported protocol '%s' in URI %q", parsedUrl.Scheme, pathOrUri)
}
} else {
return d.handleFile(pathOrUri)
}
}
func (d *Downloader) handleFile(path string) (string, error) {
var (
err error
)
if path, err = filepath.Abs(path); err != nil {
return "", nil
}
return path, nil
}
func (d *Downloader) handleHTTP(uri string) (string, error) {
cacheDir := d.versionedCacheDir()
if err := os.MkdirAll(cacheDir, 0744); err != nil {
return "", err
}
cachePath := filepath.Join(cacheDir, fmt.Sprintf("%x", sha256.Sum256([]byte(uri))))
tgzFile := cachePath + ".tgz"
etagFile := cachePath + ".etag"
etagExists, err := fileExists(etagFile)
if err != nil {
return "", err
}
etag := ""
if etagExists {
bytes, err := ioutil.ReadFile(etagFile)
if err != nil {
return "", err
}
etag = string(bytes)
}
reader, etag, err := d.downloadAsStream(uri, etag)
if err != nil {
return "", errors.Wrapf(err, "failed to download from %q", uri)
} else if reader == nil {
return tgzFile, nil
}
defer reader.Close()
fh, err := os.Create(tgzFile)
if err != nil {
return "", err
}
defer fh.Close()
_, err = io.Copy(fh, reader)
if err != nil {
return "", err
}
if err = ioutil.WriteFile(etagFile, []byte(etag), 0744); err != nil {
return "", err
}
return tgzFile, nil
}
func (d *Downloader) downloadAsStream(uri string, etag string) (io.ReadCloser, string, error) {
req, err := http.NewRequest("GET", uri, nil)
if err != nil {
return nil, "", err
}
if etag != "" {
req.Header.Set("If-None-Match", etag)
}
resp, err := (&http.Client{}).Do(req)
if err != nil {
return nil, "", err
}
if resp.StatusCode >= 200 && resp.StatusCode < 300 {
d.logger.Debugf("Downloading from %q", uri)
return resp.Body, resp.Header.Get("Etag"), nil
}
if resp.StatusCode == 304 {
d.logger.Debugf("Using cached version of %q", uri)
return nil, etag, nil
}
return nil, "", fmt.Errorf("could not download from %q, code http status %d", uri, resp.StatusCode)
}
func (d *Downloader) versionedCacheDir() string {
return filepath.Join(d.baseCacheDir, cacheDirPrefix+cacheVersion)
}
func fileExists(file string) (bool, error) {
_, err := os.Stat(file)
if err != nil {
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
return true, nil
}