file.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. package apis
  2. import (
  3. "encoding/base64"
  4. "errors"
  5. "fmt"
  6. "io/ioutil"
  7. "strings"
  8. "IotAdmin/core/sdk/api"
  9. "IotAdmin/core/sdk/pkg"
  10. "IotAdmin/core/sdk/pkg/utils"
  11. "github.com/gin-gonic/gin"
  12. "github.com/google/uuid"
  13. )
  14. type FileResponse struct {
  15. Size int64 `json:"size"`
  16. Path string `json:"path"`
  17. FullPath string `json:"full_path"`
  18. Name string `json:"name"`
  19. Type string `json:"type"`
  20. }
  21. const path = "static/uploadfile/"
  22. type File struct {
  23. api.Api
  24. }
  25. // UploadFile 上传图片
  26. // @Summary 上传图片
  27. // @Description 获取JSON
  28. // @Tags 系統接口/上传图片
  29. // @Accept multipart/form-data
  30. // @Param type query string true "type" (1:单图,2:多图, 3:base64图片)
  31. // @Param file formData file true "file"
  32. // @Success 200 {string} string "{"code": 200, "message": "添加成功"}"
  33. // @Success 200 {string} string "{"code": -1, "message": "添加失败"}"
  34. // @Router /api/sys/upload-file [post]
  35. // @Security Bearer
  36. func (e *File) UploadFile(c *gin.Context) {
  37. e.MakeContext(c)
  38. tag, _ := c.GetPostForm("type")
  39. urlPrefix := fmt.Sprintf("%s://%s/", "http", c.Request.Host)
  40. var fileResponse FileResponse
  41. switch tag {
  42. case "1": // 单图
  43. var done bool
  44. fileResponse, done = e.singleFile(c, fileResponse, urlPrefix)
  45. if done {
  46. return
  47. }
  48. e.OK(fileResponse, "上传成功")
  49. return
  50. case "2": // 多图
  51. multipartFile := e.multipleFile(c, urlPrefix)
  52. e.OK(multipartFile, "上传成功")
  53. return
  54. case "3": // base64
  55. fileResponse = e.baseImg(c, fileResponse, urlPrefix)
  56. e.OK(fileResponse, "上传成功")
  57. default:
  58. var done bool
  59. fileResponse, done = e.singleFile(c, fileResponse, urlPrefix)
  60. if done {
  61. return
  62. }
  63. e.OK(fileResponse, "上传成功")
  64. return
  65. }
  66. }
  67. func (e *File) baseImg(c *gin.Context, fileResponse FileResponse, urlPerfix string) FileResponse {
  68. files, _ := c.GetPostForm("file")
  69. file2list := strings.Split(files, ",")
  70. ddd, _ := base64.StdEncoding.DecodeString(file2list[1])
  71. guid := uuid.New().String()
  72. fileName := guid + ".jpg"
  73. err := utils.IsNotExistMkDir(path)
  74. if err != nil {
  75. e.Error(500, errors.New(""), "初始化文件路径失败")
  76. }
  77. base64File := path + fileName
  78. _ = ioutil.WriteFile(base64File, ddd, 0666)
  79. typeStr := strings.Replace(strings.Replace(file2list[0], "data:", "", -1), ";base64", "", -1)
  80. fileResponse = FileResponse{
  81. Size: pkg.GetFileSize(base64File),
  82. Path: base64File,
  83. FullPath: urlPerfix + base64File,
  84. Name: "",
  85. Type: typeStr,
  86. }
  87. source, _ := c.GetPostForm("source")
  88. err = thirdUpload(source, fileName, base64File)
  89. if err != nil {
  90. e.Error(200, errors.New(""), "上传第三方失败")
  91. return fileResponse
  92. }
  93. if source != "1" {
  94. fileResponse.Path = "/static/uploadfile/" + fileName
  95. fileResponse.FullPath = "/static/uploadfile/" + fileName
  96. }
  97. return fileResponse
  98. }
  99. func (e *File) multipleFile(c *gin.Context, urlPerfix string) []FileResponse {
  100. files := c.Request.MultipartForm.File["file"]
  101. source, _ := c.GetPostForm("source")
  102. var multipartFile []FileResponse
  103. for _, f := range files {
  104. guid := uuid.New().String()
  105. fileName := guid + utils.GetExt(f.Filename)
  106. err := utils.IsNotExistMkDir(path)
  107. if err != nil {
  108. e.Error(500, errors.New(""), "初始化文件路径失败")
  109. }
  110. multipartFileName := path + fileName
  111. err1 := c.SaveUploadedFile(f, multipartFileName)
  112. fileType, _ := utils.GetType(multipartFileName)
  113. if err1 == nil {
  114. err := thirdUpload(source, fileName, multipartFileName)
  115. if err != nil {
  116. e.Error(500, errors.New(""), "上传第三方失败")
  117. } else {
  118. fileResponse := FileResponse{
  119. Size: pkg.GetFileSize(multipartFileName),
  120. Path: multipartFileName,
  121. FullPath: urlPerfix + multipartFileName,
  122. Name: f.Filename,
  123. Type: fileType,
  124. }
  125. if source != "1" {
  126. fileResponse.Path = "/static/uploadfile/" + fileName
  127. fileResponse.FullPath = "/static/uploadfile/" + fileName
  128. }
  129. multipartFile = append(multipartFile, fileResponse)
  130. }
  131. }
  132. }
  133. return multipartFile
  134. }
  135. func (e *File) singleFile(c *gin.Context, fileResponse FileResponse, urlPrefix string) (FileResponse, bool) {
  136. files, err := c.FormFile("file")
  137. if err != nil {
  138. e.Error(200, errors.New(""), "图片不能为空")
  139. return FileResponse{}, true
  140. }
  141. // 上传文件至指定目录
  142. guid := uuid.New().String()
  143. fileName := guid + utils.GetExt(files.Filename)
  144. err = utils.IsNotExistMkDir(path)
  145. if err != nil {
  146. e.Error(500, errors.New(""), "初始化文件路径失败")
  147. }
  148. singleFile := path + fileName
  149. _ = c.SaveUploadedFile(files, singleFile)
  150. fileType, _ := utils.GetType(singleFile)
  151. fileResponse = FileResponse{
  152. Size: pkg.GetFileSize(singleFile),
  153. Path: singleFile,
  154. FullPath: urlPrefix + singleFile,
  155. Name: files.Filename,
  156. Type: fileType,
  157. }
  158. //source, _ := c.GetPostForm("source")
  159. //err = thirdUpload(source, fileName, singleFile)
  160. //if err != nil {
  161. // e.Error(200, errors.New(""), "上传第三方失败")
  162. // return FileResponse{}, true
  163. //}
  164. fileResponse.Path = "/static/uploadfile/" + fileName
  165. fileResponse.FullPath = "/static/uploadfile/" + fileName
  166. return fileResponse, false
  167. }
  168. func thirdUpload(source string, name string, path string) error {
  169. switch source {
  170. case "2":
  171. //return ossUpload("img/"+name, path)
  172. case "3":
  173. //return qiniuUpload("img/"+name, path)
  174. }
  175. return nil
  176. }
  177. //func ossUpload(name string, path string) error {
  178. // oss := file_store.ALiYunOSS{}
  179. // return oss.UpLoad(name, path)
  180. //}
  181. //
  182. //func qiniuUpload(name string, path string) error {
  183. // oss := file_store.ALiYunOSS{}
  184. // return oss.UpLoad(name, path)
  185. //}