exa_breakpoint_continue.go 5.3 KB

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