upos.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package dao
  2. import (
  3. "bytes"
  4. "context"
  5. "encoding/json"
  6. "fmt"
  7. "go-common/library/ecode"
  8. "go-common/library/log"
  9. "net/http"
  10. )
  11. const (
  12. _uposURL = "api/v1/task/push/audiowaveform"
  13. _uposBfsFmt = "subtitle/waveform_%d_%d.pcm"
  14. _uposCallback = "x/internal/v2/dm/subtitle/upos/callback"
  15. _defaultPixelDensity = 20
  16. )
  17. // UposReq .
  18. type UposReq struct {
  19. Cid int64 `json:"cid"`
  20. SaveTo string `json:"saveto"`
  21. CallbackURL string `json:"callback_url"`
  22. PixelDensity int32 `json:"pixel_density"`
  23. }
  24. // UposResp .
  25. type UposResp struct {
  26. Code int32 `json:"code"`
  27. Message string `json:"message"`
  28. }
  29. // Upos .
  30. func (d *Dao) Upos(c context.Context, oid int64) (saveTo string, err error) {
  31. var (
  32. req *http.Request
  33. bs []byte
  34. resp []byte
  35. )
  36. saveTo = fmt.Sprintf(_uposBfsFmt, oid, 1)
  37. params := &UposReq{
  38. Cid: oid,
  39. SaveTo: fmt.Sprintf("bfs://%s", saveTo),
  40. CallbackURL: fmt.Sprintf("%s/%s?oid=%d", d.conf.Host.Self, _uposCallback, oid),
  41. PixelDensity: _defaultPixelDensity,
  42. }
  43. if bs, err = json.Marshal(&params); err != nil {
  44. log.Error("params(%+v),error(%v)", params, err)
  45. return
  46. }
  47. if req, err = http.NewRequest(http.MethodPost, fmt.Sprintf("%s/%s", d.conf.Host.Upos, _uposURL), bytes.NewReader(bs)); err != nil {
  48. log.Error("http.NewReques.error(%v)", err)
  49. return
  50. }
  51. if resp, err = d.httpCli.Raw(c, req); err != nil {
  52. log.Error("d.httpCli.Raw.error(%v)", err)
  53. return
  54. }
  55. uposResp := &UposResp{}
  56. if err = json.Unmarshal(resp, &uposResp); err != nil {
  57. log.Error("params(%s),error(%v)", resp, err)
  58. return
  59. }
  60. if uposResp.Code != 0 {
  61. err = ecode.SubtitleWaveFormFailed
  62. log.Error("d.Upos,error(%v),info(%s)", err, uposResp.Message)
  63. }
  64. return
  65. }