dao.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package report
  2. import (
  3. "context"
  4. "crypto/hmac"
  5. "crypto/sha1"
  6. "encoding/base64"
  7. "fmt"
  8. "hash"
  9. "io"
  10. "net/http"
  11. "net/url"
  12. "strconv"
  13. "time"
  14. "go-common/app/interface/main/app-view/conf"
  15. "go-common/library/ecode"
  16. httpx "go-common/library/net/http/blademaster"
  17. "go-common/library/net/metadata"
  18. "github.com/pkg/errors"
  19. )
  20. const (
  21. _add = "/videoup/archive/report"
  22. _timeout = 800 * time.Millisecond
  23. _bucket = "archive"
  24. _url = "http://bfs.bilibili.co/bfs/archive/"
  25. _template = "%s\n%s\n\n%d\n"
  26. _method = "PUT"
  27. _key = "8d4e593ba7555502"
  28. _secret = "0bdbd4c7caeeddf587c3c4daec0475"
  29. )
  30. // Dao is report dao
  31. type Dao struct {
  32. client *httpx.Client
  33. bfsClient *http.Client
  34. add string
  35. }
  36. // New is appeal inital func .
  37. func New(c *conf.Config) (d *Dao) {
  38. d = &Dao{
  39. client: httpx.NewClient(c.HTTPWrite),
  40. bfsClient: http.DefaultClient,
  41. add: c.Host.Archive + _add,
  42. }
  43. return
  44. }
  45. // AddAppeal add appeal .
  46. func (d *Dao) AddReport(c context.Context, mid, aid int64, mold int, ak, reason, pics string) (err error) {
  47. ip := metadata.String(c, metadata.RemoteIP)
  48. params := url.Values{}
  49. params.Set("access_key", ak)
  50. params.Set("mid", strconv.FormatInt(mid, 10))
  51. params.Set("aid", strconv.FormatInt(aid, 10))
  52. params.Set("type", strconv.Itoa(mold))
  53. params.Set("reason", reason)
  54. params.Set("pics", pics)
  55. var res struct {
  56. Code int `json:"code"`
  57. }
  58. if err = d.client.Post(c, d.add, ip, params, &res); err != nil {
  59. return
  60. }
  61. if res.Code != ecode.OK.Code() {
  62. err = errors.Wrap(ecode.Int(res.Code), d.add+"?"+params.Encode())
  63. }
  64. return
  65. }
  66. // Upload imgage upload .
  67. func (d *Dao) Upload(c context.Context, fileType string, body io.Reader) (location string, err error) {
  68. req, err := http.NewRequest(_method, _url, body)
  69. if err != nil {
  70. return
  71. }
  72. expire := time.Now().Unix()
  73. authorization := authorize(_key, _secret, _method, _bucket, expire)
  74. req.Header.Set("Host", _url)
  75. req.Header.Add("Date", fmt.Sprint(expire))
  76. req.Header.Add("Authorization", authorization)
  77. req.Header.Add("Content-Type", fileType)
  78. // timeout
  79. c, cancel := context.WithTimeout(c, _timeout)
  80. req = req.WithContext(c)
  81. defer cancel()
  82. resp, err := d.bfsClient.Do(req)
  83. if err != nil {
  84. return
  85. }
  86. if resp.StatusCode != http.StatusOK {
  87. err = errors.Wrap(ecode.Int(resp.StatusCode), req.URL.String())
  88. return
  89. }
  90. code := resp.Header.Get("Code")
  91. if code != strconv.Itoa(http.StatusOK) {
  92. err = errors.Wrap(ecode.String(code), req.URL.String())
  93. return
  94. }
  95. location = resp.Header.Get("Location")
  96. return
  97. }
  98. // authorize returns authorization for upload file to bfs
  99. func authorize(key, secret, method, bucket string, expire int64) (authorization string) {
  100. var (
  101. content string
  102. mac hash.Hash
  103. signature string
  104. )
  105. content = fmt.Sprintf(_template, method, bucket, expire)
  106. mac = hmac.New(sha1.New, []byte(secret))
  107. mac.Write([]byte(content))
  108. signature = base64.StdEncoding.EncodeToString(mac.Sum(nil))
  109. authorization = fmt.Sprintf("%s:%s:%d", key, signature, expire)
  110. return
  111. }