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.

141 lines
5.0 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. package example
  2. import (
  3. "fmt"
  4. "github.com/flipped-aurora/gin-vue-admin/server/model/example"
  5. "io/ioutil"
  6. "mime/multipart"
  7. "strconv"
  8. "github.com/flipped-aurora/gin-vue-admin/server/global"
  9. "github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
  10. exampleRes "github.com/flipped-aurora/gin-vue-admin/server/model/example/response"
  11. "github.com/flipped-aurora/gin-vue-admin/server/utils"
  12. "github.com/gin-gonic/gin"
  13. "go.uber.org/zap"
  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 {string} string "{"success":true,"data":{},"msg":"切片创建成功"}"
  22. // @Router /fileUploadAndDownload/breakpointContinue [post]
  23. func (u *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. err, file := 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. err, pathc := 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 {string} string "{"success":true,"data":{},"msg":"查找成功"}"
  79. // @Router /fileUploadAndDownload/findFile [post]
  80. func (u *FileUploadAndDownloadApi) FindFile(c *gin.Context) {
  81. fileMd5 := c.Query("fileMd5")
  82. fileName := c.Query("fileName")
  83. chunkTotal, _ := strconv.Atoi(c.Query("chunkTotal"))
  84. err, file := 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 {string} string "{"success":true,"data":{},"msg":"file uploaded, 文件创建成功"}"
  99. // @Router /fileUploadAndDownload/findFile [post]
  100. func (b *FileUploadAndDownloadApi) BreakpointContinueFinish(c *gin.Context) {
  101. fileMd5 := c.Query("fileMd5")
  102. fileName := c.Query("fileName")
  103. err, filePath := 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 {string} string "{"success":true,"data":{},"msg":"缓存切片删除成功"}"
  118. // @Router /fileUploadAndDownload/removeChunk [post]
  119. func (u *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. response.FailWithMessage(err.Error(), c)
  125. return
  126. }
  127. err = fileUploadAndDownloadService.DeleteFileChunk(file.FileMd5, file.FileName, 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. }