file.go 5.7 KB

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