bfs.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package http
  2. import (
  3. "io/ioutil"
  4. "net/http"
  5. "go-common/library/ecode"
  6. "go-common/library/log"
  7. bm "go-common/library/net/http/blademaster"
  8. "go-common/library/net/http/blademaster/render"
  9. )
  10. func clientUpload(c *bm.Context) {
  11. var (
  12. req = c.Request
  13. md5 string
  14. url string
  15. )
  16. req.ParseMultipartForm(int64(bfsSvc.BfsMaxSize))
  17. file, _, err := req.FormFile("file")
  18. if err != nil {
  19. log.Error("c.Request().FormFile(\"file\") error(%v) | ", err)
  20. c.JSON(nil, ecode.RequestErr)
  21. return
  22. }
  23. bs, err := ioutil.ReadAll(file)
  24. file.Close()
  25. if err != nil {
  26. log.Error("ioutil.ReadAll(c.Request().Body) error(%v)", err)
  27. c.JSON(nil, ecode.RequestErr)
  28. return
  29. }
  30. if md5, err = bfsSvc.FileMd5(bs); err != nil {
  31. log.Error("bfsSvc.FileMd5 error(%v)", err)
  32. c.JSON(nil, ecode.RequestErr)
  33. return
  34. }
  35. ftype := http.DetectContentType(bs)
  36. //if model.IsCoverType(ftype) {
  37. // log.Error("filetype not allow file type(%s)", ftype)
  38. // renderErrMsg(c, ecode.RequestErr.Code(), "文件上传错误:图片类型错误")
  39. // return
  40. //}
  41. if url, err = bfsSvc.ClientUpCover(c, ftype, bs); err != nil {
  42. log.Error("bfsSvc.ClientUpCover error(%v)", err)
  43. c.JSON("文件上传错误:"+err.Error(), ecode.RequestErr)
  44. return
  45. }
  46. data := map[string]interface{}{
  47. "url": url,
  48. "md5": md5,
  49. "size": len(bs),
  50. }
  51. c.Render(http.StatusOK, render.MapJSON(data))
  52. }