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.

100 lines
3.2 KiB

  1. package api
  2. import (
  3. "fmt"
  4. "gin-vue-admin/controller/servers"
  5. "gin-vue-admin/model/dbModel"
  6. "gin-vue-admin/model/modelInterface"
  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. servers.ReportFormat(c, false, fmt.Sprintf("上传文件失败,%v", err), gin.H{})
  23. } else {
  24. //文件上传后拿到文件路径
  25. err, filePath, key := servers.Upload(header, USER_HEADER_BUCKET, USER_HEADER_IMG_PATH)
  26. if err != nil {
  27. servers.ReportFormat(c, false, fmt.Sprintf("接收返回值失败,%v", err), gin.H{})
  28. } else {
  29. //修改数据库后得到修改后的user并且返回供前端使用
  30. var file dbModel.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. servers.ReportFormat(c, false, fmt.Sprintf("修改数据库链接失败,%v", err), gin.H{})
  41. } else {
  42. servers.ReportFormat(c, true, "上传成功", gin.H{"file": file})
  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 dbModel.ExaFileUploadAndDownload
  56. _ = c.ShouldBind(&file)
  57. err, f := file.FindFile()
  58. if err != nil {
  59. servers.ReportFormat(c, false, fmt.Sprintf("删除失败,%v", err), gin.H{})
  60. } else {
  61. err = servers.DeleteFile(USER_HEADER_BUCKET, f.Key)
  62. if err != nil {
  63. servers.ReportFormat(c, false, fmt.Sprintf("删除失败,%v", err), gin.H{})
  64. } else {
  65. err = f.DeleteFile()
  66. if err != nil {
  67. servers.ReportFormat(c, false, fmt.Sprintf("删除失败,%v", err), gin.H{})
  68. } else {
  69. servers.ReportFormat(c, true, fmt.Sprintf("删除成功,%v", err), gin.H{})
  70. }
  71. }
  72. }
  73. }
  74. // @Tags ExaFileUploadAndDownload
  75. // @Summary 分页文件列表
  76. // @Security ApiKeyAuth
  77. // @accept application/json
  78. // @Produce application/json
  79. // @Param data body modelInterface.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 modelInterface.PageInfo
  84. _ = c.BindJSON(&pageInfo)
  85. err, list, total := new(dbModel.ExaFileUploadAndDownload).GetInfoList(pageInfo)
  86. if err != nil {
  87. servers.ReportFormat(c, false, fmt.Sprintf("获取数据失败,%v", err), gin.H{})
  88. } else {
  89. servers.ReportFormat(c, true, "获取数据成功", gin.H{
  90. "list": list,
  91. "total": total,
  92. "page": pageInfo.Page,
  93. "pageSize": pageInfo.PageSize,
  94. })
  95. }
  96. }