exa_file_upload_download.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package example
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "go.uber.org/zap"
  5. "server/global"
  6. "server/model/common/request"
  7. "server/model/common/response"
  8. "server/model/example"
  9. exampleRes "server/model/example/response"
  10. )
  11. type FileUploadAndDownloadApi struct{}
  12. // UploadFile
  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 {object} response.Response{data=exampleRes.ExaFileResponse,msg=string} "上传文件示例,返回包括文件详情"
  20. // @Router /fileUploadAndDownload/upload [post]
  21. func (b *FileUploadAndDownloadApi) UploadFile(c *gin.Context) {
  22. var file example.ExaFileUploadAndDownload
  23. noSave := c.DefaultQuery("noSave", "0")
  24. _, header, err := c.Request.FormFile("file")
  25. if err != nil {
  26. global.GVA_LOG.Error("接收文件失败!", zap.Error(err))
  27. response.FailWithMessage("接收文件失败", c)
  28. return
  29. }
  30. file, err = fileUploadAndDownloadService.UploadFile(header, noSave) // 文件上传后拿到文件路径
  31. if err != nil {
  32. global.GVA_LOG.Error("修改数据库链接失败!", zap.Error(err))
  33. response.FailWithMessage("修改数据库链接失败", c)
  34. return
  35. }
  36. response.OkWithDetailed(exampleRes.ExaFileResponse{File: file}, "上传成功", c)
  37. }
  38. // EditFileName 编辑文件名或者备注
  39. func (b *FileUploadAndDownloadApi) EditFileName(c *gin.Context) {
  40. var file example.ExaFileUploadAndDownload
  41. err := c.ShouldBindJSON(&file)
  42. if err != nil {
  43. response.FailWithMessage(err.Error(), c)
  44. return
  45. }
  46. err = fileUploadAndDownloadService.EditFileName(file)
  47. if err != nil {
  48. global.GVA_LOG.Error("编辑失败!", zap.Error(err))
  49. response.FailWithMessage("编辑失败", c)
  50. return
  51. }
  52. response.OkWithMessage("编辑成功", c)
  53. }
  54. // DeleteFile
  55. // @Tags ExaFileUploadAndDownload
  56. // @Summary 删除文件
  57. // @Security ApiKeyAuth
  58. // @Produce application/json
  59. // @Param data body example.ExaFileUploadAndDownload true "传入文件里面id即可"
  60. // @Success 200 {object} response.Response{msg=string} "删除文件"
  61. // @Router /fileUploadAndDownload/deleteFile [post]
  62. func (b *FileUploadAndDownloadApi) DeleteFile(c *gin.Context) {
  63. var file example.ExaFileUploadAndDownload
  64. err := c.ShouldBindJSON(&file)
  65. if err != nil {
  66. response.FailWithMessage(err.Error(), c)
  67. return
  68. }
  69. if err := fileUploadAndDownloadService.DeleteFile(file); err != nil {
  70. global.GVA_LOG.Error("删除失败!", zap.Error(err))
  71. response.FailWithMessage("删除失败", c)
  72. return
  73. }
  74. response.OkWithMessage("删除成功", c)
  75. }
  76. // GetFileList
  77. // @Tags ExaFileUploadAndDownload
  78. // @Summary 分页文件列表
  79. // @Security ApiKeyAuth
  80. // @accept application/json
  81. // @Produce application/json
  82. // @Param data body request.PageInfo true "页码, 每页大小"
  83. // @Success 200 {object} response.Response{data=response.PageResult,msg=string} "分页文件列表,返回包括列表,总数,页码,每页数量"
  84. // @Router /fileUploadAndDownload/getFileList [post]
  85. func (b *FileUploadAndDownloadApi) GetFileList(c *gin.Context) {
  86. var pageInfo request.PageInfo
  87. err := c.ShouldBindJSON(&pageInfo)
  88. if err != nil {
  89. response.FailWithMessage(err.Error(), c)
  90. return
  91. }
  92. list, total, err := fileUploadAndDownloadService.GetFileRecordInfoList(pageInfo)
  93. if err != nil {
  94. global.GVA_LOG.Error("获取失败!", zap.Error(err))
  95. response.FailWithMessage("获取失败", c)
  96. return
  97. }
  98. response.OkWithDetailed(response.PageResult{
  99. List: list,
  100. Total: total,
  101. Page: pageInfo.Page,
  102. PageSize: pageInfo.PageSize,
  103. }, "获取成功", c)
  104. }