file.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package http
  2. import (
  3. "bytes"
  4. "io"
  5. "mime/multipart"
  6. "path"
  7. "strconv"
  8. "time"
  9. "go-common/app/admin/ep/melloi/model"
  10. "go-common/library/ecode"
  11. "go-common/library/log"
  12. bm "go-common/library/net/http/blademaster"
  13. "go-common/library/net/http/blademaster/binding"
  14. )
  15. const (
  16. _upLoadSuccess = "上传成功"
  17. _upLoadFail = "上传失败"
  18. )
  19. func upload(c *bm.Context) {
  20. uploadParam := model.UploadParam{}
  21. if err := c.BindWith(&uploadParam, binding.Form); err != nil {
  22. c.JSON(nil, err)
  23. return
  24. }
  25. formFile, header, err := c.Request.FormFile("file")
  26. if err != nil {
  27. log.Error("Get form file failed,error(%v)", err)
  28. c.JSON(nil, err)
  29. return
  30. }
  31. var upLoadResultMap = make(map[string]string)
  32. upLoadResultMap["result"] = _upLoadSuccess
  33. scriptID, scriptPath, err := srv.Upload(c, &uploadParam, formFile, header)
  34. if err != nil {
  35. log.Error("Write file failed, error(%v)", err)
  36. upLoadResultMap["result"] = _upLoadFail
  37. c.JSON(upLoadResultMap, err)
  38. return
  39. }
  40. upLoadResultMap["script_id"] = strconv.Itoa(scriptID)
  41. upLoadResultMap["script_path"] = scriptPath
  42. upLoadResultMap["full_path"] = path.Join(scriptPath, header.Filename)
  43. c.JSON(upLoadResultMap, nil)
  44. }
  45. func uploadDependProto(c *bm.Context) {
  46. var (
  47. upParam = &model.UploadParam{}
  48. header *multipart.FileHeader
  49. formFile multipart.File
  50. err error
  51. )
  52. if err = c.BindWith(upParam, binding.Form); err != nil {
  53. return
  54. }
  55. if formFile, header, err = c.Request.FormFile("file"); err != nil {
  56. c.JSON(nil, err)
  57. return
  58. }
  59. c.JSON(srv.UploadAndGetProtoInfo(c, upParam, formFile, header))
  60. }
  61. func compileProtoFile(c *bm.Context) {
  62. uploadParam := model.UploadParam{}
  63. if err := c.BindWith(&uploadParam, binding.JSON); err != nil {
  64. c.JSON(nil, err)
  65. return
  66. }
  67. fileName := strconv.FormatInt(time.Now().UTC().UnixNano(), 10)
  68. c.JSON(srv.CompileProtoFile(uploadParam.ScriptPath, fileName))
  69. }
  70. func uploadImg(c *bm.Context) {
  71. file, header, err := c.Request.FormFile("file")
  72. if err != nil {
  73. log.Error("get IMG file error (%v)", err)
  74. c.JSON(nil, err)
  75. return
  76. }
  77. defer file.Close()
  78. buff := new(bytes.Buffer)
  79. if _, err = io.Copy(buff, file); err != nil {
  80. c.JSON(nil, ecode.RequestErr)
  81. return
  82. }
  83. c.JSON(srv.UploadImg(c, buff.Bytes(), header.Filename))
  84. }
  85. func readFile(c *bm.Context) {
  86. file := c.Request.Form.Get("file")
  87. limit := c.Request.Form.Get("limit")
  88. var upLoadResultMap = make(map[string]string)
  89. data, err := srv.ReadFile(c, file, limit)
  90. if err != nil {
  91. return
  92. }
  93. upLoadResultMap["data"] = data
  94. c.JSON(upLoadResultMap, nil)
  95. }
  96. func downloadFile(c *bm.Context) {
  97. filePath := c.Request.Form.Get("file_path")
  98. c.JSON(nil, srv.DownloadFile(c, filePath, c.Writer))
  99. }
  100. func isFileExists(c *bm.Context) {
  101. fileName := c.Request.Form.Get("file_name")
  102. exists, err := srv.IsFileExists(c, fileName)
  103. var ResultMap = make(map[string]string)
  104. ResultMap["fileExists"] = strconv.FormatBool(exists)
  105. c.JSON(ResultMap, err)
  106. }