Initial commit
This commit is contained in:
@@ -0,0 +1,96 @@
|
||||
package proxy
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"stat/common/httpUtils"
|
||||
"stat/common/utils"
|
||||
"sync"
|
||||
)
|
||||
|
||||
var (
|
||||
buffer = bytes.Buffer{}
|
||||
lock sync.Mutex
|
||||
)
|
||||
|
||||
var (
|
||||
header = map[string]string{"Content-Type": "application/json"}
|
||||
)
|
||||
|
||||
const (
|
||||
Success = 200
|
||||
)
|
||||
|
||||
// PutObject put object to proxy
|
||||
func PutObject(ctx context.Context, apiUrl, objpath string, body io.Reader) (path string, err error) {
|
||||
lock.Lock()
|
||||
defer lock.Unlock()
|
||||
|
||||
buffer.Reset()
|
||||
_, err = buffer.ReadFrom(body)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
base64data := base64.StdEncoding.EncodeToString(buffer.Bytes())
|
||||
|
||||
r := objreq{
|
||||
Name: utils.PathForInfo(objpath).FullFileName,
|
||||
Data: base64data,
|
||||
}
|
||||
|
||||
jsonBytes, err := json.Marshal(r)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
resp, err := httpUtils.Post(apiUrl, header, bytes.NewReader(jsonBytes))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
var or objresp
|
||||
err = json.Unmarshal(resp.Data, &or)
|
||||
if err != nil {
|
||||
fmt.Println(apiUrl, string(resp.Data))
|
||||
return
|
||||
}
|
||||
if or.Code != Success {
|
||||
err = errors.New(or.Msg)
|
||||
return
|
||||
}
|
||||
path = or.Object.Name
|
||||
return
|
||||
}
|
||||
|
||||
func PutBase64Object(ctx context.Context, apiUrl, objpath string, base64data string) (path string, err error) {
|
||||
r := objreq{
|
||||
Name: utils.PathForInfo(objpath).FullFileName,
|
||||
Data: base64data,
|
||||
}
|
||||
|
||||
jsonbytes, err := json.Marshal(r)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
resp, err := httpUtils.Post(apiUrl, header, bytes.NewReader(jsonbytes))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
var or objresp
|
||||
err = json.Unmarshal(resp.Data, &or)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if or.Code != Success {
|
||||
err = errors.New(or.Msg)
|
||||
return
|
||||
}
|
||||
path = or.Object.Name
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user