-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathoptions.go
More file actions
193 lines (167 loc) · 3.99 KB
/
Copy pathoptions.go
File metadata and controls
193 lines (167 loc) · 3.99 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
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
package rain
import (
"io"
"io/fs"
"net/http"
"path/filepath"
"time"
)
// WithDebug 设置 debug 调试信息开关
func WithDebug(d bool) OptionFunc {
return func(ctl *control) {
ctl.setDebug(d)
}
}
// WithOutdir 文件输出目录
func WithOutdir(outdir string) OptionFunc {
return func(ctl *control) {
ctl.outdir = outdir
}
}
// WithOutname 文件输出名称
func WithOutname(outname string) OptionFunc {
return func(ctl *control) {
ctl.outname = outname
}
}
// WithOutpath 文件输出路径
func WithOutpath(outpath string) OptionFunc {
return func(ctl *control) {
ctl.outdir, ctl.outname = filepath.Split(outpath)
}
}
// WithEvent 事件监听
func WithEvent(e ...ProgressEvent) OptionFunc {
return func(ctl *control) {
ctl.addEvent(e...)
}
}
// WithEventExtend 事件监听
func WithEventExtend(e ...ProgressEventExtend) OptionFunc {
return func(ctl *control) {
ctl.addEventExtend(e...)
}
}
// WithBar 进度条
func WithBar(bars ...*Bar) OptionFunc {
return func(ctl *control) {
var bar *Bar
if len(bars) > 0 {
bar = bars[0]
} else {
bar = NewBar()
}
ctl.addEventExtend(bar)
}
}
// WithClient 设置默认请求客户端
func WithClient(d *http.Client) OptionFunc {
return func(ctl *control) {
ctl.request.client = d
}
}
// WithMethod 设置默认请求方式
func WithMethod(d string) OptionFunc {
return func(ctl *control) {
ctl.request.method = d
}
}
// WithBody 设置默认请求 Body
func WithBody(d io.Reader) OptionFunc {
return func(ctl *control) {
if d != nil {
d = NewMultiReadable(d)
}
ctl.request.body = d
}
}
// WithHeader 设置默认请求 Header
func WithHeader(d func(h http.Header)) OptionFunc {
return func(ctl *control) {
d(ctl.request.header)
}
}
// WithPerm 设置默认输出文件权限
func WithPerm(d fs.FileMode) OptionFunc {
return func(ctl *control) {
ctl.perm = d
}
}
// WithRoutineSize 设置协程下载最大字节数
func WithRoutineSize(d int64) OptionFunc {
return func(ctl *control) {
ctl.config.RoutineSize = d
}
}
// WithRoutineCount 设置协程最大数
func WithRoutineCount(d int) OptionFunc {
return func(ctl *control) {
ctl.config.RoutineCount = d
}
}
// WithDiskCache 设置磁盘缓冲区大小
func WithDiskCache(d int) OptionFunc {
return func(ctl *control) {
ctl.config.DiskCache = d
}
}
// WithSpeedLimit 设置下载速度限制
func WithSpeedLimit(d int) OptionFunc {
return func(ctl *control) {
ctl.config.SpeedLimit = d
}
}
// WithCreateDir 设置是否可以创建目录
func WithCreateDir(d bool) OptionFunc {
return func(ctl *control) {
ctl.config.CreateDir = d
}
}
// WithAllowOverwrite 设置是否允许覆盖下载文件
func WithAllowOverwrite(d bool) OptionFunc {
return func(ctl *control) {
ctl.config.AllowOverwrite = d
}
}
// WithBreakpointResume 设置是否启用断点续传
func WithBreakpointResume(d bool) OptionFunc {
return func(ctl *control) {
ctl.config.BreakpointResume = d
}
}
// WithAutoFileRenaming 设置是否自动重命名文件,新文件名在名称之后扩展名之前加上一个点和一个数字(1..9999)
func WithAutoFileRenaming(d bool) OptionFunc {
return func(ctl *control) {
ctl.config.AutoFileRenaming = d
}
}
// WithAutoFilterFilename 设置是否自动过滤掉文件名称中的非法字符
func WithAutoFilterFilename(d bool) OptionFunc {
return func(ctl *control) {
ctl.config.AutoFilterFilename = d
}
}
// WithTimeout 设置下载超时时间
func WithTimeout(d time.Duration) OptionFunc {
return func(ctl *control) {
ctl.config.Timeout = d
}
}
// WithRetryNumber 设置请求重试次数
func WithRetryNumber(d int) OptionFunc {
return func(ctl *control) {
ctl.config.RetryNumber = d
}
}
// WithRetryNumber 设置请求重试的间隔时间
func WithRetryTime(d time.Duration) OptionFunc {
return func(ctl *control) {
ctl.config.RetryTime = d
}
}
// WithBreakpointExt 断点文件扩展, 默认为 .temp.rain
func WithBreakpointExt(d string) OptionFunc {
return func(ctl *control) {
ctl.config.BreakpointExt = d
}
}