transcodercallbacklogic.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package logic
  2. import (
  3. "audio_transcoder/internal/svc"
  4. "audio_transcoder/internal/types"
  5. "context"
  6. "encoding/json"
  7. "errors"
  8. "github.com/google/uuid"
  9. "regexp"
  10. "github.com/zeromicro/go-zero/core/logx"
  11. )
  12. type TranscoderCallbackLogic struct {
  13. logx.Logger
  14. ctx context.Context
  15. svcCtx *svc.ServiceContext
  16. }
  17. func NewTranscoderCallbackLogic(ctx context.Context, svcCtx *svc.ServiceContext) *TranscoderCallbackLogic {
  18. return &TranscoderCallbackLogic{
  19. Logger: logx.WithContext(ctx),
  20. ctx: ctx,
  21. svcCtx: svcCtx,
  22. }
  23. }
  24. func (l *TranscoderCallbackLogic) TranscoderCallback(req *types.CallbackRequest) (resp *types.CallbackResponse, err error) {
  25. if req.Path == "" || req.CallbackUrl == "" {
  26. err = errors.New("path or callback_url is empty")
  27. return
  28. }
  29. //校验req.Path是否符合url的正则格式
  30. var urlRegexp = regexp.MustCompile(`^(?:(http|https|ftp):\/\/)?((|[\w-]+\.)*[a-z0-9]+(:\d+)?)(?:(\/[^/?#]+)*)?(\?[^#]+)?(#.+)?$`)
  31. if !urlRegexp.MatchString(req.Path) || !urlRegexp.MatchString(req.CallbackUrl) {
  32. err = errors.New("path or callback_url is not url")
  33. return
  34. }
  35. bts, err := json.Marshal(req)
  36. if err != nil {
  37. return
  38. }
  39. err = l.svcCtx.BoltSdk.Set(uuid.NewString(), string(bts))
  40. resp = &types.CallbackResponse{}
  41. return
  42. }