-
Notifications
You must be signed in to change notification settings - Fork 1
/
task.go
294 lines (263 loc) · 6.59 KB
/
task.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
package oget
import (
"context"
"fmt"
"io"
"mime"
"net/http"
"os"
"time"
"path/filepath"
"github.com/pkg/errors"
"golang.org/x/sync/errgroup"
)
type GettingTask struct {
url string
filename string
useragent string
referer string
contentLength int64
client *http.Client
context context.Context
timeout time.Duration
}
// creates a new GettingTask. will access the URL to get the file information.
func CreateGettingTask(config *RemoteFile) (*GettingTask, error) {
c := config.standardize()
client := newGettingClient(c.MaxIdleConnsPerHost)
ctx, cancel := context.WithTimeout(c.Context, c.Timeout)
defer cancel()
req, err := http.NewRequest("HEAD", c.URL, nil)
if err != nil {
return nil, errors.Wrap(err, "failed to make head request")
}
req = req.WithContext(ctx)
if config.Useragent != "" {
req.Header.Set("User-Agent", config.Useragent)
}
if config.Referer != "" {
req.Header.Set("Referer", config.Referer)
}
resp, err := client.Do(req)
if err != nil {
return nil, errors.Wrap(err, "failed to head request")
}
if resp.Header.Get("Accept-Ranges") != "bytes" {
return nil, errors.New("does not support range request")
}
if resp.ContentLength <= 0 {
return nil, errors.New("invalid content length")
}
filename := ""
_, params, _ := mime.ParseMediaType(resp.Header.Get("Content-Disposition"))
if len(params) > 0 && params["filename"] != "" {
filename = params["filename"]
}
task := &GettingTask{
url: c.URL,
filename: filename,
useragent: c.Useragent,
referer: c.Referer,
contentLength: resp.ContentLength,
client: client,
context: c.Context,
timeout: c.Timeout,
}
return task, nil
}
// returns the content length of the file.
func (t *GettingTask) ContentLength() int64 {
return t.contentLength
}
// downloads the file.
func (t *GettingTask) Get(config *GettingConfig) (func() error, error) {
var prog *progress
c := config.standardize()
tasks := []*subTask{}
if c.ListenProgress != nil {
prog = downloadingProgress(t.contentLength, c.ListenProgress)
}
for i := 0; i < c.Parts; i++ {
task := t.getPartTask(&c, i)
if task != nil {
tasks = append(tasks, task)
}
}
clean := func() error {
return t.cleanPartFiles(&c)
}
if len(tasks) > 0 {
err := os.MkdirAll(c.PartsPath, 0755)
if err != nil {
return clean, err
}
}
eg, ctx := errgroup.WithContext(t.context)
for _, task := range tasks {
task := task
eg.Go(func() error {
req, err := t.createRequest(ctx)
if err != nil {
return err
}
if len(tasks) > 1 || !tasks[0].overrideFile {
req.Header.Set("Range", fmt.Sprintf("bytes=%d-%d", task.begin, task.end))
}
if err := t.downloadToFile(req, task, prog); err != nil {
return err
}
return nil
})
}
if err := eg.Wait(); err != nil {
return clean, err
}
if prog != nil {
prog = prog.toCopingPhase()
}
if err := t.mergeFile(&c, prog); err != nil {
return clean, err
}
if prog != nil {
prog.fireDone()
}
clean = func() error { return nil }
return clean, nil
}
func (t *GettingTask) createRequest(ctx context.Context) (*http.Request, error) {
req, err := http.NewRequest("GET", t.url, nil)
if err != nil {
return nil, errors.Wrap(err, "failed to make a new request")
}
req = req.WithContext(ctx)
if t.useragent != "" {
req.Header.Set("User-Agent", t.useragent)
}
if t.referer != "" {
req.Header.Set("Referer", t.referer)
}
return req, nil
}
func (t *GettingTask) downloadToFile(req *http.Request, task *subTask, prog *progress) error {
resp, err := t.client.Do(req)
if err != nil {
return errors.Wrapf(err, "failed to get response: %q", err)
}
defer resp.Body.Close()
flag := os.O_WRONLY | os.O_CREATE
if !task.overrideFile {
flag |= os.O_APPEND
}
output, err := os.OpenFile(task.path, flag, 0666)
if err != nil {
return errors.Wrapf(err, "failed to write file")
}
defer output.Close()
var respReader io.Reader = resp.Body
if prog != nil {
respReader = prog.reader(respReader)
}
written, err := io.Copy(output, respReader)
if err != nil {
return errors.Wrapf(err, "failed to write response body")
}
wantSize := task.end - task.begin + 1
if written < wantSize {
return errors.New("download bytes is less than expected")
}
return nil
}
func (t *GettingTask) mergeFile(c *GettingConfig, prog *progress) error {
err := os.MkdirAll(c.dirPath(), 0755)
if err != nil {
return errors.Wrapf(err, "make directory failed")
}
partPathList := []string{}
for i := 0; i < c.Parts; i++ {
partPath := filepath.Join(c.PartsPath, c.partFileName(i))
partPathList = append(partPathList, partPath)
}
if c.SHA512 != "" {
code, err := sha512OfFiles(&partPathList)
if err != nil {
return errors.Wrapf(err, "failed to get sha512 code")
}
if code != c.SHA512 {
return createSHA512Error("sha512 code does not match")
}
}
if len(partPathList) == 1 {
partPath := partPathList[0]
err := os.Rename(partPath, c.FilePath)
if err != nil {
return errors.Wrapf(err, "failed to move file")
}
} else {
targetFile, err := os.Create(c.FilePath)
if err != nil {
return errors.Wrap(err, "failed to create a file in download location")
}
defer targetFile.Close()
for _, partPath := range partPathList {
subFile, err := os.Open(partPath)
if err != nil {
return errors.Wrapf(err, "failed to open file in download location")
}
defer subFile.Close()
var reader io.Reader = subFile
if prog != nil {
reader = prog.reader(reader)
}
_, err = io.Copy(targetFile, reader)
if err != nil {
errors.Wrapf(err, "failed to copy part of file")
}
}
t.cleanPartFiles(c)
}
return nil
}
func (t *GettingTask) cleanPartFiles(c *GettingConfig) error {
for i := 0; i < c.Parts; i++ {
partPath := filepath.Join(c.PartsPath, c.partFileName(i))
if err := os.Remove(partPath); err != nil {
if !os.IsNotExist(err) {
return err
}
}
}
return nil
}
type subTask struct {
begin int64
end int64
path string
overrideFile bool
}
func (t *GettingTask) getPartTask(c *GettingConfig, index int) *subTask {
chunkSize := t.contentLength / int64(c.Parts)
begin := chunkSize * int64(index)
end := int64(0)
if index == c.Parts-1 {
end = t.contentLength - 1
} else {
end = begin + chunkSize - 1
}
filePath := filepath.Join(c.PartsPath, c.partFileName(index))
info, err := os.Stat(filePath)
overrideFile := false
if err != nil {
overrideFile = true
} else {
begin += info.Size()
if begin > end {
return nil
}
}
return &subTask{
begin: begin,
end: end,
path: filePath,
overrideFile: overrideFile,
}
}