forked from Angey40/BaiduPCS-Go
/
upload.go
217 lines (178 loc) · 4.67 KB
/
upload.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
package baidupcscmd
import (
"bufio"
"crypto/md5"
"fmt"
"github.com/bitly/go-simplejson"
"github.com/iikira/BaiduPCS-Go/requester"
"github.com/iikira/BaiduPCS-Go/uploader"
"github.com/iikira/BaiduPCS-Go/util"
"hash/crc32"
"net/http/cookiejar"
"os"
"path/filepath"
"strings"
"time"
)
const requiredSliceLen = 256 * pcsutil.KB // 256 KB
type upload struct {
dir string // 目录路径
files []string // 目录下所有子文件
}
type localPathInfo struct {
path string
file *os.File
length int64
sliceMD5 string
md5 string
crc32 string
}
func (lp *localPathInfo) check() bool {
var err error
lp.file, err = os.Open(lp.path)
if err != nil {
return false
}
info, _ := lp.file.Stat()
lp.length = info.Size()
return true
}
func (lp *localPathInfo) sum() {
bf := bufio.NewReader(lp.file)
// 获取前 256KB 文件切片的 md5
buf := make([]byte, requiredSliceLen)
lp.file.ReadAt(buf[:], requiredSliceLen)
lp.sliceMD5 = pcsutil.Md5Encrypt(buf[:])
// 获取 文件 md5
m := md5.New()
bf.WriteTo(m)
lp.md5 = fmt.Sprintf("%x", m.Sum(nil))
// reset
lp.file, _ = os.Open(lp.path)
bf.Reset(lp.file)
// 获取 文件 crc32
c := crc32.NewIEEE()
bf.WriteTo(c)
lp.crc32 = fmt.Sprint(c.Sum32())
// reset
lp.file, _ = os.Open(lp.path)
bf.Reset(nil)
}
// RunUpload 执行文件上传
func RunUpload(localPaths []string, targetPath string) {
targetPath, err := getAbsPath(targetPath)
if err != nil {
fmt.Printf("警告: 上传文件, 获取网盘路径 %s 错误, %s\n", targetPath, err)
}
switch len(localPaths) {
case 0:
fmt.Printf("本地路径为空\n")
return
}
var _localPaths []upload
for k := range localPaths {
_paths, err := filepath.Glob(localPaths[k])
if err != nil {
fmt.Printf("上传文件, 匹配本地路径失败, %s\n", err)
continue
}
for k2 := range _paths {
_files, err := pcsutil.WalkDir(_paths[k2], "")
if err != nil {
fmt.Println(err)
continue
}
_localPaths = append(_localPaths, upload{
dir: filepath.Dir(_paths[k2]),
files: _files,
})
}
}
filesTotalNum := len(_localPaths)
for ftN, uploadInfo := range _localPaths {
filesNum := len(uploadInfo.files)
for fN, file := range uploadInfo.files {
func() {
fmt.Printf("[%d/%d - %d/%d] - [%s]: 任务开始\n", ftN+1, filesTotalNum, fN+1, filesNum, file)
localPathInfo := localPathInfo{
path: file,
}
if !localPathInfo.check() {
fmt.Printf("文件不可读, 跳过...\n")
return
}
defer localPathInfo.file.Close() // 关闭文件
_targetPath := targetPath + "/" + strings.TrimPrefix(localPathInfo.path, uploadInfo.dir)
if localPathInfo.length >= requiredSliceLen {
fmt.Printf("检测秒传中, 请稍候...\n")
localPathInfo.sum()
err := info.RapidUpload(_targetPath, localPathInfo.md5, localPathInfo.sliceMD5, localPathInfo.crc32, localPathInfo.length)
if err == nil {
fmt.Printf("秒传成功\n")
return
}
fmt.Printf("秒传失败, 开始上传文件...\n")
}
err = info.Upload(_targetPath, func(uploadURL string, jar *cookiejar.Jar) (uperr error) {
h := requester.NewHTTPClient()
h.SetCookiejar(jar)
u := uploader.NewUploader(uploadURL, localPathInfo.file, -1, 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:
if v.Length == 0 {
fmt.Printf("\rPrepareing upload...")
t = time.Now()
continue
}
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(func(respBodyContents []byte, err error) {
if err != nil {
uperr = err
return
}
json, err := simplejson.NewJson(respBodyContents)
if err != nil {
uperr = fmt.Errorf("json parse error, %s", err)
return
}
pj, ok := json.CheckGet("path")
if !ok {
uperr = fmt.Errorf("unknown response data, path not found, %s", string(respBodyContents))
return
}
_targetPath = pj.MustString()
})
<-exit2
return uperr
})
fmt.Printf("\n")
if err != nil {
fmt.Printf("上传文件失败, %s\n", err)
return
}
fmt.Printf("上传文件成功, 保存位置: %s\n", _targetPath)
}()
}
}
}