You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

114 lines
3.4 KiB

  1. package v1
  2. import (
  3. "fmt"
  4. "gin-vue-admin/global"
  5. "gin-vue-admin/global/response"
  6. "gin-vue-admin/model"
  7. "gin-vue-admin/model/request"
  8. resp "gin-vue-admin/model/response"
  9. "gin-vue-admin/service"
  10. "gin-vue-admin/utils"
  11. "github.com/gin-gonic/gin"
  12. "strings"
  13. )
  14. // @Tags ExaFileUploadAndDownload
  15. // @Summary 上传文件示例
  16. // @Security ApiKeyAuth
  17. // @accept multipart/form-data
  18. // @Produce application/json
  19. // @Param file formData file true "上传文件示例"
  20. // @Success 200 {string} string "{"success":true,"data":{},"msg":"上传成功"}"
  21. // @Router /fileUploadAndDownload/upload [post]
  22. func UploadFile(c *gin.Context) {
  23. noSave := c.DefaultQuery("noSave", "0")
  24. _, header, err := c.Request.FormFile("file")
  25. if err != nil {
  26. response.FailWithMessage(fmt.Sprintf("上传文件失败,%v", err), c)
  27. } else {
  28. // 文件上传后拿到文件路径
  29. var uploadErr error
  30. var filePath string
  31. var key string
  32. if global.GVA_CONFIG.LocalUpload.Local {
  33. // 本地上传
  34. uploadErr, filePath, key = utils.UploadFileLocal(header)
  35. } else {
  36. // 七牛云上传
  37. uploadErr, filePath, key = utils.UploadRemote(header)
  38. }
  39. if uploadErr != nil {
  40. response.FailWithMessage(fmt.Sprintf("接收返回值失败,%v", err), c)
  41. } else {
  42. // 修改数据库后得到修改后的user并且返回供前端使用
  43. var file model.ExaFileUploadAndDownload
  44. file.Url = filePath
  45. file.Name = header.Filename
  46. s := strings.Split(file.Name, ".")
  47. file.Tag = s[len(s)-1]
  48. file.Key = key
  49. if noSave == "0" {
  50. err = service.Upload(file)
  51. }
  52. if err != nil {
  53. response.FailWithMessage(fmt.Sprintf("修改数据库链接失败,%v", err), c)
  54. } else {
  55. response.OkDetailed(resp.ExaFileResponse{File: file}, "上传成功", c)
  56. }
  57. }
  58. }
  59. }
  60. // @Tags ExaFileUploadAndDownload
  61. // @Summary 删除文件
  62. // @Security ApiKeyAuth
  63. // @Produce application/json
  64. // @Param data body model.ExaFileUploadAndDownload true "传入文件里面id即可"
  65. // @Success 200 {string} string "{"success":true,"data":{},"msg":"返回成功"}"
  66. // @Router /fileUploadAndDownload/deleteFile [post]
  67. func DeleteFile(c *gin.Context) {
  68. var file model.ExaFileUploadAndDownload
  69. _ = c.ShouldBindJSON(&file)
  70. err, f := service.FindFile(file.ID)
  71. if err != nil {
  72. response.FailWithMessage(fmt.Sprintf("删除失败,%v", err), c)
  73. } else {
  74. err = utils.DeleteFile(f.Key)
  75. if err != nil {
  76. response.FailWithMessage(fmt.Sprintf("删除失败,%v", err), c)
  77. } else {
  78. err = service.DeleteFile(f)
  79. if err != nil {
  80. response.FailWithMessage(fmt.Sprintf("删除失败,%v", err), c)
  81. } else {
  82. response.OkWithMessage("删除成功", c)
  83. }
  84. }
  85. }
  86. }
  87. // @Tags ExaFileUploadAndDownload
  88. // @Summary 分页文件列表
  89. // @Security ApiKeyAuth
  90. // @accept application/json
  91. // @Produce application/json
  92. // @Param data body request.PageInfo true "分页获取文件户列表"
  93. // @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
  94. // @Router /fileUploadAndDownload/getFileList [post]
  95. func GetFileList(c *gin.Context) {
  96. var pageInfo request.PageInfo
  97. _ = c.ShouldBindJSON(&pageInfo)
  98. err, list, total := service.GetFileRecordInfoList(pageInfo)
  99. if err != nil {
  100. response.FailWithMessage(fmt.Sprintf("获取数据失败,%v", err), c)
  101. } else {
  102. response.OkWithData(resp.PageResult{
  103. List: list,
  104. Total: total,
  105. Page: pageInfo.Page,
  106. PageSize: pageInfo.PageSize,
  107. }, c)
  108. }
  109. }