-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcontrol.go
More file actions
298 lines (259 loc) · 6.03 KB
/
Copy pathcontrol.go
File metadata and controls
298 lines (259 loc) · 6.03 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
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
295
296
297
298
package rain
import (
"context"
"errors"
"io/fs"
"log"
"os"
"path/filepath"
"sync"
"golang.org/x/time/rate"
)
type control struct {
// debug 调试模式
debug bool
// ctx 上下文
ctx context.Context
// cancel 取消上下文
cancel context.CancelFunc
// config 配置项
config *Config
// request 请求器
request *request
// uri 资源链接
uri string
// outdir 输出目录
outdir string
// outname 输出名称
outname string
// perm 新建文件的权限, 默认为 0600
perm fs.FileMode
// bpfilepath 断点文件
bpfilepath string
// outpath 输出文件
outpath string
// status 运行状态
status Status
// totalSize 资源大小
totalSize int64
// completedSize 已下载大小
completedSize *int64
// threadCount 协程数量
threadCount int
// breakpointResume 是否可以断点续传
breakpointResume bool
// multithread 是否支持多线程
multithread bool
// outfile 文件指针
outfile *os.File
// breakpoint 断点
breakpoint *Breakpoint
// event 进度事件
event []ProgressEvent
// eventExend 进度事件扩展
eventExend []ProgressEventExtend
// sendEvent 事件发送
sendEvent func()
// rate 限速器
rate *rate.Limiter
// isclose 是否执行了 close
isclose bool
// mux 锁
mux sync.Mutex
// err 运行时产生的错误
err error
// done 完成下载的通道通知
done chan error
}
// start 开始下载
func (ctl *control) start(ctx context.Context) (err error) {
// 已经下载完成
if ctl.status.Is(STATUS_FINISH) {
return errors.New("status is finish")
}
// 已经在运行中
if ctl.status.Is(STATUS_BEGIN, STATUS_RUNNING) {
return errors.New("status is rinning")
}
// 启动已经关闭的下载
if ctl.status.Is(STATUS_CLOSE, STATUS_ERROR) {
ctl.log("reuse download: ", ctl.uri)
return ctl.reuse(ctx)
}
// 竞争首次启动
ctl.mux.Lock()
if ctl.status != STATUS_NOTSTART {
ctl.mux.Unlock()
return errors.New("status not is STATUS_NOTSTART")
}
ctl.setStatus(STATUS_BEGIN)
ctl.mux.Unlock()
ctl.log("new download: ", ctl.uri)
err = ctl.Init(ctx)
if err != nil {
ctl.setStatus(STATUS_NOTSTART)
return err
}
go ctl.startTask()
return nil
}
// reuse 复用操作
func (ctl *control) reuse(ctx context.Context) (err error) {
ctl.packContext(ctx)
ctl.done = make(chan error, 1)
ctl.isclose = false
ctl.err = nil
// 打开文件
ctl.outfile, err = os.OpenFile(ctl.outpath, os.O_CREATE|os.O_WRONLY, ctl.perm)
if err != nil {
return err
}
// 加载事件
ctl.loadEvent()
go ctl.startTask()
return nil
}
// packContext 包装上下文
func (ctl *control) packContext(ctx context.Context) {
if ctl.config.Timeout > 0 {
ctl.ctx, ctl.cancel = context.WithTimeout(ctx, ctl.config.Timeout)
} else {
ctl.ctx, ctl.cancel = context.WithCancel(ctx)
}
ctl.request.ctx = ctl.ctx
}
// Init 初始化
func (ctl *control) Init(ctx context.Context) error {
// 包装上下文
ctl.packContext(ctx)
// 资源基本信息
resInfo, err := ctl.request.getResourceInfo()
if err != nil {
return err
}
ctl.logf("resourceInfo: %#v\n", resInfo)
// 断点信息
ctl.breakpoint = &Breakpoint{
Filesize: resInfo.filesize,
Etag: resInfo.etag,
Position: 0,
Tasks: make([]*Block, 0),
}
// 设置限速器
ctl.setSpeedLimit(ctl.config.SpeedLimit)
ctl.multithread = resInfo.multithread
ctl.totalSize = resInfo.filesize
ctl.threadCount = ctl.config.RoutineCount
ctl.breakpointResume = ctl.multithread && ctl.config.BreakpointResume
// 文件夹检查
if !fileExist(ctl.outdir) {
if ctl.config.CreateDir {
err = os.MkdirAll(ctl.outdir, os.ModePerm)
if err != nil {
return err
}
} else {
return os.ErrNotExist
}
}
// 自动获取文件名
if ctl.outname == "" {
ctl.outname = resInfo.getFilename()
}
// 文件名非法字符过滤
if ctl.config.AutoFilterFilename {
ctl.outname = filterFileName(ctl.outname)
}
// 文件检查
ctl.outpath, _ = filepath.Abs(filepath.Join(ctl.outdir, ctl.outname))
ctl.bpfilepath = filepath.Join(ctl.outdir, ctl.outname+ctl.config.BreakpointExt)
isFileExist := fileExist(ctl.outpath)
isBpfileExist := fileExist(ctl.bpfilepath)
if isFileExist && (!ctl.breakpointResume || (!isBpfileExist && ctl.breakpointResume)) {
if ctl.config.AllowOverwrite {
err := os.Remove(ctl.outpath)
if err != nil {
return err
}
} else if ctl.config.AutoFileRenaming {
// 文件重命名
ctl.outpath, ctl.outname = autoFileRenaming(ctl.outdir, ctl.outname)
ctl.bpfilepath = filepath.Join(ctl.outdir, ctl.outname+ctl.config.BreakpointExt)
} else {
return os.ErrExist
}
}
// 打开文件
ctl.outfile, err = os.OpenFile(ctl.outpath, os.O_CREATE|os.O_WRONLY, ctl.perm)
if err != nil {
return err
}
// 加载事件
ctl.loadEvent()
return nil
}
// wait 等待下载
func (ctl *control) wait() <-chan error {
return ctl.done
}
// close 关闭下载
func (ctl *control) close() {
if ctl.cancel == nil {
return
}
ctl.isclose = true
ctl.cancel()
// 等待关闭
<-ctl.done
}
// getError 获取下载的错误信息
func (ctl *control) getError() error {
return ctl.err
}
// setStatus 设置下载状态
func (ctl *control) setStatus(d Status) {
ctl.status = d
}
// setSpeedLimit 设置限速
func (ctl *control) setSpeedLimit(speedLimit int) {
ctl.mux.Lock()
defer ctl.mux.Unlock()
if speedLimit > 0 {
if speedLimit < COPY_BUFFER_SIZE {
speedLimit = COPY_BUFFER_SIZE
}
ctl.rate = rate.NewLimiter(rate.Limit(speedLimit), speedLimit)
} else {
ctl.rate = nil
}
ctl.config.SpeedLimit = speedLimit
}
// rateWaitN 消费限速器
func (ctl *control) rateWaitN(n int) {
if ctl.rate == nil {
return
}
ctl.mux.Lock()
defer ctl.mux.Unlock()
err := ctl.rate.WaitN(context.Background(), n)
if err != nil {
log.Println(err)
}
}
// setDebug 设置 debug
func (ctl *control) setDebug(v bool) {
ctl.debug = v
ctl.request.debug = v
}
// log 打印调试信息
func (ctl *control) log(v ...interface{}) {
if ctl.debug {
log.Println(v...)
}
}
// logf 打印调试信息
func (ctl *control) logf(format string, v ...any) {
if ctl.debug {
log.Printf(format, v...)
}
}