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.

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