upload.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package http
  2. import (
  3. "encoding/json"
  4. "io/ioutil"
  5. "mime/multipart"
  6. "time"
  7. "go-common/app/admin/main/apm/model/ut"
  8. "go-common/library/ecode"
  9. "go-common/library/log"
  10. bm "go-common/library/net/http/blademaster"
  11. )
  12. func upload(c *bm.Context) {
  13. var (
  14. file multipart.File
  15. body []byte
  16. htmlURL string
  17. reportURL string
  18. dataURL string
  19. err error
  20. pkg = new(ut.PkgAnls)
  21. files []*ut.File
  22. res = &ut.UploadRes{}
  23. header *multipart.FileHeader
  24. )
  25. c.Request.ParseMultipartForm(32 << 20)
  26. if err = c.Bind(res); err != nil {
  27. return
  28. }
  29. log.Info("ut.upload(%d) start! current_time(%d)", res.MergeID, time.Now().Unix())
  30. defer log.Info("ut.upload(%d) finished. current_time(%d)", res.MergeID, time.Now().Unix())
  31. if file, _, err = c.Request.FormFile("report_file"); err != nil {
  32. c.JSON(nil, ecode.RequestErr)
  33. log.Error("report request upload err (%v)", err)
  34. return
  35. }
  36. defer file.Close()
  37. if body, err = ioutil.ReadAll(file); err != nil {
  38. c.JSON(nil, ecode.RequestErr)
  39. log.Error("ioutil.ReadAll(c.Request().Body) error(%v)", err)
  40. return
  41. }
  42. if body, err = apmSvc.ParseContent(c, body); err != nil {
  43. c.JSON(err.Error(), err)
  44. return
  45. }
  46. if pkg, err = apmSvc.CalcCount(c, body); err != nil {
  47. c.JSON(nil, err)
  48. return
  49. }
  50. if pkg.Assertions == 0 {
  51. c.JSON("no result", nil)
  52. return
  53. }
  54. if reportURL, err = apmSvc.Upload(c, "json", time.Now().Unix(), body); err != nil {
  55. c.JSON(nil, err)
  56. return
  57. }
  58. if file, header, err = c.Request.FormFile("data_file"); err == nil && header.Size > 0 {
  59. defer file.Close()
  60. if body, err = ioutil.ReadAll(file); err != nil {
  61. c.JSON(nil, ecode.RequestErr)
  62. log.Error("Upload data request error(%v)", err)
  63. return
  64. }
  65. if files, err = apmSvc.CalcCountFiles(c, res, body); err != nil {
  66. c.JSON(nil, err)
  67. log.Error("Upload data calcCount error(%v)", err)
  68. return
  69. }
  70. if dataURL, err = apmSvc.Upload(c, "text/plain", time.Now().Unix(), body); err != nil {
  71. c.JSON(nil, err)
  72. return
  73. }
  74. }
  75. if file, _, err = c.Request.FormFile("html_file"); err != nil {
  76. c.JSON(nil, ecode.RequestErr)
  77. log.Error("html request upload err (%v)", err)
  78. return
  79. }
  80. defer file.Close()
  81. if body, err = ioutil.ReadAll(file); err != nil {
  82. c.JSON(nil, ecode.RequestErr)
  83. log.Error("html read error(%v)", err)
  84. return
  85. }
  86. if htmlURL, err = apmSvc.Upload(c, "html", time.Now().Unix(), body); err != nil {
  87. c.JSON(nil, err)
  88. return
  89. }
  90. if err = apmSvc.AddUT(c, pkg, files, res, dataURL, reportURL, htmlURL); err != nil {
  91. c.JSON(nil, err)
  92. return
  93. }
  94. // update ut_app has_ut = 1 && converage
  95. if err = apmSvc.UpdateUTApp(c, pkg); err != nil {
  96. c.JSON(nil, err)
  97. return
  98. }
  99. c.JSON(nil, nil)
  100. }
  101. // uploadApp upload path to ut_app
  102. func uploadApp(c *bm.Context) {
  103. var (
  104. file multipart.File
  105. body []byte
  106. apps []*ut.App
  107. err error
  108. )
  109. c.Request.ParseMultipartForm(32 << 20)
  110. if file, _, err = c.Request.FormFile("path_file"); err != nil {
  111. c.JSON(nil, ecode.RequestErr)
  112. log.Error("path request upload error(%v)", err)
  113. return
  114. }
  115. defer file.Close()
  116. if body, err = ioutil.ReadAll(file); err != nil {
  117. c.JSON(nil, ecode.RequestErr)
  118. log.Error("path_file read error(%v)", err)
  119. return
  120. }
  121. if err = json.Unmarshal(body, &apps); err != nil {
  122. log.Error("json.Unmarshal error(%v)", err)
  123. return
  124. }
  125. if err = apmSvc.AddUTApp(c, apps); err != nil {
  126. c.JSON(nil, ecode.RequestErr)
  127. log.Error("apmSvc.AddUtApp error(%v)", err)
  128. return
  129. }
  130. c.JSON(nil, err)
  131. }