upload.go 911 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package http
  2. import (
  3. "bytes"
  4. "io/ioutil"
  5. "net/http"
  6. "time"
  7. "go-common/library/log"
  8. bm "go-common/library/net/http/blademaster"
  9. )
  10. func upload(c *bm.Context) {
  11. imageFile, _, err := c.Request.FormFile("file")
  12. if err != nil {
  13. log.Error("upload err(%v)", err)
  14. httpCode(c, err)
  15. return
  16. }
  17. defer imageFile.Close()
  18. bs, err := ioutil.ReadAll(imageFile)
  19. if err != nil {
  20. log.Error("ioutil.ReadAll err(%v)", err)
  21. httpCode(c, err)
  22. return
  23. }
  24. filetype := http.DetectContentType(bs)
  25. // var extension string
  26. switch filetype {
  27. case "image/jpeg", "image/jpg", "image/gif", "image/png", "application/pdf":
  28. default:
  29. log.Warn("unknown filetype(%s) ", filetype)
  30. return
  31. }
  32. //重新格式化文件名
  33. local, err := svc.Upload(c, "", filetype, time.Now().Unix(), bytes.NewReader(bs))
  34. if err != nil {
  35. log.Error("svc.Upload error(%v)", err)
  36. httpCode(c, err)
  37. return
  38. }
  39. httpData(c, local, nil)
  40. }