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.

97 lines
3.1 KiB

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