upload.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package http
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "net/http"
  6. "time"
  7. "go-common/library/ecode"
  8. "go-common/library/log"
  9. bm "go-common/library/net/http/blademaster"
  10. )
  11. func upload(c *bm.Context) {
  12. file, _, err := c.Request.FormFile("file")
  13. if err != nil {
  14. log.Error("FormFile err(%v)", err)
  15. httpCode(c, fmt.Sprintf("File Upload FormFile Error:(%v)", err), ecode.RequestErr)
  16. return
  17. }
  18. defer file.Close()
  19. content, err := ioutil.ReadAll(file)
  20. if err != nil {
  21. log.Error(" uploadFile.ReadAll error(%v)", err)
  22. httpCode(c, fmt.Sprintf("File ioutil.ReadAll Error:(%v)", err), ecode.RequestErr)
  23. return
  24. }
  25. filetype := http.DetectContentType(content)
  26. switch filetype {
  27. case "image/jpeg", "image/jpg":
  28. case "image/gif":
  29. case "image/png":
  30. case "application/zip":
  31. default:
  32. httpCode(c, fmt.Sprintf("not allow filetype(%s)", filetype), ecode.RequestErr)
  33. log.Warn("not allow filetype(%s) ", filetype)
  34. return
  35. }
  36. local, err := svc.Upload(c, "", filetype, time.Now().Unix(), content)
  37. if err != nil {
  38. log.Error("svc.Upload error(%v)", err)
  39. httpCode(c, fmt.Sprintf("svc.Upload error:(%v)", err), ecode.RequestErr)
  40. return
  41. }
  42. c.JSON(local, nil)
  43. }