-
Notifications
You must be signed in to change notification settings - Fork 1
/
upload.go
121 lines (94 loc) · 3.18 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
package baidupcs
import (
"fmt"
"github.com/json-iterator/go"
"net/http/cookiejar"
)
// RapidUpload 秒传文件
func (p *PCSApi) RapidUpload(targetPath, contentMD5, sliceMD5, crc32 string, length int64) (err error) {
operation := "秒传文件"
if targetPath == "/" || p.Isdir(targetPath) {
return fmt.Errorf("%s 遇到错误, 保存路径不能是目录", operation)
}
p.setAPI("file", "rapidupload", map[string]string{
"path": targetPath, // 上传文件的全路径名
"content-length": fmt.Sprint(length), // 待秒传的文件长度
"content-md5": contentMD5, // 待秒传的文件的MD5
"slice-md5": sliceMD5, // 待秒传的文件的MD5
"content-crc32": crc32, // 待秒传文件CRC32
"ondup": "overwrite", // overwrite: 表示覆盖同名文件; newcopy: 表示生成文件副本并进行重命名,命名规则为“文件名_日期.后缀”
})
resp, err := p.client.Req("POST", p.url.String(), nil, nil)
if err != nil {
return
}
defer resp.Body.Close()
errInfo := NewErrorInfo(operation)
d := jsoniter.NewDecoder(resp.Body)
err = d.Decode(errInfo)
if err != nil {
return fmt.Errorf("%s, json 数据解析失败, %s", operation, err)
}
switch errInfo.ErrCode {
case 31079:
// file md5 not found, you should use upload api to upload the whole file.
}
if errInfo.ErrCode != 0 {
return errInfo
}
return nil
}
// Upload 上传单个文件
func (p *PCSApi) Upload(targetPath string, uploadFunc func(uploadURL string, jar *cookiejar.Jar) error) (err error) {
if targetPath == "/" || p.Isdir(targetPath) {
return fmt.Errorf("上传文件 遇到错误, 保存路径不能是目录")
}
p.setAPI("file", "upload", map[string]string{
"path": targetPath,
"ondup": "overwrite",
})
return uploadFunc(p.url.String(), p.client.Jar.(*cookiejar.Jar))
}
// UploadTmpFile 分片上传—文件分片及上传
func (p *PCSApi) UploadTmpFile(targetPath string, uploadFunc func(uploadURL string, jar *cookiejar.Jar) error) (err error) {
p.setAPI("file", "upload", map[string]string{
"type": "tmpfile",
})
return uploadFunc(p.url.String(), p.client.Jar.(*cookiejar.Jar))
}
// UploadCreateSuperFile 分片上传—合并分片文件
func (p *PCSApi) UploadCreateSuperFile(targetPath string, blockList ...string) (err error) {
operation := "分片上传—合并分片文件"
if targetPath == "/" || p.Isdir(targetPath) {
return fmt.Errorf("%s 遇到错误, 保存路径不能是目录", operation)
}
bl := struct {
BlockList []string `json:"block_list"`
}{
BlockList: blockList,
}
data, err := jsoniter.Marshal(&bl)
if err != nil {
panic(operation + " 发生错误, " + err.Error())
}
p.setAPI("file", "createsuperfile", map[string]string{
"path": targetPath,
"param": string(data),
"ondup": "overwrite",
})
resp, err := p.client.Req("POST", p.url.String(), nil, nil)
if err != nil {
return err
}
defer resp.Body.Close()
errInfo := NewErrorInfo(operation)
d := jsoniter.NewDecoder(resp.Body)
err = d.Decode(errInfo)
if err != nil {
return fmt.Errorf("%s, json 数据解析失败, %s", operation, err)
}
if errInfo.ErrCode != 0 {
return errInfo
}
return nil
}