123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- package dao
- import (
- "bytes"
- "context"
- "crypto/hmac"
- "crypto/sha1"
- "encoding/base64"
- "fmt"
- "hash"
- "io/ioutil"
- "net/http"
- "strconv"
- "time"
- "go-common/app/admin/main/member/conf"
- "go-common/library/ecode"
- "github.com/pkg/errors"
- )
- // UploadImage upload bfs.
- func (d *Dao) UploadImage(c context.Context, fileType string, bs []byte, bfsConf *conf.BFS) (location string, err error) {
- url := bfsConf.URL + bfsConf.Bucket + "/"
- req, err := http.NewRequest("PUT", url, bytes.NewBuffer(bs))
- if err != nil {
- err = errors.Wrap(err, " UploadImage http.NewRequest() failed")
- return
- }
- expire := time.Now().Unix()
- authorization := authorize(bfsConf.Key, bfsConf.Secret, "PUT", bfsConf.Bucket, "", expire)
- req.Header.Set("Host", url)
- req.Header.Add("Date", fmt.Sprint(expire))
- req.Header.Add("Authorization", authorization)
- req.Header.Add("Content-Type", fileType)
- // timeout
- ctx, cancel := context.WithTimeout(c, time.Duration(bfsConf.Timeout))
- req = req.WithContext(ctx)
- defer cancel()
- resp, err := d.bfsclient.Do(req)
- if err != nil {
- err = errors.Wrapf(ecode.BfsUploadServiceUnavailable, "d.bfsclient.Do error(%v) | url(%s)", err, url)
- return
- }
- if resp.StatusCode != http.StatusOK {
- err = errors.Errorf("UploadImage failed,http code:%d", resp.StatusCode)
- return
- }
- header := resp.Header
- code := header.Get("Code")
- if code != strconv.Itoa(http.StatusOK) {
- err = errors.Wrap(ecode.String(code), "UploadImage failed")
- return
- }
- location = header.Get("Location")
- return
- }
- // DelImage del bfs image.
- func (d *Dao) DelImage(c context.Context, fileName string, bfsConf *conf.BFS) (err error) {
- url := bfsConf.URL + bfsConf.Bucket + "/" + fileName
- req, err := http.NewRequest("DELETE", url, nil)
- if err != nil {
- err = errors.Wrap(err, "DelImage http.NewRequest() failed")
- return
- }
- expire := time.Now().Unix()
- authorization := authorize(bfsConf.Key, bfsConf.Secret, "DELETE", bfsConf.Bucket, fileName, expire)
- req.Header.Set("Host", url)
- req.Header.Add("Date", fmt.Sprint(expire))
- req.Header.Add("Authorization", authorization)
- // timeout
- ctx, cancel := context.WithTimeout(c, time.Duration(bfsConf.Timeout))
- req = req.WithContext(ctx)
- defer cancel()
- resp, err := d.bfsclient.Do(req)
- if err != nil {
- err = errors.Wrapf(ecode.BfsUploadServiceUnavailable, "d.bfsclient.Do error(%v) | url(%s)", err, url)
- return
- }
- if resp.StatusCode != http.StatusOK {
- err = errors.Errorf("DelImage failed,http code:%d", resp.StatusCode)
- return
- }
- header := resp.Header
- code := header.Get("Code")
- if code != strconv.Itoa(http.StatusOK) {
- err = errors.Errorf("DelImage failed,res code:%s", code)
- return
- }
- return
- }
- // authorize returns authorization for upload file to bfs
- func authorize(key, secret, method, bucket, fileName string, expire int64) (authorization string) {
- var (
- content string
- mac hash.Hash
- signature string
- )
- content = fmt.Sprintf("%s\n%s\n%s\n%d\n", method, bucket, fileName, expire)
- mac = hmac.New(sha1.New, []byte(secret))
- mac.Write([]byte(content))
- signature = base64.StdEncoding.EncodeToString(mac.Sum(nil))
- authorization = fmt.Sprintf("%s:%s:%d", key, signature, expire)
- return
- }
- //Image image.
- func (d *Dao) Image(url string) ([]byte, error) {
- resp, err := d.bfsclient.Get(url)
- if err != nil {
- return nil, err
- }
- if resp.StatusCode != http.StatusOK {
- return nil, errors.Errorf("Image url(%s) resp.StatusCode code(%v)", url, resp.StatusCode)
- }
- defer resp.Body.Close()
- return ioutil.ReadAll(resp.Body)
- }
|