-
Notifications
You must be signed in to change notification settings - Fork 0
/
download.go
68 lines (65 loc) · 1.57 KB
/
download.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
package mmdb
import (
"archive/tar"
"compress/gzip"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
)
const (
downloadUrl = "https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-Country&license_key=%s&suffix=tar.gz"
)
func Download(filePath, licenseKey string, client ...*http.Client) (*http.Response, error) {
c := http.DefaultClient
if len(client) > 0 {
c = client[0]
}
downloadURL := fmt.Sprintf(downloadUrl, licenseKey)
resp, err := c.Get(downloadURL)
if err != nil {
return resp, err
}
if resp.StatusCode != http.StatusOK {
return resp, fmt.Errorf("download returned statusCode: %d", resp.StatusCode)
}
defer resp.Body.Close()
uncompressedStream, err := gzip.NewReader(resp.Body)
if err != nil {
return resp, fmt.Errorf("gzip.NewReader: %w", err)
}
tarReader := tar.NewReader(uncompressedStream)
for {
header, err := tarReader.Next()
if err == io.EOF {
break
}
if err != nil {
return resp, fmt.Errorf("tarReader.Next: %w", err)
}
switch header.Typeflag {
case tar.TypeDir:
continue
case tar.TypeReg:
if filepath.Ext(header.Name) != ".mmdb" { // Skip all but the database
continue
}
outFile, err := os.Create(filePath)
if err != nil {
return resp, fmt.Errorf("os.Create: %w", err)
}
if _, err := io.Copy(outFile, tarReader); err != nil {
return resp, fmt.Errorf("io.Copy: %w", err)
}
_ = outFile.Close()
default:
err = fmt.Errorf(
"ExtractTarGz: uknown type: %v in %s",
header.Typeflag,
header.Name)
return resp, fmt.Errorf("header.Typeflag: %w", err)
}
}
return resp, nil
}