This repository has been archived by the owner on Feb 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 110
/
resume_base.go
187 lines (154 loc) · 4.44 KB
/
resume_base.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
package storage
import (
"bytes"
"context"
"encoding/base64"
"fmt"
"hash/crc32"
"io"
"net/http"
"strconv"
"strings"
"github.com/qiniu/api.v7/client"
"github.com/qiniu/api.v7/conf"
"github.com/qiniu/api.v7/internal/log"
)
// ResumeUploader 表示一个分片上传的对象
type ResumeUploader struct {
Client *client.Client
Cfg *Config
}
// NewResumeUploader 表示构建一个新的分片上传的对象
func NewResumeUploader(cfg *Config) *ResumeUploader {
if cfg == nil {
cfg = &Config{}
}
return &ResumeUploader{
Cfg: cfg,
Client: &client.DefaultClient,
}
}
// NewResumeUploaderEx 表示构建一个新的分片上传的对象
func NewResumeUploaderEx(cfg *Config, clt *client.Client) *ResumeUploader {
if cfg == nil {
cfg = &Config{}
}
if clt == nil {
clt = &client.DefaultClient
}
return &ResumeUploader{
Client: clt,
Cfg: cfg,
}
}
// 创建块请求
func (p *ResumeUploader) Mkblk(
ctx context.Context, upToken string, upHost string, ret *BlkputRet, blockSize int, body io.Reader, size int) error {
reqUrl := upHost + "/mkblk/" + strconv.Itoa(blockSize)
headers := http.Header{}
headers.Add("Content-Type", conf.CONTENT_TYPE_OCTET)
headers.Add("Authorization", "UpToken "+upToken)
return p.Client.CallWith(ctx, ret, "POST", reqUrl, headers, body, size)
}
// 发送bput请求
func (p *ResumeUploader) Bput(
ctx context.Context, upToken string, ret *BlkputRet, body io.Reader, size int) error {
reqUrl := ret.Host + "/bput/" + ret.Ctx + "/" + strconv.FormatUint(uint64(ret.Offset), 10)
headers := http.Header{}
headers.Add("Content-Type", conf.CONTENT_TYPE_OCTET)
headers.Add("Authorization", "UpToken "+upToken)
return p.Client.CallWith(ctx, ret, "POST", reqUrl, headers, body, size)
}
// 分片上传请求
func (p *ResumeUploader) resumableBput(
ctx context.Context, upToken string, upHost string, ret *BlkputRet, f io.ReaderAt, blkIdx, blkSize int, extra *RputExtra) (err error) {
h := crc32.NewIEEE()
offbase := int64(blkIdx) << blockBits
chunkSize := extra.ChunkSize
var bodyLength int
if ret.Ctx == "" {
if chunkSize < blkSize {
bodyLength = chunkSize
} else {
bodyLength = blkSize
}
body1 := io.NewSectionReader(f, offbase, int64(bodyLength))
body := io.TeeReader(body1, h)
err = p.Mkblk(ctx, upToken, upHost, ret, blkSize, body, bodyLength)
if err != nil {
return
}
if ret.Crc32 != h.Sum32() || int(ret.Offset) != bodyLength {
err = ErrUnmatchedChecksum
return
}
extra.Notify(blkIdx, blkSize, ret)
}
for int(ret.Offset) < blkSize {
if chunkSize < blkSize-int(ret.Offset) {
bodyLength = chunkSize
} else {
bodyLength = blkSize - int(ret.Offset)
}
tryTimes := extra.TryTimes
lzRetry:
h.Reset()
body1 := io.NewSectionReader(f, offbase+int64(ret.Offset), int64(bodyLength))
body := io.TeeReader(body1, h)
err = p.Bput(ctx, upToken, ret, body, bodyLength)
if err == nil {
if ret.Crc32 == h.Sum32() {
extra.Notify(blkIdx, blkSize, ret)
continue
}
log.Warn("ResumableBlockput: invalid checksum, retry")
err = ErrUnmatchedChecksum
} else {
if ei, ok := err.(*client.ErrorInfo); ok && ei.Code == InvalidCtx {
ret.Ctx = "" // reset
log.Warn("ResumableBlockput: invalid ctx, please retry")
return
}
log.Warn("ResumableBlockput: bput failed -", err)
}
if tryTimes > 1 {
tryTimes--
log.Info("ResumableBlockput retrying ...")
goto lzRetry
}
break
}
return
}
// 创建文件请求
func (p *ResumeUploader) Mkfile(
ctx context.Context, upToken string, upHost string, ret interface{}, key string, hasKey bool, fsize int64, extra *RputExtra) (err error) {
url := upHost + "/mkfile/" + strconv.FormatInt(fsize, 10)
if extra.MimeType != "" {
url += "/mimeType/" + encode(extra.MimeType)
}
if hasKey {
url += "/key/" + encode(key)
}
for k, v := range extra.Params {
if (strings.HasPrefix(k, "x:") || strings.HasPrefix(k, "x-qn-meta-")) && v != "" {
url += fmt.Sprintf("/%s/%s", k, encode(v))
}
}
buf := make([]byte, 0, 196*len(extra.Progresses))
for _, prog := range extra.Progresses {
buf = append(buf, prog.Ctx...)
buf = append(buf, ',')
}
if len(buf) > 0 {
buf = buf[:len(buf)-1]
}
headers := http.Header{}
headers.Add("Content-Type", conf.CONTENT_TYPE_OCTET)
headers.Add("Authorization", "UpToken "+upToken)
return p.Client.CallWith(
ctx, ret, "POST", url, headers, bytes.NewReader(buf), len(buf))
}
func encode(raw string) string {
return base64.URLEncoding.EncodeToString([]byte(raw))
}