exa_breakpoint_continue.go 4.9 KB

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