-
Notifications
You must be signed in to change notification settings - Fork 1
/
get.go
66 lines (63 loc) · 2.03 KB
/
get.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 oget
import (
"context"
"time"
)
type OGet struct {
// the URL of the file to download.
URL string
// the path to save the downloaded file.
FilePath string
// the context for the http request.
// if the value is nil, the context will be context.Background().
Context context.Context
// the maximum amount of time a dial will wait for a connect or copy to complete.
// the default is 10 seconds.
Timeout time.Duration
// the User-Agent header field value.
// if the value is empty, the User-Agent header will not be set.
Useragent string
// the Referer header field value.
// if the value is empty, the Referer header will not be set.
Referer string
// the maximum number of idle (keep-alive) connections to keep per-host.
// the default is 16.
MaxIdleConnsPerHost int
// the SHA512 code of the file.
// if the code is empty, the file will not be checked.
SHA512 string
// PartsPath is the path to save the temp files of downloaded parts.
// if the value is empty, the temp files will be saved in the same directory as the FilePath.
PartsPath string
// the name of the part file.
// if the value is empty, the name will be the same as the file name.
PartName string
// the number of parts to download the file.
// if the value is less than or equal to 0, the file will be downloaded in one part.
Parts int
// the progress listener.
// if the value is nil, the progress will not be listened.
ListenProgress ProgressListener
}
func (o *OGet) Get() (func() error, error) {
clean := func() error { return nil }
task, err := CreateGettingTask(&RemoteFile{
Context: o.Context,
Timeout: o.Timeout,
URL: o.URL,
Useragent: o.Useragent,
Referer: o.Referer,
MaxIdleConnsPerHost: o.MaxIdleConnsPerHost,
})
if err != nil {
return clean, err
}
return task.Get(&GettingConfig{
FilePath: o.FilePath,
SHA512: o.SHA512,
PartsPath: o.PartsPath,
PartName: o.PartName,
Parts: o.Parts,
ListenProgress: o.ListenProgress,
})
}