http.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package http
  2. import (
  3. "io/ioutil"
  4. "net/http"
  5. grpc "go-common/app/service/bbq/video-image/api/grpc/v1"
  6. "go-common/app/service/bbq/video-image/api/http/v1"
  7. "go-common/app/service/bbq/video-image/conf"
  8. "go-common/app/service/bbq/video-image/service"
  9. "go-common/library/ecode"
  10. "go-common/library/log"
  11. bm "go-common/library/net/http/blademaster"
  12. "github.com/pkg/errors"
  13. )
  14. var (
  15. srv *service.Service
  16. )
  17. // Init init
  18. func Init(c *conf.Config, s *service.Service) {
  19. srv = s
  20. engine := bm.DefaultServer(c.BM.Server)
  21. route(engine)
  22. if err := engine.Start(); err != nil {
  23. log.Error("bm Start error(%v)", err)
  24. panic(err)
  25. }
  26. }
  27. func route(e *bm.Engine) {
  28. e.Ping(ping)
  29. e.Register(register)
  30. g := e.Group("/bbq/internal/image")
  31. {
  32. g.POST("/upload", imageUpload)
  33. g.POST("/video_cover/score", videoCoverScore)
  34. g.POST("/upload/v2", upload)
  35. }
  36. }
  37. func ping(c *bm.Context) {
  38. if err := srv.Ping(c); err != nil {
  39. log.Error("ping error(%v)", err)
  40. c.AbortWithStatus(http.StatusServiceUnavailable)
  41. }
  42. }
  43. func register(c *bm.Context) {
  44. c.JSON(map[string]interface{}{}, nil)
  45. }
  46. func videoCoverScore(c *bm.Context) {
  47. args := &v1.ScoreRequest{}
  48. if err := c.Bind(args); err != nil {
  49. errors.Wrap(err, "参数验证失败")
  50. return
  51. }
  52. c.Request.ParseMultipartForm(1 << 22)
  53. f, h, err := c.Request.FormFile("file")
  54. //参数判断
  55. if err != nil {
  56. log.Errorv(c, log.KV("log", "get file error"), log.KV("err", err))
  57. err = ecode.FileNotExists
  58. return
  59. }
  60. defer f.Close()
  61. //文件大小
  62. if h.Size > (1 << 22) {
  63. log.Errorv(c, log.KV("log", "get file error"), log.KV("err", "file size too large"))
  64. err = ecode.FileTooLarge
  65. return
  66. }
  67. c.JSON(srv.VideoCoverScore(c, args.Name, f))
  68. }
  69. //internal upload
  70. func upload(c *bm.Context) {
  71. req := &grpc.ImgUploadRequest{}
  72. req.Filename = c.Request.PostFormValue("filename")
  73. req.Dir = c.Request.PostFormValue("dir")
  74. req.File = []byte(c.Request.PostFormValue("file"))
  75. c.JSON(srv.ImgUpload(c, req))
  76. }
  77. func imageUpload(c *bm.Context) {
  78. req := &grpc.ImgUploadRequest{}
  79. req.Filename = c.Request.PostFormValue("filename")
  80. req.Dir = c.Request.PostFormValue("dir")
  81. c.Request.ParseMultipartForm(1 << 22)
  82. f, h, err := c.Request.FormFile("file")
  83. //参数判断
  84. if err != nil {
  85. log.Errorv(c, log.KV("log", "get file error"), log.KV("err", err))
  86. err = ecode.FileNotExists
  87. return
  88. }
  89. defer f.Close()
  90. //文件大小
  91. if h.Size > (1 << 22) {
  92. log.Errorv(c, log.KV("log", "get file error"), log.KV("err", "file size too large"))
  93. err = ecode.FileTooLarge
  94. return
  95. }
  96. req.File, err = ioutil.ReadAll(f)
  97. // req.File = []byte(c.Request.PostFormValue("file"))
  98. //参数判断
  99. if err != nil {
  100. log.Errorv(c, log.KV("log", "get file error"), log.KV("err", err))
  101. err = ecode.FileNotExists
  102. return
  103. }
  104. c.JSON(srv.ImgUpload(c, req))
  105. }