/
zip_buildpack.go
66 lines (55 loc) · 1.59 KB
/
zip_buildpack.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
package buildpackrunner
import (
"crypto/tls"
"fmt"
"net/url"
"os"
"path/filepath"
"strings"
"code.cloudfoundry.org/archiver/extractor"
"code.cloudfoundry.org/cacheddownloader"
"code.cloudfoundry.org/lager/v3"
)
type ZipDownloader struct {
downloader *cacheddownloader.Downloader
}
func IsZipFile(filename string) bool {
return strings.HasSuffix(filename, ".zip")
}
func NewZipDownloader(skipSSLVerification bool) *ZipDownloader {
tlsConfig := &tls.Config{
InsecureSkipVerify: skipSSLVerification,
}
return &ZipDownloader{
downloader: cacheddownloader.NewDownloader(DOWNLOAD_TIMEOUT, 1, tlsConfig),
}
}
func (z *ZipDownloader) DownloadAndExtract(u *url.URL, destination string) (uint64, error) {
zipFile, err := os.CreateTemp("", filepath.Base(u.Path))
if err != nil {
return 0, fmt.Errorf("Could not create zip file: %s", err.Error())
}
defer os.Remove(zipFile.Name())
_, _, err = z.downloader.Download(
lager.NewLogger("noop"),
u,
func() (*os.File, error) {
return os.OpenFile(zipFile.Name(), os.O_WRONLY, 0666)
},
cacheddownloader.CachingInfoType{},
cacheddownloader.ChecksumInfoType{},
make(chan struct{}),
)
if err != nil {
return 0, fmt.Errorf("Failed to download buildpack '%s': %s", u.String(), err.Error())
}
fi, err := zipFile.Stat()
if err != nil {
return 0, fmt.Errorf("Failed to obtain the size of the buildpack '%s': %s", u.String(), err.Error())
}
err = extractor.NewZip().Extract(zipFile.Name(), destination)
if err != nil {
return 0, fmt.Errorf("Failed to extract buildpack '%s': %s", u.String(), err.Error())
}
return uint64(fi.Size()), nil
}