-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker.go
More file actions
93 lines (80 loc) · 1.69 KB
/
worker.go
File metadata and controls
93 lines (80 loc) · 1.69 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
package quickdown
import (
"errors"
"fmt"
"io"
"net/http"
"os"
"sync"
)
type worker struct {
id int
url string
filename string
min, max int
downloaded, total int
done bool
wg *sync.WaitGroup
}
func newWorker(id int, url string, min, max int, wg *sync.WaitGroup, filename string) *worker {
return &worker{
id: id,
url: url,
min: min,
max: max,
total: max - min,
wg: wg,
filename: filename,
}
}
func (self *worker) run() {
if self.done {
panic(errors.New(fmt.Sprintf("worker(id=%d, url=%s) already done.", self.id, self.url)))
}
var tmpfile *os.File
fInfo, err := os.Stat(self.filename)
if err == nil {
fSize := int(fInfo.Size())
self.min += fSize
self.downloaded = fSize
if fSize >= self.total {
self.setDone()
return
}
tmpfile, err = os.OpenFile(self.filename, os.O_APPEND|os.O_WRONLY, 0600)
} else {
tmpfile, _ = os.Create(self.filename)
}
defer tmpfile.Close()
request, _ := http.NewRequest("GET", self.url, nil)
request.Header.Add("User-Agent", "github.com/xlzd/godown")
if self.min >= 0 && self.max > 0 {
request.Header.Add("Range", fmt.Sprintf("bytes=%d-%d", self.min, self.max))
}
client := &http.Client{}
response, err := client.Do(request)
if err != nil {
panic(err)
}
defer response.Body.Close()
for i := 0; ; i++ {
buf := make([]byte, 4096*16)
n, err := response.Body.Read(buf)
if n != 0 {
self.downloaded += n
tmpfile.Write(buf[:n])
}
if err != nil {
if err == io.EOF {
break
}
panic(err)
}
}
tmpfile.Sync()
self.setDone()
}
func (self *worker) setDone() {
self.done = true
self.wg.Done()
}