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.

140 lines
5.1 KiB

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