forked from Angey40/BaiduPCS-Go
/
example.go
47 lines (40 loc) · 851 Bytes
/
example.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
package uploader
import (
"fmt"
"github.com/iikira/BaiduPCS-Go/requester"
"github.com/iikira/BaiduPCS-Go/util"
"io"
"strings"
"time"
)
func DoUpload(uploadURL string, uploadReader io.Reader) {
h := requester.NewHTTPClient()
u := NewUploader(uploadURL, uploadReader, 0, h)
exit := make(chan struct{})
exit2 := make(chan struct{})
u.OnExecute(func() {
t := time.Now()
c := u.GetStatusChan()
for {
select {
case <-exit:
return
case v := <-c:
fmt.Printf("\r%v/%v %v/s time: %s %v",
pcsutil.ConvertFileSize(v.Uploaded, 2),
pcsutil.ConvertFileSize(v.Length, 2),
pcsutil.ConvertFileSize(v.Speed, 2),
time.Since(t)/1000000*1000000,
"[UPLOADING]"+strings.Repeat(" ", 10),
)
}
}
})
u.OnFinish(func() {
exit <- struct{}{}
exit2 <- struct{}{}
})
u.Execute(nil)
<-exit2
return
}