task_qa_video.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package archive
  2. import (
  3. "bytes"
  4. "context"
  5. "crypto/md5"
  6. "encoding/hex"
  7. "fmt"
  8. "net/http"
  9. "net/url"
  10. "strconv"
  11. "strings"
  12. "time"
  13. "go-common/library/log"
  14. )
  15. //SendQAVideoAdd http request to add qa video task
  16. func (d *Dao) SendQAVideoAdd(c context.Context, task []byte) (err error) {
  17. ctx, cancel := context.WithTimeout(c, time.Millisecond*500)
  18. defer cancel()
  19. res := new(struct {
  20. Code int `json:"code"`
  21. Data int64 `json:"data"`
  22. })
  23. val := url.Values{}
  24. val.Set("appkey", d.c.HTTPClient.Write.App.Key)
  25. val.Set("ts", strconv.FormatInt(time.Now().Unix(), 10))
  26. val.Set("sign", sign(val, d.c.HTTPClient.Write.App.Key, d.c.HTTPClient.Write.App.Secret, true))
  27. host := fmt.Sprintf("%s?%s", d.addQAVideoURL, val.Encode())
  28. req, err := http.NewRequest(http.MethodPost, host, bytes.NewBuffer(task))
  29. if err != nil {
  30. log.Error("SendQAVideoAdd http.NewRequest error(%v), params(%s)", err, string(task))
  31. return
  32. }
  33. req = req.WithContext(ctx)
  34. req.Header.Set("Content-Type", "application/json;charset=UTF-8")
  35. if err = d.clientW.Do(c, req, res); err != nil {
  36. log.Error("SendQAVideoAdd d.clientW.Do error(%v)", err)
  37. return
  38. }
  39. if res == nil || res.Code != 0 {
  40. log.Error("SendQAVideoAdd request failed, response(%+v)", res)
  41. return
  42. }
  43. return
  44. }
  45. // sign is used to sign form params by given condition.
  46. func sign(params url.Values, appkey string, secret string, lower bool) (hexdigest string) {
  47. data := params.Encode()
  48. if strings.IndexByte(data, '+') > -1 {
  49. data = strings.Replace(data, "+", "%20", -1)
  50. }
  51. if lower {
  52. data = strings.ToLower(data)
  53. }
  54. digest := md5.Sum([]byte(data + secret))
  55. hexdigest = hex.EncodeToString(digest[:])
  56. return
  57. }