forked from all_user/stat_center
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
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package proxy
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
)
|
||||
|
||||
func ExamplePutObject() {
|
||||
file, err := os.OpenFile("/Users/mac/Desktop/abc.jpeg", os.O_RDONLY, 0655)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
filestat, err := file.Stat()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
path, err := PutObject(context.TODO(), "http://img.bbaazz.me/file/upload", filestat.Name(), file)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
log.Println("path", path, "err", err)
|
||||
|
||||
// Output:
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package proxy
|
||||
|
||||
type objreq struct {
|
||||
Name string `json:"fileName"`
|
||||
Data string `json:"fileData"`
|
||||
}
|
||||
|
||||
type Obj struct {
|
||||
Domain string `json:"domain"`
|
||||
Name string `json:"fileName"`
|
||||
}
|
||||
|
||||
type objresp struct {
|
||||
Code int `json:"code"`
|
||||
Object Obj `json:"data"`
|
||||
Msg string `json:"msg"`
|
||||
}
|
||||
Reference in New Issue
Block a user