upload.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package service
  2. import (
  3. interface_video_v1 "go-common/app/interface/bbq/app-bbq/api/http/v1"
  4. "go-common/app/interface/bbq/app-bbq/conf"
  5. "go-common/app/interface/bbq/app-bbq/model"
  6. service_video_v1 "go-common/app/service/bbq/video/api/grpc/v1"
  7. "go-common/library/ecode"
  8. "go-common/library/log"
  9. bm "go-common/library/net/http/blademaster"
  10. "strconv"
  11. "time"
  12. )
  13. const (
  14. //FILESIZE .
  15. FILESIZE = 2 * 1024 * 1024
  16. )
  17. //Upload ...
  18. func (s *Service) Upload(c *bm.Context, mid int64, t int) (location string, err error) {
  19. var (
  20. fileName string
  21. filePath string
  22. file []byte
  23. )
  24. c.Request.ParseMultipartForm(32 << 20)
  25. f, h, err := c.Request.FormFile("file")
  26. //参数判断
  27. if err != nil {
  28. //log.Errorv(c, log.KV("event", "service/upload/http/FormFile"), log.KV("err", err))
  29. err = ecode.FileNotExists
  30. return
  31. }
  32. defer f.Close()
  33. //文件大小
  34. if h.Size > FILESIZE {
  35. err = ecode.FileTooLarge
  36. return
  37. }
  38. tmp := make([]byte, h.Size)
  39. if _, err = f.Read(tmp); err != nil {
  40. log.Errorv(c, log.KV("event", "service/upload/ioreader/read"), log.KV("err", err))
  41. err = ecode.ServerErr
  42. }
  43. file = tmp
  44. fileName = strconv.FormatInt(mid, 10) + "_" + strconv.FormatInt(time.Now().Unix(), 10)
  45. filePath = "userface"
  46. switch t {
  47. case 1:
  48. filePath = "home_img"
  49. }
  50. if location, err = s.dao.Upload(c, fileName, filePath, file); err != nil {
  51. log.Errorv(c, log.KV("event", "service/upload/dao/upload"), log.KV("err", err))
  52. err = ecode.ServerErr
  53. }
  54. return
  55. }
  56. //PreUpload ..
  57. func (s *Service) PreUpload(c *bm.Context, req *interface_video_v1.PreUploadRequest, mid int64) (rep *service_video_v1.PreUploadResponse, err error) {
  58. p := &service_video_v1.PreUploadRequest{
  59. Title: req.Title,
  60. Mid: mid,
  61. From: model.FromBBQ,
  62. FileExt: req.FileExt,
  63. Entension: req.Extension,
  64. }
  65. if rep, err = s.videoClient.PreUpload(c, p); err != nil {
  66. log.Errorw(c, "event", "s.videoClient.PreUpload err", "err", err)
  67. return
  68. }
  69. return
  70. }
  71. //CallBack ..
  72. func (s *Service) CallBack(c *bm.Context, req *interface_video_v1.CallBackRequest, mid int64) (rep struct{}, err error) {
  73. p := &service_video_v1.CallBackRequest{
  74. Svid: req.Svid,
  75. Mid: mid,
  76. }
  77. if _, err = s.videoClient.CallBack(c, p); err != nil {
  78. log.Errorw(c, "event", "s.videoClient.CallBack", "err", err)
  79. return
  80. }
  81. s.dao.MergeUploadReq(c, req.URL, req.UploadID, req.Profile, req.Svid, req.Auth)
  82. return
  83. }
  84. // VideoUploadCheck 创作中心白名单(估计用不到)
  85. func (s *Service) VideoUploadCheck(c *bm.Context, mid int64) (response interface{}, err error) {
  86. isAllow := true
  87. msg := ""
  88. for _, v := range conf.Filter.MidFilter.White {
  89. if v == mid {
  90. isAllow = true
  91. }
  92. }
  93. for _, v := range conf.Filter.MidFilter.Black {
  94. if v == mid {
  95. isAllow = false
  96. msg = "您不在邀请名单之内,请期待我们的邀请"
  97. }
  98. }
  99. return &interface_video_v1.UploadCheckResponse{
  100. Msg: msg,
  101. IsAllow: isAllow,
  102. }, nil
  103. }
  104. //HomeImg ..
  105. func (s *Service) HomeImg(c *bm.Context, req *interface_video_v1.HomeImgRequest, mid int64) (rep struct{}, err error) {
  106. p := &service_video_v1.HomeImgCreateRequest{
  107. Svid: req.SVID,
  108. Url: req.URL,
  109. Width: req.Width,
  110. Mid: mid,
  111. Height: req.Height,
  112. }
  113. if _, err = s.videoClient.HomeImgCreate(c, p); err != nil {
  114. return
  115. }
  116. return
  117. }