exa_file_upload_download.go 3.3 KB

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