upload.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package http
  2. import (
  3. "io/ioutil"
  4. "mime/multipart"
  5. "net/http"
  6. "path"
  7. "strings"
  8. "time"
  9. "bytes"
  10. "crypto/md5"
  11. "fmt"
  12. "go-common/app/admin/main/growup/conf"
  13. "go-common/app/admin/main/growup/dao"
  14. "go-common/library/ecode"
  15. "go-common/library/log"
  16. bm "go-common/library/net/http/blademaster"
  17. "go-common/library/net/http/blademaster/render"
  18. "io"
  19. "os"
  20. )
  21. // upload
  22. func upload(c *bm.Context) {
  23. var (
  24. fileTpye string
  25. file multipart.File
  26. header *multipart.FileHeader
  27. fileName string
  28. body []byte
  29. location string
  30. err error
  31. )
  32. if file, header, err = c.Request.FormFile("file"); err != nil {
  33. c.JSON(nil, ecode.RequestErr)
  34. log.Error("c.Request().FormFile(\"file\") error(%v)", err)
  35. return
  36. }
  37. defer file.Close()
  38. fileName = header.Filename
  39. fileTpye = strings.TrimPrefix(path.Ext(fileName), ".")
  40. if body, err = ioutil.ReadAll(file); err != nil {
  41. c.JSON(nil, ecode.RequestErr)
  42. log.Error("ioutil.ReadAll(c.Request().Body) error(%v)", err)
  43. return
  44. }
  45. if location, err = svr.Upload(c, "", fileTpye, time.Now(), body); err != nil {
  46. c.JSON(nil, err)
  47. return
  48. }
  49. c.Render(http.StatusOK, render.MapJSON(map[string]interface{}{
  50. "code": 0,
  51. "message": "0",
  52. "data": struct {
  53. URL string `json:"url"`
  54. }{
  55. location,
  56. },
  57. }))
  58. }
  59. const (
  60. uploadFilePath = "/data/uploadfiles/"
  61. )
  62. type localUploadResult struct {
  63. FileName string `json:"filename"`
  64. }
  65. type uploadResult struct {
  66. localUploadResult
  67. MemberCount int64 `json:"member_count"`
  68. }
  69. // upload
  70. func uploadLocal(c *bm.Context) {
  71. var (
  72. file multipart.File
  73. header *multipart.FileHeader
  74. result uploadResult
  75. err error
  76. )
  77. switch {
  78. default:
  79. if file, header, err = c.Request.FormFile("file"); err != nil {
  80. log.Error("c.Request().FormFile(\"file\") error(%v)", err)
  81. break
  82. }
  83. if header.Size >= int64(conf.Conf.Bfs.MaxFileSize) {
  84. log.Error("file is too big, filesize=%d, expected<=%d", conf.Conf.Bfs.MaxFileSize, header.Size)
  85. err = fmt.Errorf("文件过大,需要小于%dkb", conf.Conf.Bfs.MaxFileSize/1024)
  86. break
  87. }
  88. var ext = path.Ext(header.Filename)
  89. if ext != ".csv" && ext != ".txt" {
  90. log.Error("only csv or txt supported")
  91. err = fmt.Errorf("只支持csv或txt格式文件,以逗号或换行分隔")
  92. break
  93. }
  94. defer file.Close()
  95. // 创建保存文件
  96. if _, e := os.Stat(uploadFilePath); os.IsNotExist(e) {
  97. os.MkdirAll(uploadFilePath, 0777)
  98. }
  99. var h = md5.New()
  100. h.Write([]byte(header.Filename))
  101. var filenameMd5 = fmt.Sprintf("%x", h.Sum(nil))
  102. var desfilename = filenameMd5 + ext
  103. var destFile *os.File
  104. destFile, err = os.Create(uploadFilePath + desfilename)
  105. if err != nil {
  106. log.Error("Create failed: %s\n", err)
  107. break
  108. }
  109. defer destFile.Close()
  110. var membuf = bytes.NewBuffer(nil)
  111. _, err = io.Copy(membuf, file)
  112. if err != nil {
  113. log.Error("err copy file, err=%s", err)
  114. break
  115. }
  116. var midList = dao.ParseMidsFromString(membuf.String())
  117. result.MemberCount = int64(len(midList))
  118. _, err = destFile.Write(membuf.Bytes())
  119. if err != nil {
  120. log.Error("write file error, err=%s", err)
  121. break
  122. }
  123. result.FileName = desfilename
  124. }
  125. if err != nil {
  126. bmHTTPErrorWithMsg(c, err, err.Error())
  127. } else {
  128. c.JSON(&result, err)
  129. }
  130. }