file.go 5.6 KB

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