record.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. package http
  2. import (
  3. "bytes"
  4. "io"
  5. "net/http"
  6. "strconv"
  7. "go-common/app/interface/main/upload/model"
  8. "go-common/library/ecode"
  9. "go-common/library/log"
  10. bm "go-common/library/net/http/blademaster"
  11. "go-common/library/net/http/blademaster/binding"
  12. "go-common/library/net/http/blademaster/render"
  13. )
  14. const (
  15. _defaultDistance = 1
  16. )
  17. // genImageUpload .
  18. func genImageUpload(c *bm.Context) {
  19. params := c.Request.Form
  20. uploadKey := params.Get("upload_key")
  21. wmKey := params.Get("wm_key")
  22. wmText := params.Get("wm_text")
  23. if len(wmText) > 20 {
  24. c.JSON(nil, ecode.RequestErr)
  25. return
  26. }
  27. distance, err := strconv.Atoi(params.Get("distance"))
  28. if err != nil {
  29. distance = _defaultDistance
  30. }
  31. vertical, err := strconv.ParseBool(params.Get("wm_vertical"))
  32. if err != nil {
  33. vertical = true
  34. }
  35. c.JSON(uploadSvr.GenImageUpload(c, uploadKey, wmKey, wmText, distance, vertical))
  36. }
  37. // uploadImagePublic .
  38. func uploadImagePublic(c *bm.Context) {
  39. if err := c.Request.ParseMultipartForm(model.MaxUploadSize); err != nil {
  40. c.JSON(nil, ecode.BfsUploadFileTooLarge)
  41. return
  42. }
  43. params := c.Request.Form
  44. uploadKey := params.Get("upload_key")
  45. uploadToken := params.Get("upload_token")
  46. file, header, err := c.Request.FormFile("file")
  47. if err != nil {
  48. log.Error("upload.UploadImage.file.illegal,err:(%v)", err.Error())
  49. c.JSON(nil, ecode.RequestErr)
  50. return
  51. }
  52. defer file.Close()
  53. buf := new(bytes.Buffer)
  54. if _, err = io.Copy(buf, file); err != nil {
  55. c.JSON(nil, ecode.RequestErr)
  56. return
  57. }
  58. result, err := uploadSvr.Upload(c, uploadKey, uploadToken, header.Header.Get("Content-Type"), buf.Bytes())
  59. if err != nil {
  60. c.JSON(nil, err)
  61. return
  62. }
  63. c.JSON(result, nil)
  64. }
  65. // internalUpload upload by key and sign.
  66. func internalUpload(c *bm.Context) {
  67. var (
  68. err error
  69. mid int64
  70. )
  71. up := new(model.UploadParam)
  72. if err = c.BindWith(up, binding.FormMultipart); err != nil {
  73. return
  74. }
  75. up.WMInit()
  76. if midInter, ok := c.Get("mid"); ok {
  77. mid = midInter.(int64)
  78. }
  79. file, _, err := c.Request.FormFile("file")
  80. if err != nil {
  81. log.Error("upload.UploadImage.file.illegal,err::%v", err.Error())
  82. c.JSON(nil, ecode.FileNotExists)
  83. return
  84. }
  85. defer file.Close()
  86. buf := new(bytes.Buffer)
  87. if _, err = io.Copy(buf, file); err != nil {
  88. c.JSON(nil, ecode.RequestErr)
  89. return
  90. }
  91. c.JSON(uploadSvr.UploadRecord(c, model.UploadInternal, mid, up, buf.Bytes()))
  92. }
  93. // internalUploadImage .
  94. func internalUploadImage(c *bm.Context) {
  95. var (
  96. err error
  97. mid int64
  98. )
  99. up := new(model.UploadParam)
  100. if err = c.BindWith(up, binding.FormMultipart); err != nil {
  101. return
  102. }
  103. up.WMInit()
  104. if midInter, ok := c.Get("mid"); ok {
  105. mid = midInter.(int64)
  106. }
  107. file, _, err := c.Request.FormFile("file")
  108. if err != nil {
  109. log.Error("upload.UploadImage.file.illegal,err::%v", err.Error())
  110. c.JSON(nil, ecode.FileNotExists)
  111. return
  112. }
  113. defer file.Close()
  114. buf := new(bytes.Buffer)
  115. if _, err = io.Copy(buf, file); err != nil {
  116. c.JSON(nil, ecode.RequestErr)
  117. return
  118. }
  119. result, err := uploadSvr.UploadRecord(c, model.UploadInternal, mid, up, buf.Bytes())
  120. if err != nil {
  121. log.Error("uploadSvr.UploadRecord(%d,%+v) error(%v)", mid, up, err)
  122. c.JSON(nil, err)
  123. return
  124. }
  125. c.JSON(result, nil)
  126. }
  127. // internalUploadAdminImage .
  128. func internalUploadAdminImage(c *bm.Context) {
  129. var err error
  130. up := new(model.UploadParam)
  131. if err = c.BindWith(up, binding.FormMultipart); err != nil {
  132. return
  133. }
  134. up.WMInit()
  135. file, _, err := c.Request.FormFile("file")
  136. if err != nil {
  137. log.Error("upload.UploadImage.file.illegal,err::%v", err.Error())
  138. c.JSON(nil, ecode.RequestErr)
  139. return
  140. }
  141. defer file.Close()
  142. buf := new(bytes.Buffer)
  143. if _, err = io.Copy(buf, file); err != nil {
  144. c.JSON(nil, ecode.RequestErr)
  145. return
  146. }
  147. result, err := uploadSvr.UploadAdminRecord(c, model.UploadInternalAdmin, up, buf.Bytes())
  148. if err != nil {
  149. log.Error("uploadSrv.Upload(%v,%v,%v) error(%v)", up.Bucket, up.FileName, err)
  150. c.JSON(nil, err)
  151. return
  152. }
  153. c.JSON(result, nil)
  154. }
  155. // uploadMobileImage .
  156. func uploadMobileImage(c *bm.Context) {
  157. var (
  158. err error
  159. mid int64
  160. )
  161. up := new(model.UploadParam)
  162. if err = c.BindWith(up, binding.FormMultipart); err != nil {
  163. return
  164. }
  165. up.WMInit()
  166. if midInter, ok := c.Get("mid"); ok {
  167. mid = midInter.(int64)
  168. }
  169. if mid <= 0 {
  170. c.Render(http.StatusOK, render.JSON{
  171. Code: ecode.UserNotExist.Code(),
  172. Message: "invalid or not exist mid",
  173. Data: nil,
  174. })
  175. return
  176. }
  177. file, _, err := c.Request.FormFile("file")
  178. if err != nil {
  179. log.Error("upload.UploadMobileImage.file.illegal,err::%v", err.Error())
  180. c.JSON(nil, ecode.FileNotExists)
  181. return
  182. }
  183. defer file.Close()
  184. buf := new(bytes.Buffer)
  185. if _, err = io.Copy(buf, file); err != nil {
  186. c.JSON(nil, ecode.RequestErr)
  187. return
  188. }
  189. result, err := uploadSvr.UploadRecord(c, model.UploadApp, mid, up, buf.Bytes())
  190. if err != nil {
  191. log.Error("uploadSrv.UploadRecord(%v,%v,%v) error(%v)", mid, up.Bucket, up.FileName, err)
  192. c.JSON(nil, err)
  193. return
  194. }
  195. log.Info("app/upload param (%+v) result (%+v)", up, result)
  196. c.JSON(result, nil)
  197. }
  198. // uploadWebImage .
  199. func uploadWebImage(c *bm.Context) {
  200. var (
  201. err error
  202. mid int64
  203. )
  204. up := new(model.UploadParam)
  205. if err = c.BindWith(up, binding.FormMultipart); err != nil {
  206. return
  207. }
  208. up.WMInit()
  209. if midInter, ok := c.Get("mid"); ok {
  210. mid = midInter.(int64)
  211. }
  212. if mid <= 0 {
  213. c.Render(http.StatusOK, render.JSON{
  214. Code: ecode.UserNotExist.Code(),
  215. Message: "invalid or not exist mid",
  216. Data: nil,
  217. })
  218. return
  219. }
  220. file, _, err := c.Request.FormFile("file")
  221. if err != nil {
  222. log.Error("upload.UploadWebImage.file.illegal,err::%v", err.Error())
  223. c.JSON(nil, ecode.FileNotExists)
  224. return
  225. }
  226. defer file.Close()
  227. buf := new(bytes.Buffer)
  228. if _, err = io.Copy(buf, file); err != nil {
  229. c.JSON(nil, ecode.RequestErr)
  230. return
  231. }
  232. result, err := uploadSvr.UploadRecord(c, model.UploadWeb, mid, up, buf.Bytes())
  233. if err != nil {
  234. log.Error("uploadSrv.UploadRecord(%v,%v,%v) error(%v)", mid, up.Bucket, up.FileName, err)
  235. c.JSON(nil, err)
  236. return
  237. }
  238. log.Info("web/upload param (%+v) result (%+v)", up, result)
  239. c.JSON(result, nil)
  240. }