Skip to content

Commit 37fd7a1

Browse files
committed
cache support and fixed a bug
1 parent dc31c2f commit 37fd7a1

File tree

1 file changed

+34
-17
lines changed

1 file changed

+34
-17
lines changed

main.go

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,30 @@ import (
1515
"os"
1616
"strconv"
1717
"time"
18+
"path/filepath"
1819
)
1920

20-
const VERSION = "1.0.1"
21+
const VERSION = "1.0.2"
2122

2223
var queue, redo, finish chan int
2324
var cor, size, length, timeout int
2425
var hash, dst string
25-
var verify bool
26-
var version bool
26+
var verify, version, cache bool
2727

2828
func main() {
2929
flag.IntVar(&cor, "c", 1, "coroutine num")
30-
flag.IntVar(&size, "s", 0, "buff length")
30+
flag.IntVar(&size, "s", 0, "chunk size")
3131
flag.IntVar(&size, "t", 0, "timeout")
32-
flag.StringVar(&dst, "f", "file", "file name")
32+
flag.StringVar(&dst, "f", "", "file name")
3333
flag.StringVar(&hash, "h", "sha1", "sha1 or md5 to verify the file")
3434
flag.BoolVar(&verify, "v", false, "verify file, not download")
35+
flag.BoolVar(&cache, "cache", false, "jump if cache exist, only verify the size")
3536
flag.BoolVar(&version, "version", false, "show version")
3637
flag.Parse()
3738

3839
url := os.Args[len(os.Args)-1]
3940

40-
if version {
41+
if version || url == "version" {
4142
fmt.Println("Fileload version:", VERSION)
4243
return
4344
}
@@ -63,6 +64,10 @@ func main() {
6364
return
6465
}
6566

67+
if dst==""{
68+
_,dst=filepath.Split(url)
69+
}
70+
6671
startTime := time.Now()
6772

6873
client := http.Client{}
@@ -86,20 +91,20 @@ func main() {
8691
redo = make(chan int, int(math.Floor(float64(cor)/2)))
8792
go func() {
8893
for i := 0; i < fragment; i++ {
89-
select {
90-
case j := <-redo:
91-
queue <- j
92-
default:
93-
}
9494
queue <- i
9595
}
96+
for {
97+
j := <-redo
98+
queue <- j
99+
}
96100
}()
97101
finish = make(chan int, cor)
98102
for j := 0; j < cor; j++ {
99103
go Do(request, fragment, j)
100104
}
101105
for k := 0; k < fragment; k++ {
102-
<-finish
106+
_ = <-finish
107+
//log.Printf("[%s][%d]Finished\n", "-", i)
103108
}
104109
log.Println("Start to combine files...")
105110

@@ -157,6 +162,18 @@ func Do(request *http.Request, fragment, no int) {
157162
} else {
158163
end = length - 1
159164
}
165+
166+
filename := fmt.Sprintf("tmp_%d", i)
167+
if cache {
168+
filesize := int64(end - start+1)
169+
file, err := os.Stat(filename)
170+
if err==nil && file.Size()==filesize{
171+
log.Printf("[%d][%d]Hint cached %s, size:%d\n", no, i, filename,filesize)
172+
finish<-i
173+
continue
174+
}
175+
}
176+
160177
req.Header.Set("Range", fmt.Sprintf("bytes=%d-%d", start, end))
161178
log.Printf("[%d][%d]Start download:%d-%d\n", no, i, start, end)
162179
cli := http.Client{
@@ -172,18 +189,18 @@ func Do(request *http.Request, fragment, no int) {
172189
//log.Printf("[%d]Content-Length:%s\n", i, resp.Header.Get("Content-Length"))
173190
log.Printf("[%d][%d]Content-Range:%s\n", no, i, resp.Header.Get("Content-Range"))
174191

175-
file, err := os.Create(fmt.Sprintf("tmp_%d", i))
192+
file, err := os.Create(filename)
176193
if err != nil {
177-
log.Printf("[%d][%d]ERROR|create file:%s\n", no, i, err.Error())
194+
log.Printf("[%d][%d]ERROR|create file %s:%s\n", no, i, filename, err.Error())
178195
file.Close()
179196
resp.Body.Close()
180197
redo <- i
181198
continue
182199
}
183-
log.Printf("[%d][%d]Writing to file...\n", no, i)
200+
log.Printf("[%d][%d]Writing to file %s\n", no, i, filename)
184201
n, err := io.Copy(file, resp.Body)
185202
if err != nil {
186-
log.Printf("[%d][%d]ERROR|write to file:%s\n", no, i, err.Error())
203+
log.Printf("[%d][%d]ERROR|write to file %s:%s\n", no, i, filename, err.Error())
187204
file.Close()
188205
resp.Body.Close()
189206
redo <- i
@@ -194,7 +211,7 @@ func Do(request *http.Request, fragment, no int) {
194211
file.Close()
195212
resp.Body.Close()
196213

197-
finish <- 1
214+
finish <- i
198215
}
199216
}
200217

0 commit comments

Comments
 (0)