bfs.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package http
  2. import (
  3. "io/ioutil"
  4. "mime/multipart"
  5. "net/http"
  6. "path"
  7. "strings"
  8. "time"
  9. "go-common/library/ecode"
  10. bm "go-common/library/net/http/blademaster"
  11. )
  12. // upload
  13. func upload(c *bm.Context) {
  14. var (
  15. fileTpye string
  16. file multipart.File
  17. header *multipart.FileHeader
  18. fileName string
  19. body []byte
  20. location string
  21. err error
  22. )
  23. // res := c.Result()
  24. if file, header, err = c.Request.FormFile("file"); err != nil {
  25. c.JSON(nil, ecode.RequestErr)
  26. return
  27. }
  28. defer file.Close()
  29. fileName = header.Filename
  30. fileTpye = strings.TrimPrefix(path.Ext(fileName), ".")
  31. if body, err = ioutil.ReadAll(file); err != nil {
  32. c.JSON(nil, ecode.RequestErr)
  33. return
  34. }
  35. location, err = feedbackSvr.Upload(c, "", fileTpye, time.Now(), body)
  36. c.JSON(struct {
  37. URL string `json:"url"`
  38. }{location}, err)
  39. }
  40. // uploadFile
  41. func uploadFile(c *bm.Context) {
  42. var (
  43. req = c.Request
  44. fileTpye, location string
  45. body []byte
  46. err error
  47. )
  48. if body, err = ioutil.ReadAll(req.Body); err != nil {
  49. c.JSON(nil, ecode.RequestErr)
  50. return
  51. }
  52. req.Body.Close()
  53. fileTpye = http.DetectContentType(body)
  54. location, err = feedbackSvr.Upload(c, "", fileTpye, time.Now(), body)
  55. c.JSON(struct {
  56. URL string `json:"url"`
  57. }{location}, err)
  58. }