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.

129 lines
4.8 KiB

  1. package example
  2. import (
  3. "gin-vue-admin/global"
  4. "gin-vue-admin/model/common/response"
  5. exampleRes "gin-vue-admin/model/example/response"
  6. "gin-vue-admin/utils"
  7. "github.com/gin-gonic/gin"
  8. "go.uber.org/zap"
  9. "io/ioutil"
  10. "strconv"
  11. )
  12. // @Tags ExaFileUploadAndDownload
  13. // @Summary 断点续传到服务器
  14. // @Security ApiKeyAuth
  15. // @accept multipart/form-data
  16. // @Produce application/json
  17. // @Param file formData file true "an example for breakpoint resume, 断点续传示例"
  18. // @Success 200 {string} string "{"success":true,"data":{},"msg":"切片创建成功"}"
  19. // @Router /fileUploadAndDownload/breakpointContinue [post]
  20. func (u *FileUploadAndDownloadApi) BreakpointContinue(c *gin.Context) {
  21. fileMd5 := c.Request.FormValue("fileMd5")
  22. fileName := c.Request.FormValue("fileName")
  23. chunkMd5 := c.Request.FormValue("chunkMd5")
  24. chunkNumber, _ := strconv.Atoi(c.Request.FormValue("chunkNumber"))
  25. chunkTotal, _ := strconv.Atoi(c.Request.FormValue("chunkTotal"))
  26. _, FileHeader, err := c.Request.FormFile("file")
  27. if err != nil {
  28. global.GVA_LOG.Error("接收文件失败!", zap.Any("err", err))
  29. response.FailWithMessage("接收文件失败", c)
  30. return
  31. }
  32. f, err := FileHeader.Open()
  33. if err != nil {
  34. global.GVA_LOG.Error("文件读取失败!", zap.Any("err", err))
  35. response.FailWithMessage("文件读取失败", c)
  36. return
  37. }
  38. defer f.Close()
  39. cen, _ := ioutil.ReadAll(f)
  40. if !utils.CheckMd5(cen, chunkMd5) {
  41. global.GVA_LOG.Error("检查md5失败!", zap.Any("err", err))
  42. response.FailWithMessage("检查md5失败", c)
  43. return
  44. }
  45. err, file := fileUploadAndDownloadService.FindOrCreateFile(fileMd5, fileName, chunkTotal)
  46. if err != nil {
  47. global.GVA_LOG.Error("查找或创建记录失败!", zap.Any("err", err))
  48. response.FailWithMessage("查找或创建记录失败", c)
  49. return
  50. }
  51. err, pathc := utils.BreakPointContinue(cen, fileName, chunkNumber, chunkTotal, fileMd5)
  52. if err != nil {
  53. global.GVA_LOG.Error("断点续传失败!", zap.Any("err", err))
  54. response.FailWithMessage("断点续传失败", c)
  55. return
  56. }
  57. if err = fileUploadAndDownloadService.CreateFileChunk(file.ID, pathc, chunkNumber); err != nil {
  58. global.GVA_LOG.Error("创建文件记录失败!", zap.Any("err", err))
  59. response.FailWithMessage("创建文件记录失败", c)
  60. return
  61. }
  62. response.OkWithMessage("切片创建成功", c)
  63. }
  64. // @Tags ExaFileUploadAndDownload
  65. // @Summary 查找文件
  66. // @Security ApiKeyAuth
  67. // @accept multipart/form-data
  68. // @Produce application/json
  69. // @Param file formData file true "Find the file, 查找文件"
  70. // @Success 200 {string} string "{"success":true,"data":{},"msg":"查找成功"}"
  71. // @Router /fileUploadAndDownload/findFile [post]
  72. func (u *FileUploadAndDownloadApi) FindFile(c *gin.Context) {
  73. fileMd5 := c.Query("fileMd5")
  74. fileName := c.Query("fileName")
  75. chunkTotal, _ := strconv.Atoi(c.Query("chunkTotal"))
  76. err, file := fileUploadAndDownloadService.FindOrCreateFile(fileMd5, fileName, chunkTotal)
  77. if err != nil {
  78. global.GVA_LOG.Error("查找失败!", zap.Any("err", err))
  79. response.FailWithMessage("查找失败", c)
  80. } else {
  81. response.OkWithDetailed(exampleRes.FileResponse{File: file}, "查找成功", c)
  82. }
  83. }
  84. // @Tags ExaFileUploadAndDownload
  85. // @Summary 创建文件
  86. // @Security ApiKeyAuth
  87. // @accept multipart/form-data
  88. // @Produce application/json
  89. // @Param file formData file true "上传文件完成"
  90. // @Success 200 {string} string "{"success":true,"data":{},"msg":"file uploaded, 文件创建成功"}"
  91. // @Router /fileUploadAndDownload/findFile [post]
  92. func (b *FileUploadAndDownloadApi) BreakpointContinueFinish(c *gin.Context) {
  93. fileMd5 := c.Query("fileMd5")
  94. fileName := c.Query("fileName")
  95. err, filePath := utils.MakeFile(fileName, fileMd5)
  96. if err != nil {
  97. global.GVA_LOG.Error("文件创建失败!", zap.Any("err", err))
  98. response.FailWithDetailed(exampleRes.FilePathResponse{FilePath: filePath}, "文件创建失败", c)
  99. } else {
  100. response.OkWithDetailed(exampleRes.FilePathResponse{FilePath: filePath}, "文件创建成功", c)
  101. }
  102. }
  103. // @Tags ExaFileUploadAndDownload
  104. // @Summary 删除切片
  105. // @Security ApiKeyAuth
  106. // @accept multipart/form-data
  107. // @Produce application/json
  108. // @Param file formData file true "删除缓存切片"
  109. // @Success 200 {string} string "{"success":true,"data":{},"msg":"缓存切片删除成功"}"
  110. // @Router /fileUploadAndDownload/removeChunk [post]
  111. func (u *FileUploadAndDownloadApi) RemoveChunk(c *gin.Context) {
  112. fileMd5 := c.Query("fileMd5")
  113. fileName := c.Query("fileName")
  114. filePath := c.Query("filePath")
  115. err := utils.RemoveChunk(fileMd5)
  116. err = fileUploadAndDownloadService.DeleteFileChunk(fileMd5, fileName, filePath)
  117. if err != nil {
  118. global.GVA_LOG.Error("缓存切片删除失败!", zap.Any("err", err))
  119. response.FailWithDetailed(exampleRes.FilePathResponse{FilePath: filePath}, "缓存切片删除失败", c)
  120. } else {
  121. response.OkWithDetailed(exampleRes.FilePathResponse{FilePath: filePath}, "缓存切片删除成功", c)
  122. }
  123. }