file.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package public
  2. import (
  3. "encoding/base64"
  4. "errors"
  5. "fmt"
  6. "io/ioutil"
  7. "strings"
  8. "github.com/gin-gonic/gin"
  9. "github.com/google/uuid"
  10. imgType "github.com/shamsher31/goimgtype"
  11. "device-manage/pkg/utils"
  12. "device-manage/tools"
  13. "device-manage/tools/app"
  14. )
  15. type FileResponse struct {
  16. Size int64 `json:"size"`
  17. Path string `json:"path"`
  18. FullPath string `json:"full_path"`
  19. Name string `json:"name"`
  20. Type string `json:"type"`
  21. }
  22. // @Summary 上传图片
  23. // @Description 获取JSON
  24. // @Tags 公共接口
  25. // @Accept multipart/form-data
  26. // @Param type query string true "type" (1:单图,2:多图, 3:base64图片)
  27. // @Param file formData file true "file"
  28. // @Success 200 {string} string "{"code": 200, "message": "添加成功"}"
  29. // @Success 200 {string} string "{"code": -1, "message": "添加失败"}"
  30. // @Router /api/v1/public/uploadFile [post]
  31. func UploadFile(c *gin.Context) {
  32. tag, _ := c.GetPostForm("type")
  33. urlPerfix := fmt.Sprintf("http://%s/", c.Request.Host)
  34. var fileResponse FileResponse
  35. if tag == "" {
  36. app.Error(c, 200, errors.New(""), "缺少标识")
  37. return
  38. } else {
  39. switch tag {
  40. case "1": // 单图
  41. files, err := c.FormFile("file")
  42. if err != nil {
  43. app.Error(c, 200, errors.New(""), "图片不能为空")
  44. return
  45. }
  46. // 上传文件至指定目录
  47. guid := uuid.New().String()
  48. singleFile := "static/uploadfile/" + guid + utils.GetExt(files.Filename)
  49. _ = c.SaveUploadedFile(files, singleFile)
  50. fileType, _ := imgType.Get(singleFile)
  51. fileResponse = FileResponse{
  52. Size: tools.GetFileSize(singleFile),
  53. Path: singleFile,
  54. FullPath: urlPerfix + singleFile,
  55. Name: files.Filename,
  56. Type: fileType,
  57. }
  58. app.OK(c, fileResponse, "上传成功")
  59. return
  60. case "2": // 多图
  61. files := c.Request.MultipartForm.File["file"]
  62. var multipartFile []FileResponse
  63. for _, f := range files {
  64. guid := uuid.New().String()
  65. multipartFileName := "static/uploadfile/" + guid + utils.GetExt(f.Filename)
  66. e := c.SaveUploadedFile(f, multipartFileName)
  67. fileType, _ := imgType.Get(multipartFileName)
  68. if e == nil {
  69. multipartFile = append(multipartFile, FileResponse{
  70. Size: tools.GetFileSize(multipartFileName),
  71. Path: multipartFileName,
  72. FullPath: urlPerfix + multipartFileName,
  73. Name: f.Filename,
  74. Type: fileType,
  75. })
  76. }
  77. }
  78. app.OK(c, multipartFile, "上传成功")
  79. return
  80. case "3": // base64
  81. files, _ := c.GetPostForm("file")
  82. file2list := strings.Split(files, ",")
  83. ddd, _ := base64.StdEncoding.DecodeString(file2list[1])
  84. guid := uuid.New().String()
  85. base64File := "static/uploadfile/" + guid + ".jpg"
  86. _ = ioutil.WriteFile(base64File, ddd, 0666)
  87. typeStr := strings.Replace(strings.Replace(file2list[0], "data:", "", -1), ";base64", "", -1)
  88. fileResponse = FileResponse{
  89. Size: tools.GetFileSize(base64File),
  90. Path: base64File,
  91. FullPath: urlPerfix + base64File,
  92. Name: "",
  93. Type: typeStr,
  94. }
  95. app.OK(c, fileResponse, "上传成功")
  96. }
  97. }
  98. }