-
Notifications
You must be signed in to change notification settings - Fork 1
/
upload.go
123 lines (97 loc) · 2.82 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
package baidupcs
import (
"fmt"
"github.com/json-iterator/go"
"net/http"
"net/http/cookiejar"
)
// UploadFunc 上传文件处理函数
type UploadFunc func(uploadURL string, jar *cookiejar.Jar) (resp *http.Response, err error)
// RapidUpload 秒传文件
func (pcs *BaiduPCS) RapidUpload(targetPath, contentMD5, sliceMD5, crc32 string, length int64) (pcsError Error) {
dataReadCloser, pcsError := pcs.PrepareRapidUpload(targetPath, contentMD5, sliceMD5, crc32, length)
if pcsError != nil {
return
}
defer dataReadCloser.Close()
errInfo := decodeJSONError(OperationUpload, dataReadCloser)
if errInfo == nil {
return nil
}
switch errInfo.ErrorCode() {
case 31079:
// file md5 not found, you should use upload api to upload the whole file.
}
return errInfo
}
// Upload 上传单个文件
func (pcs *BaiduPCS) Upload(targetPath string, uploadFunc UploadFunc) (pcsError Error) {
dataReadCloser, pcsError := pcs.PrepareUpload(targetPath, uploadFunc)
if pcsError != nil {
return pcsError
}
defer dataReadCloser.Close()
// 数据处理
jsonData := &struct {
*PathJSON
*ErrInfo
}{
ErrInfo: NewErrorInfo(OperationUpload),
}
d := jsoniter.NewDecoder(dataReadCloser)
err := d.Decode(jsonData)
if err != nil {
jsonData.ErrInfo.jsonError(err)
return jsonData.ErrInfo
}
if jsonData.ErrCode != 0 {
return jsonData.ErrInfo
}
if jsonData.Path == "" {
jsonData.ErrInfo.errType = ErrTypeInternalError
jsonData.ErrInfo.err = fmt.Errorf("unknown response data, file saved path not found")
return jsonData.ErrInfo
}
return nil
}
// UploadTmpFile 分片上传—文件分片及上传
func (pcs *BaiduPCS) UploadTmpFile(uploadFunc UploadFunc) (md5 string, pcsError Error) {
dataReadCloser, pcsError := pcs.PrepareUploadTmpFile(uploadFunc)
if pcsError != nil {
return "", pcsError
}
defer dataReadCloser.Close()
// 数据处理
jsonData := &struct {
MD5 string `json:"md5"`
*ErrInfo
}{
ErrInfo: NewErrorInfo(OperationUploadTmpFile),
}
d := jsoniter.NewDecoder(dataReadCloser)
err := d.Decode(jsonData)
if err != nil {
jsonData.ErrInfo.jsonError(err)
return "", jsonData.ErrInfo
}
if jsonData.ErrCode != 0 {
return "", jsonData.ErrInfo
}
// 未找到md5
if jsonData.MD5 == "" {
jsonData.ErrInfo.errType = ErrTypeInternalError
jsonData.ErrInfo.err = fmt.Errorf("unknown response data, md5 not found, error: %s", err)
return "", jsonData.ErrInfo
}
return jsonData.MD5, nil
}
// UploadCreateSuperFile 分片上传—合并分片文件
func (pcs *BaiduPCS) UploadCreateSuperFile(targetPath string, blockList ...string) (pcsError Error) {
dataReadCloser, pcsError := pcs.PrepareUploadCreateSuperFile(targetPath, blockList...)
if pcsError != nil {
return pcsError
}
defer dataReadCloser.Close()
errInfo := decodeJSONError(OperationUploadCreateSuperFile, dataReadCloser)
return errInfo
}