token.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package v1
  2. import (
  3. "context"
  4. "time"
  5. v1pb "go-common/app/admin/live/live-admin/api/http/v1"
  6. "go-common/app/admin/live/live-admin/conf"
  7. "go-common/app/admin/live/live-admin/dao"
  8. "go-common/library/ecode"
  9. "go-common/library/log"
  10. )
  11. // TokenService struct
  12. type TokenService struct {
  13. conf *conf.Config
  14. // optionally add other properties here, such as dao
  15. dao *dao.Dao
  16. }
  17. //NewTokenService init
  18. func NewTokenService(c *conf.Config, d *dao.Dao) (s *TokenService) {
  19. s = &TokenService{
  20. conf: c,
  21. dao: d,
  22. }
  23. return s
  24. }
  25. // New implementation
  26. // Request for a token for upload.
  27. // `method:"POST" internal:"true"`
  28. func (s *TokenService) New(ctx context.Context, req *v1pb.NewTokenReq) (resp *v1pb.NewTokenResp, err error) {
  29. // Must be live's bucket.
  30. _, ok := s.conf.Bucket[req.Bucket]
  31. if !ok {
  32. err = ecode.UploadBucketErr
  33. return
  34. }
  35. var token string
  36. if token, err = s.dao.RequestUploadToken(ctx, req.Bucket, req.Operator, time.Now().Unix()); err != nil {
  37. log.Error("New a upload token failure: %v", err)
  38. err = ecode.UploadTokenGenErr
  39. return
  40. }
  41. resp = &v1pb.NewTokenResp{
  42. Token: token,
  43. }
  44. return
  45. }