creative.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package creative
  2. import (
  3. "bytes"
  4. "context"
  5. "crypto/md5"
  6. "encoding/hex"
  7. "encoding/json"
  8. "go-common/app/interface/main/videoup/conf"
  9. "go-common/app/interface/main/videoup/model/archive"
  10. "go-common/library/log"
  11. "net/http"
  12. "net/url"
  13. "strconv"
  14. "time"
  15. )
  16. const (
  17. _setWatermark = "/x/internal/creative/watermark/set"
  18. _uploadMaterial = "/x/internal/creative/upload/material"
  19. )
  20. // SetWatermark fn
  21. func (d *Dao) SetWatermark(c context.Context, mid int64, state, ty, pos int8, ip string) (err error) {
  22. params := url.Values{}
  23. params.Set("mid", strconv.FormatInt(mid, 10))
  24. params.Set("state", strconv.Itoa(int(state)))
  25. params.Set("type", strconv.Itoa(int(ty)))
  26. params.Set("position", strconv.Itoa(int(pos)))
  27. var res struct {
  28. Code int `json:"code"`
  29. }
  30. if err = d.httpW.Post(c, d.setWatermarkURL, ip, params, &res); err != nil {
  31. log.Error("d.httpW.Post(%s) error(%v)", d.setWatermarkURL+"?"+params.Encode(), err)
  32. return
  33. }
  34. log.Info("SetWatermark url(%s) code(%d)", d.setWatermarkURL+"?"+params.Encode(), res.Code)
  35. if res.Code != 0 {
  36. log.Error("url(%s) code(%d)", d.setWatermarkURL+"?"+params.Encode(), res.Code)
  37. }
  38. return
  39. }
  40. // UploadMaterial fn
  41. func (d *Dao) UploadMaterial(c context.Context, editors []*archive.Editor, aid, mid int64, ip string) (err error) {
  42. params := url.Values{}
  43. params.Set("appkey", conf.Conf.App.Key)
  44. params.Set("ts", strconv.FormatInt(time.Now().Unix(), 10))
  45. params.Set("mid", strconv.FormatInt(mid, 10))
  46. params.Set("aid", strconv.FormatInt(aid, 10))
  47. mh := md5.Sum([]byte(params.Encode() + d.c.App.Secret))
  48. params.Set("sign", hex.EncodeToString(mh[:]))
  49. var (
  50. uri = d.uploadMaterialURL + "?" + params.Encode()
  51. )
  52. bs, err := json.Marshal(editors)
  53. if err != nil {
  54. log.Error("UploadMaterial json.Marshal error(%+v)|editor(%+v)", err, editors)
  55. return
  56. }
  57. req, err := http.NewRequest("POST", uri, bytes.NewReader(bs))
  58. if err != nil {
  59. log.Error("UploadMaterial http.NewRequest error(%v) | uri(%s) bs(%+v)", err, uri, bs)
  60. return
  61. }
  62. req.Header.Set("X-BACKEND-BILI-REAL-IP", ip)
  63. var res struct {
  64. Code int `json:"code"`
  65. Message string `json:"message"`
  66. }
  67. if err = d.httpW.Do(c, req, &res); err != nil {
  68. log.Error("UploadMaterial do error(%v)|uri(%s)", err, uri)
  69. return
  70. }
  71. log.Info("UploadMaterial url(%s) code(%d)", uri, res.Code)
  72. if res.Code != 0 {
  73. log.Error("url(%s) code(%d)", uri, res.Code)
  74. }
  75. return
  76. }