exa_file_upload_download.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package example
  2. import (
  3. "github.com/flipped-aurora/gin-vue-admin/server/global"
  4. "github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
  5. "github.com/flipped-aurora/gin-vue-admin/server/model/example"
  6. "github.com/flipped-aurora/gin-vue-admin/server/model/example/request"
  7. exampleRes "github.com/flipped-aurora/gin-vue-admin/server/model/example/response"
  8. "github.com/gin-gonic/gin"
  9. "go.uber.org/zap"
  10. "strconv"
  11. )
  12. type FileUploadAndDownloadApi struct{}
  13. // UploadFile
  14. // @Tags ExaFileUploadAndDownload
  15. // @Summary 上传文件示例
  16. // @Security ApiKeyAuth
  17. // @accept multipart/form-data
  18. // @Produce application/json
  19. // @Param file formData file true "上传文件示例"
  20. // @Success 200 {object} response.Response{data=exampleRes.ExaFileResponse,msg=string} "上传文件示例,返回包括文件详情"
  21. // @Router /fileUploadAndDownload/upload [post]
  22. func (b *FileUploadAndDownloadApi) UploadFile(c *gin.Context) {
  23. var file example.ExaFileUploadAndDownload
  24. noSave := c.DefaultQuery("noSave", "0")
  25. _, header, err := c.Request.FormFile("file")
  26. classId, _ := strconv.Atoi(c.DefaultPostForm("classId", "0"))
  27. if err != nil {
  28. global.GVA_LOG.Error("接收文件失败!", zap.Error(err))
  29. response.FailWithMessage("接收文件失败", c)
  30. return
  31. }
  32. file, err = fileUploadAndDownloadService.UploadFile(header, noSave, classId) // 文件上传后拿到文件路径
  33. if err != nil {
  34. global.GVA_LOG.Error("上传文件失败!", zap.Error(err))
  35. response.FailWithMessage("上传文件失败", c)
  36. return
  37. }
  38. response.OkWithDetailed(exampleRes.ExaFileResponse{File: file}, "上传成功", c)
  39. }
  40. // EditFileName 编辑文件名或者备注
  41. func (b *FileUploadAndDownloadApi) EditFileName(c *gin.Context) {
  42. var file example.ExaFileUploadAndDownload
  43. err := c.ShouldBindJSON(&file)
  44. if err != nil {
  45. response.FailWithMessage(err.Error(), c)
  46. return
  47. }
  48. err = fileUploadAndDownloadService.EditFileName(file)
  49. if err != nil {
  50. global.GVA_LOG.Error("编辑失败!", zap.Error(err))
  51. response.FailWithMessage("编辑失败", c)
  52. return
  53. }
  54. response.OkWithMessage("编辑成功", c)
  55. }
  56. // DeleteFile
  57. // @Tags ExaFileUploadAndDownload
  58. // @Summary 删除文件
  59. // @Security ApiKeyAuth
  60. // @Produce application/json
  61. // @Param data body example.ExaFileUploadAndDownload true "传入文件里面id即可"
  62. // @Success 200 {object} response.Response{msg=string} "删除文件"
  63. // @Router /fileUploadAndDownload/deleteFile [post]
  64. func (b *FileUploadAndDownloadApi) DeleteFile(c *gin.Context) {
  65. var file example.ExaFileUploadAndDownload
  66. err := c.ShouldBindJSON(&file)
  67. if err != nil {
  68. response.FailWithMessage(err.Error(), c)
  69. return
  70. }
  71. if err := fileUploadAndDownloadService.DeleteFile(file); err != nil {
  72. global.GVA_LOG.Error("删除失败!", zap.Error(err))
  73. response.FailWithMessage("删除失败", c)
  74. return
  75. }
  76. response.OkWithMessage("删除成功", c)
  77. }
  78. // GetFileList
  79. // @Tags ExaFileUploadAndDownload
  80. // @Summary 分页文件列表
  81. // @Security ApiKeyAuth
  82. // @accept application/json
  83. // @Produce application/json
  84. // @Param data body request.ExaAttachmentCategorySearch true "页码, 每页大小, 分类id"
  85. // @Success 200 {object} response.Response{data=response.PageResult,msg=string} "分页文件列表,返回包括列表,总数,页码,每页数量"
  86. // @Router /fileUploadAndDownload/getFileList [post]
  87. func (b *FileUploadAndDownloadApi) GetFileList(c *gin.Context) {
  88. var pageInfo request.ExaAttachmentCategorySearch
  89. err := c.ShouldBindJSON(&pageInfo)
  90. if err != nil {
  91. response.FailWithMessage(err.Error(), c)
  92. return
  93. }
  94. list, total, err := fileUploadAndDownloadService.GetFileRecordInfoList(pageInfo)
  95. if err != nil {
  96. global.GVA_LOG.Error("获取失败!", zap.Error(err))
  97. response.FailWithMessage("获取失败", c)
  98. return
  99. }
  100. response.OkWithDetailed(response.PageResult{
  101. List: list,
  102. Total: total,
  103. Page: pageInfo.Page,
  104. PageSize: pageInfo.PageSize,
  105. }, "获取成功", c)
  106. }
  107. // ImportURL
  108. // @Tags ExaFileUploadAndDownload
  109. // @Summary 导入URL
  110. // @Security ApiKeyAuth
  111. // @Produce application/json
  112. // @Param data body example.ExaFileUploadAndDownload true "对象"
  113. // @Success 200 {object} response.Response{msg=string} "导入URL"
  114. // @Router /fileUploadAndDownload/importURL [post]
  115. func (b *FileUploadAndDownloadApi) ImportURL(c *gin.Context) {
  116. var file []example.ExaFileUploadAndDownload
  117. err := c.ShouldBindJSON(&file)
  118. if err != nil {
  119. response.FailWithMessage(err.Error(), c)
  120. return
  121. }
  122. if err := fileUploadAndDownloadService.ImportURL(&file); err != nil {
  123. global.GVA_LOG.Error("导入URL失败!", zap.Error(err))
  124. response.FailWithMessage("导入URL失败", c)
  125. return
  126. }
  127. response.OkWithMessage("导入URL成功", c)
  128. }