-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.go
97 lines (88 loc) · 2.45 KB
/
config.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
package oget
import (
"context"
"fmt"
"path/filepath"
"time"
)
type RemoteFile struct {
// 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 URL of the file to download.
URL string
// 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
}
type GettingConfig struct {
// the path to save the downloaded file.
FilePath string
// 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 (config *RemoteFile) standardize() RemoteFile {
c := *config
if c.Context == nil {
c.Context = context.Background()
}
if c.MaxIdleConnsPerHost <= 0 {
c.MaxIdleConnsPerHost = 16
}
if c.Timeout == 0 {
c.Timeout = time.Duration(10) * time.Second
}
return c
}
func (c *GettingConfig) fileName() string {
_, fileName := filepath.Split(c.FilePath)
return fileName
}
func (c *GettingConfig) dirPath() string {
dirPath, _ := filepath.Split(c.FilePath)
return dirPath
}
func (c *GettingConfig) partFileName(index int) string {
var fileName string
if c.Parts == 1 {
fileName = fmt.Sprintf("%s.downloading", c.PartName)
} else {
fileName = fmt.Sprintf("%s.%d.%d.downloading", c.PartName, c.Parts, index)
}
return fileName
}
func (config *GettingConfig) standardize() GettingConfig {
c := *config
if c.Parts <= 0 {
c.Parts = 1
}
if c.PartName == "" {
c.PartName = c.fileName()
}
if c.PartsPath == "" {
c.PartsPath = c.dirPath()
}
return c
}