-
Notifications
You must be signed in to change notification settings - Fork 0
/
httpdownload.go
221 lines (189 loc) · 6.65 KB
/
httpdownload.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package gokits
import (
"errors"
"io"
"net/http"
"net/url"
"os"
"path/filepath"
"time"
)
type HttpDownload struct {
RawURL string
DstDir string
FileName string
OverWrite bool
}
type HttpDownloadOptions struct {
RawURL string
DstDir string
FileName string
OverWrite bool
}
var defaultHttpDownloadOptions = HttpDownloadOptions{
DstDir: "",
FileName: "",
OverWrite: false,
}
type HttpDownloadOption func(*HttpDownloadOptions)
func WithRawURL(rawURL string) HttpDownloadOption {
return func(o *HttpDownloadOptions) { o.RawURL = rawURL }
}
func WithDstDir(dstDir string) HttpDownloadOption {
return func(o *HttpDownloadOptions) { o.DstDir = dstDir }
}
func WithFileName(fileName string) HttpDownloadOption {
return func(o *HttpDownloadOptions) { o.FileName = fileName }
}
func WithOverWrite(overWrite bool) HttpDownloadOption {
return func(o *HttpDownloadOptions) { o.OverWrite = overWrite }
}
func NewHttpDownload(opts ...HttpDownloadOption) *HttpDownload {
options := defaultHttpDownloadOptions
for _, o := range opts {
o(&options)
}
return &HttpDownload{
RawURL: options.RawURL,
DstDir: options.DstDir,
FileName: options.FileName,
OverWrite: options.OverWrite,
}
}
type HttpDownloadDelegate struct {
DownloadDidFailWithError func(*HttpDownload, error)
DownloadDidStarted func(*HttpDownload)
DownloadingWithProgress func(*HttpDownload, float64)
DownloadingProgressInterval time.Duration
DownloadDidFinish func(*HttpDownload)
}
type HttpDownloadDelegateOptions struct {
DownloadDidFailWithError func(*HttpDownload, error)
DownloadDidStarted func(*HttpDownload)
DownloadingWithProgress func(*HttpDownload, float64)
DownloadingProgressInterval time.Duration
DownloadDidFinish func(*HttpDownload)
}
var defaultHttpDownloadDelegateOptions = HttpDownloadDelegateOptions{
DownloadingProgressInterval: time.Second,
}
type HttpDownloadDelegateOption func(*HttpDownloadDelegateOptions)
func WithDownloadDidFailWithError(f func(*HttpDownload, error)) HttpDownloadDelegateOption {
return func(o *HttpDownloadDelegateOptions) { o.DownloadDidFailWithError = f }
}
func WithDownloadDidStarted(f func(*HttpDownload)) HttpDownloadDelegateOption {
return func(o *HttpDownloadDelegateOptions) { o.DownloadDidStarted = f }
}
func WithDownloadingWithProgress(f func(*HttpDownload, float64)) HttpDownloadDelegateOption {
return func(o *HttpDownloadDelegateOptions) { o.DownloadingWithProgress = f }
}
func WithDownloadingProgressInterval(interval time.Duration) HttpDownloadDelegateOption {
return func(o *HttpDownloadDelegateOptions) { o.DownloadingProgressInterval = interval }
}
func WithDownloadDidFinish(f func(*HttpDownload)) HttpDownloadDelegateOption {
return func(o *HttpDownloadDelegateOptions) { o.DownloadDidFinish = f }
}
func (download *HttpDownload) Start(opts ...HttpDownloadDelegateOption) {
options := defaultHttpDownloadDelegateOptions
for _, o := range opts {
o(&options)
}
delegate := HttpDownloadDelegate{
DownloadDidFailWithError: options.DownloadDidFailWithError,
DownloadDidStarted: options.DownloadDidStarted,
DownloadingWithProgress: options.DownloadingWithProgress,
DownloadingProgressInterval: options.DownloadingProgressInterval,
DownloadDidFinish: options.DownloadDidFinish,
}
parsedURL, err := url.Parse(download.RawURL)
if nil != err && nil != delegate.DownloadDidFailWithError {
delegate.DownloadDidFailWithError(download, err)
return
}
fileName := filepath.Base(parsedURL.Path)
if 0 != len(download.FileName) {
fileName = download.FileName
}
dstDir := "."
if 0 != len(download.DstDir) {
dstDir = download.DstDir
}
dir, err := os.Stat(dstDir)
if nil == err && !dir.IsDir() && nil != delegate.DownloadDidFailWithError { // exists, but not directory
delegate.DownloadDidFailWithError(download, errors.New("DstDir exists, but not directory"))
return
}
err = os.MkdirAll(dstDir, 0777)
if nil != err && nil != delegate.DownloadDidFailWithError {
delegate.DownloadDidFailWithError(download, err)
return
}
fileDir := PathJoin(dstDir, fileName)
_, err = os.Stat(fileDir)
if nil == err && !download.OverWrite && nil != delegate.DownloadDidFailWithError { // exists, don't overwrite
delegate.DownloadDidFailWithError(download, errors.New("DstFile exists, don't overwrite"))
return
}
go download.internalDownload(fileDir, delegate)
}
func (download *HttpDownload) internalDownload(fileDir string, delegate HttpDownloadDelegate) {
file, err := os.Create(fileDir)
if err != nil && nil != delegate.DownloadDidFailWithError {
delegate.DownloadDidFailWithError(download, err)
return
}
defer func() { _ = file.Close() }()
res, err := http.Get(download.RawURL)
if err != nil {
delegate.DownloadDidFailWithError(download, err)
return
}
defer func() { _ = res.Body.Close() }()
reader := &_HttpDownloadReader{
Reader: res.Body,
Total: res.ContentLength,
DelegateInterval: delegate.DownloadingProgressInterval,
}
if nil != delegate.DownloadingWithProgress {
reader.Delegate = &_HttpDownloadReaderDelegate{
Downloading: func(reader *_HttpDownloadReader) {
delegate.DownloadingWithProgress(download, reader.Progress)
},
}
}
if nil != delegate.DownloadDidStarted {
delegate.DownloadDidStarted(download)
}
_, _ = io.Copy(file, reader)
if nil != delegate.DownloadDidFinish {
delegate.DownloadDidFinish(download)
}
}
type _HttpDownloadReaderDelegate struct {
Downloading func(*_HttpDownloadReader)
}
type _HttpDownloadReader struct {
io.Reader
Total int64
Current int64
Progress float64
Delegate *_HttpDownloadReaderDelegate
DelegateInterval time.Duration
DelegateTime time.Time
}
func (r *_HttpDownloadReader) Read(p []byte) (n int, err error) {
if r.DelegateTime.IsZero() {
r.DelegateTime = time.Now()
}
n, err = r.Reader.Read(p)
r.Current += int64(n)
if time.Now().Sub(r.DelegateTime) < r.DelegateInterval {
return
}
r.DelegateTime = time.Now()
r.Progress = float64(r.Current*10000/r.Total) / 100
if nil != r.Delegate && nil != r.Delegate.Downloading {
r.Delegate.Downloading(r)
}
return
}