upload.go 900 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package http
  2. import (
  3. "path/filepath"
  4. "go-common/app/service/main/push/conf"
  5. "go-common/library/ecode"
  6. "go-common/library/log"
  7. bm "go-common/library/net/http/blademaster"
  8. )
  9. var imgExts = map[string]bool{
  10. ".jpg": true,
  11. ".jpeg": true,
  12. ".png": true,
  13. }
  14. func upimg(ctx *bm.Context) {
  15. f, h, err := ctx.Request.FormFile("file")
  16. if err != nil {
  17. log.Error("upimg error(%v)", err)
  18. ctx.JSON(nil, err)
  19. return
  20. }
  21. defer f.Close()
  22. if h.Size > conf.Conf.Push.UpimgMaxSize {
  23. log.Error("filesize error name(%s) size(%d)", h.Filename, h.Size)
  24. ctx.JSON(nil, ecode.PushServiceFileSizeErr)
  25. return
  26. }
  27. if ok := imgExts[filepath.Ext(h.Filename)]; !ok {
  28. log.Error("file ext error name(%s)", h.Filename)
  29. ctx.JSON(nil, ecode.PushServiceFileExtErr)
  30. return
  31. }
  32. url, err := pushSrv.Upimg(ctx, f)
  33. if err != nil {
  34. ctx.JSON(nil, err)
  35. return
  36. }
  37. ctx.JSON(map[string]string{"url": url}, nil)
  38. }