mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2024-01-19 02:48:24 +00:00
refactor
This commit is contained in:
parent
6923af7280
commit
3adc3da291
|
@ -168,10 +168,18 @@ func doUploadData(data []byte, option *UploadOption) (uploadResult *UploadResult
|
||||||
}
|
}
|
||||||
|
|
||||||
// upload data
|
// upload data
|
||||||
uploadResult, err = upload_content(option.UploadUrl, func(w io.Writer) (err error) {
|
uploadResult, err = upload_content(func(w io.Writer) (err error) {
|
||||||
_, err = w.Write(encryptedData)
|
_, err = w.Write(encryptedData)
|
||||||
return
|
return
|
||||||
}, "", false, len(encryptedData), "", nil, option.Jwt)
|
}, len(encryptedData), &UploadOption{
|
||||||
|
UploadUrl: option.UploadUrl,
|
||||||
|
Filename: "",
|
||||||
|
Cipher: false,
|
||||||
|
IsInputCompressed: false,
|
||||||
|
MimeType: "",
|
||||||
|
PairMap: nil,
|
||||||
|
Jwt: option.Jwt,
|
||||||
|
})
|
||||||
if uploadResult == nil {
|
if uploadResult == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -181,10 +189,18 @@ func doUploadData(data []byte, option *UploadOption) (uploadResult *UploadResult
|
||||||
uploadResult.Size = uint32(clearDataLen)
|
uploadResult.Size = uint32(clearDataLen)
|
||||||
} else {
|
} else {
|
||||||
// upload data
|
// upload data
|
||||||
uploadResult, err = upload_content(option.UploadUrl, func(w io.Writer) (err error) {
|
uploadResult, err = upload_content(func(w io.Writer) (err error) {
|
||||||
_, err = w.Write(data)
|
_, err = w.Write(data)
|
||||||
return
|
return
|
||||||
}, option.Filename, contentIsGzipped, len(data), option.MimeType, option.PairMap, option.Jwt)
|
}, len(data), &UploadOption{
|
||||||
|
UploadUrl: option.UploadUrl,
|
||||||
|
Filename: option.Filename,
|
||||||
|
Cipher: false,
|
||||||
|
IsInputCompressed: contentIsGzipped,
|
||||||
|
MimeType: option.MimeType,
|
||||||
|
PairMap: option.PairMap,
|
||||||
|
Jwt: option.Jwt,
|
||||||
|
})
|
||||||
if uploadResult == nil {
|
if uploadResult == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -197,20 +213,20 @@ func doUploadData(data []byte, option *UploadOption) (uploadResult *UploadResult
|
||||||
return uploadResult, err
|
return uploadResult, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func upload_content(uploadUrl string, fillBufferFunction func(w io.Writer) error, filename string, isGzipped bool, originalDataSize int, mtype string, pairMap map[string]string, jwt security.EncodedJwt) (*UploadResult, error) {
|
func upload_content(fillBufferFunction func(w io.Writer) error, originalDataSize int, option *UploadOption) (*UploadResult, error) {
|
||||||
buf := GetBuffer()
|
buf := GetBuffer()
|
||||||
defer PutBuffer(buf)
|
defer PutBuffer(buf)
|
||||||
body_writer := multipart.NewWriter(buf)
|
body_writer := multipart.NewWriter(buf)
|
||||||
h := make(textproto.MIMEHeader)
|
h := make(textproto.MIMEHeader)
|
||||||
h.Set("Content-Disposition", fmt.Sprintf(`form-data; name="file"; filename="%s"`, fileNameEscaper.Replace(filename)))
|
h.Set("Content-Disposition", fmt.Sprintf(`form-data; name="file"; filename="%s"`, fileNameEscaper.Replace(option.Filename)))
|
||||||
h.Set("Idempotency-Key", uploadUrl)
|
h.Set("Idempotency-Key", option.UploadUrl)
|
||||||
if mtype == "" {
|
if option.MimeType == "" {
|
||||||
mtype = mime.TypeByExtension(strings.ToLower(filepath.Ext(filename)))
|
option.MimeType = mime.TypeByExtension(strings.ToLower(filepath.Ext(option.Filename)))
|
||||||
}
|
}
|
||||||
if mtype != "" {
|
if option.MimeType != "" {
|
||||||
h.Set("Content-Type", mtype)
|
h.Set("Content-Type", option.MimeType)
|
||||||
}
|
}
|
||||||
if isGzipped {
|
if option.IsInputCompressed {
|
||||||
h.Set("Content-Encoding", "gzip")
|
h.Set("Content-Encoding", "gzip")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -229,17 +245,17 @@ func upload_content(uploadUrl string, fillBufferFunction func(w io.Writer) error
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
req, postErr := http.NewRequest("POST", uploadUrl, bytes.NewReader(buf.Bytes()))
|
req, postErr := http.NewRequest("POST", option.UploadUrl, bytes.NewReader(buf.Bytes()))
|
||||||
if postErr != nil {
|
if postErr != nil {
|
||||||
glog.V(1).Infof("create upload request %s: %v", uploadUrl, postErr)
|
glog.V(1).Infof("create upload request %s: %v", option.UploadUrl, postErr)
|
||||||
return nil, fmt.Errorf("create upload request %s: %v", uploadUrl, postErr)
|
return nil, fmt.Errorf("create upload request %s: %v", option.UploadUrl, postErr)
|
||||||
}
|
}
|
||||||
req.Header.Set("Content-Type", content_type)
|
req.Header.Set("Content-Type", content_type)
|
||||||
for k, v := range pairMap {
|
for k, v := range option.PairMap {
|
||||||
req.Header.Set(k, v)
|
req.Header.Set(k, v)
|
||||||
}
|
}
|
||||||
if jwt != "" {
|
if option.Jwt != "" {
|
||||||
req.Header.Set("Authorization", "BEARER "+string(jwt))
|
req.Header.Set("Authorization", "BEARER "+string(option.Jwt))
|
||||||
}
|
}
|
||||||
// print("+")
|
// print("+")
|
||||||
resp, post_err := HttpClient.Do(req)
|
resp, post_err := HttpClient.Do(req)
|
||||||
|
@ -250,7 +266,7 @@ func upload_content(uploadUrl string, fillBufferFunction func(w io.Writer) error
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if post_err != nil {
|
if post_err != nil {
|
||||||
return nil, fmt.Errorf("upload %s %d bytes to %v: %v", filename, originalDataSize, uploadUrl, post_err)
|
return nil, fmt.Errorf("upload %s %d bytes to %v: %v", option.Filename, originalDataSize, option.UploadUrl, post_err)
|
||||||
}
|
}
|
||||||
// print("-")
|
// print("-")
|
||||||
defer util.CloseResponse(resp)
|
defer util.CloseResponse(resp)
|
||||||
|
@ -264,16 +280,16 @@ func upload_content(uploadUrl string, fillBufferFunction func(w io.Writer) error
|
||||||
|
|
||||||
resp_body, ra_err := ioutil.ReadAll(resp.Body)
|
resp_body, ra_err := ioutil.ReadAll(resp.Body)
|
||||||
if ra_err != nil {
|
if ra_err != nil {
|
||||||
return nil, fmt.Errorf("read response body %v: %v", uploadUrl, ra_err)
|
return nil, fmt.Errorf("read response body %v: %v", option.UploadUrl, ra_err)
|
||||||
}
|
}
|
||||||
|
|
||||||
unmarshal_err := json.Unmarshal(resp_body, &ret)
|
unmarshal_err := json.Unmarshal(resp_body, &ret)
|
||||||
if unmarshal_err != nil {
|
if unmarshal_err != nil {
|
||||||
glog.Errorf("unmarshal %s: %v", uploadUrl, string(resp_body))
|
glog.Errorf("unmarshal %s: %v", option.UploadUrl, string(resp_body))
|
||||||
return nil, fmt.Errorf("unmarshal %v: %v", uploadUrl, unmarshal_err)
|
return nil, fmt.Errorf("unmarshal %v: %v", option.UploadUrl, unmarshal_err)
|
||||||
}
|
}
|
||||||
if ret.Error != "" {
|
if ret.Error != "" {
|
||||||
return nil, fmt.Errorf("unmarshalled error %v: %v", uploadUrl, ret.Error)
|
return nil, fmt.Errorf("unmarshalled error %v: %v", option.UploadUrl, ret.Error)
|
||||||
}
|
}
|
||||||
ret.ETag = etag
|
ret.ETag = etag
|
||||||
ret.ContentMd5 = resp.Header.Get("Content-MD5")
|
ret.ContentMd5 = resp.Header.Get("Content-MD5")
|
||||||
|
|
Loading…
Reference in a new issue