This repository was archived by the owner on Feb 27, 2023. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathdownloader.go
More file actions
147 lines (115 loc) · 2.58 KB
/
Copy pathdownloader.go
File metadata and controls
147 lines (115 loc) · 2.58 KB
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
package dataloading
import (
"os"
"time"
"math/rand"
"io"
"net/http"
"encoding/csv"
)
type Fetcher func(url string) (io.ReadCloser, error)
type Downloader struct {
directory string
downloader Fetcher
cache map[string]string
}
// randSeq from http://stackoverflow.com/questions/22892120/how-to-generate-a-random-string-of-a-fixed-length-in-golang
var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
func randSeq(n int) string {
b := make([]rune, n)
for i := range b {
b[i] = letters[rand.Intn(len(letters))]
}
return string(b)
}
func loadDownloaderCache(directory string) map[string]string {
ce := map[string]string{}
file, err := os.Open(directory + "/cache.csv")
if err != nil {
return ce
}
defer file.Close()
csvReader := csv.NewReader(file)
records, err := csvReader.ReadAll()
if err != nil {
return ce
}
for _, record := range records {
ce[record[0]] = record[1]
}
return ce
}
func saveDownloaderCache(directory string, cache map[string]string) error {
file, err := os.Create(directory + "/cache.csv")
if err != nil {
return err
}
defer file.Close()
writer := csv.NewWriter(file)
for key, value := range cache {
writer.Write([]string{ key, value })
}
writer.Flush()
return nil
}
func defaultFetcher(url string) (io.ReadCloser, error) {
resp, err := http.Get(url)
if err != nil {
return nil, err
}
return resp.Body, nil
}
func NewDownloader(directory string, fetcher Fetcher) *Downloader {
if fetcher == nil {
fetcher = defaultFetcher
}
rand.Seed(time.Now().UTC().UnixNano())
if _, err := os.Stat(directory); err != nil {
if os.IsNotExist(err) {
os.MkdirAll(directory, 0755)
}
}
cache := loadDownloaderCache(directory)
return &Downloader{
directory,
fetcher,
cache,
}
}
func (d *Downloader) getCached(url string) string {
return d.cache[url]
}
func (d *Downloader) updateCache(url string, path string) {
d.cache[url] = path
// Eat error as nothing to be done/ not fatal
_ = saveDownloaderCache(d.directory, d.cache)
return
}
func (d *Downloader) Fetch(url string) (string, error) {
cachedPath := d.getCached(url)
if cachedPath != "" {
return cachedPath, nil
}
path := randSeq(32)
outputPath := d.directory + path
out, err := os.Create(outputPath)
if err != nil {
return "", err
}
defer out.Close()
rc, err := d.downloader(url)
if err != nil {
return "", err
}
defer rc.Close()
_, err = io.Copy(out, rc)
if err != nil {
return "", err
}
d.updateCache(url, outputPath)
return outputPath, nil
}
func (d *Downloader) Clear() error {
err := os.RemoveAll(d.directory)
return err
}