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.

105 lines
3.2 KiB

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