This repository has been archived by the owner on Feb 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparts.go
299 lines (279 loc) · 5.34 KB
/
parts.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
295
296
297
298
299
package warplib
import (
"crypto/rand"
"encoding/hex"
"errors"
"io"
"log"
"net/http"
"os"
"strconv"
"strings"
"sync"
"time"
)
type Part struct {
// URL
url string
// size of a bytes chunk to be used for copying
chunk int
// unique hash for this part
hash string
// number of bytes downloaded
read int64
// download progress handler
pfunc DownloadProgressHandlerFunc
// download complete handler
ofunc DownloadCompleteHandlerFunc
// compile progress handler
cfunc CompileProgressHandlerFunc
// http client
client *http.Client
// prename
preName string
// part file
pf *os.File
// offset of part
offset int64
// expected speed
etime time.Duration
// logger
l *log.Logger
wg *sync.WaitGroup
// main download file
f *os.File
}
type partArgs struct {
copyChunk int
preName string
rpHandler ResumeProgressHandlerFunc
pHandler DownloadProgressHandlerFunc
oHandler DownloadCompleteHandlerFunc
cpHandler CompileProgressHandlerFunc
logger *log.Logger
offset int64
f *os.File
}
func initPart(wg *sync.WaitGroup, client *http.Client, hash, url string, args partArgs) (*Part, error) {
p := Part{
url: url,
client: client,
chunk: args.copyChunk,
preName: args.preName,
pfunc: args.pHandler,
ofunc: args.oHandler,
cfunc: args.cpHandler,
l: args.logger,
offset: args.offset,
hash: hash,
wg: wg,
f: args.f,
}
err := p.openPartFile()
if err != nil {
return nil, err
}
err = p.seek(args.rpHandler)
if err != nil {
return nil, err
}
return &p, nil
}
func newPart(wg *sync.WaitGroup, client *http.Client, url string, args partArgs) (*Part, error) {
p := Part{
url: url,
client: client,
chunk: args.copyChunk,
preName: args.preName,
pfunc: args.pHandler,
ofunc: args.oHandler,
cfunc: args.cpHandler,
l: args.logger,
offset: args.offset,
wg: wg,
f: args.f,
}
p.setHash()
return &p, p.createPartFile()
}
func (p *Part) setEpeed(espeed int64) {
p.etime = getDownloadTime(espeed, int64(p.chunk))
}
func (p *Part) download(headers Headers, ioff, foff int64, force bool) (slow bool, err error) {
req, er := http.NewRequest(http.MethodGet, p.url, nil)
if er != nil {
err = er
return
}
header := req.Header
headers.Set(header)
if foff != -1 {
setRange(header, ioff, foff)
} else {
force = true
}
resp, er := p.client.Do(req)
if er != nil {
err = er
return
}
defer resp.Body.Close()
return p.copyBuffer(resp.Body, p.pf, force)
}
func (p *Part) copyBuffer(src io.Reader, dst io.Writer, force bool) (slow bool, err error) {
var (
te time.Duration
buf = make([]byte, p.chunk)
)
var n int
for {
n++
if !force && n%10 == 0 {
te, err = getSpeed(func() error {
return p.copyBufferChunk(src, dst, buf)
})
if err != nil {
break
}
if te > p.etime {
slow = true
return
}
continue
}
err = p.copyBufferChunk(src, dst, buf)
if err != nil {
break
}
}
if err == io.EOF {
err = nil
p.log("%s: part download complete", p.hash)
p.wg.Add(1)
go func() {
p.ofunc(p.hash, p.read)
p.wg.Done()
}()
}
return
}
func (p *Part) copyBufferChunk(src io.Reader, dst io.Writer, buf []byte) (err error) {
nr, er := src.Read(buf)
if nr > 0 {
nw, ew := dst.Write(buf[0:nr])
if nw < 0 || nr < nw {
nw = 0
if ew == nil {
ew = errors.New("invalid write results")
}
}
p.read += int64(nw)
p.wg.Add(1)
go func() {
p.pfunc(p.hash, nw)
p.wg.Done()
}()
if ew != nil {
err = ew
return
}
if nr != nw {
err = io.ErrShortWrite
return
}
}
err = er
return
}
func (p *Part) compile() (read, written int64, err error) {
wg := &sync.WaitGroup{}
// take the reader to origin from end
p.pf.Seek(0, 0)
buf := make([]byte, p.chunk)
off := p.offset
for {
nr, er := p.pf.Read(buf)
read += int64(nr)
if nr > 0 {
nw, ew := p.f.WriteAt(buf[0:nr], off)
if nw < 0 || nr < nw {
nw = 0
if ew == nil {
ew = errors.New("invalid write results")
}
}
written += int64(nw)
wg.Add(1)
go func() {
p.cfunc(p.hash, nw)
wg.Done()
}()
if ew != nil {
err = ew
break
}
if nr != nw {
err = io.ErrShortWrite
break
}
}
off += int64(nr)
if er != nil {
if er != io.EOF {
err = er
}
break
}
}
wg.Wait()
return
}
func setRange(header http.Header, ioff, foff int64) {
str := func(i int64) string {
return strconv.FormatInt(i, 10)
}
var b strings.Builder
b.WriteString("bytes=")
b.WriteString(str(ioff))
b.WriteRune('-')
if foff != 0 {
b.WriteString(str(foff))
}
header.Set("Range", b.String())
}
func (p *Part) setHash() {
t := make([]byte, 2)
rand.Read(t)
p.hash = hex.EncodeToString(t)
}
func (p *Part) createPartFile() (err error) {
p.pf, err = os.Create(p.getFileName())
return
}
func (p *Part) openPartFile() (err error) {
p.pf, err = os.OpenFile(p.getFileName(), os.O_RDWR, 0666)
return
}
func (p *Part) seek(rpFunc ResumeProgressHandlerFunc) (err error) {
pReader := NewProxyReader(p.pf, func(n int) {
rpFunc(p.hash, n)
})
n, err := io.Copy(io.Discard, pReader)
if err != nil {
return
}
p.read = n
return
}
func (p *Part) getFileName() string {
return getFileName(p.preName, p.hash)
}
func (p *Part) close() error {
return p.pf.Close()
}
func (p *Part) log(s string, a ...any) {
p.l.Printf(s+"\n", a...)
}
func (p *Part) String() string {
return p.hash
}