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.

120 lines
4.0 KiB

  1. package v1
  2. import (
  3. "fmt"
  4. "gin-vue-admin/global/response"
  5. resp "gin-vue-admin/model/response"
  6. "gin-vue-admin/service"
  7. "gin-vue-admin/utils"
  8. "github.com/gin-gonic/gin"
  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 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. response.FailWithMessage(err.Error(), c)
  29. return
  30. }
  31. f, err := FileHeader.Open()
  32. if err != nil {
  33. response.FailWithMessage(err.Error(), c)
  34. return
  35. }
  36. defer f.Close()
  37. cen, _ := ioutil.ReadAll(f)
  38. if flag := utils.CheckMd5(cen, chunkMd5); !flag {
  39. response.FailWithMessage(err.Error(), c)
  40. return
  41. }
  42. err, file := service.FindOrCreateFile(fileMd5, fileName, chunkTotal)
  43. if err != nil {
  44. response.FailWithMessage(err.Error(), c)
  45. return
  46. }
  47. err, pathc := utils.BreakPointContinue(cen, fileName, chunkNumber, chunkTotal, fileMd5)
  48. if err != nil {
  49. response.FailWithMessage(err.Error(), c)
  50. return
  51. }
  52. if err = service.CreateFileChunk(file.ID, pathc, chunkNumber); err != nil {
  53. response.FailWithMessage(err.Error(), c)
  54. return
  55. }
  56. response.OkWithMessage("切片创建成功", c)
  57. }
  58. // @Tags ExaFileUploadAndDownload
  59. // @Summary 查找文件
  60. // @Security ApiKeyAuth
  61. // @accept multipart/form-data
  62. // @Produce application/json
  63. // @Param file formData file true "Find the file, 查找文件"
  64. // @Success 200 {string} string "{"success":true,"data":{},"msg":"查找成功"}"
  65. // @Router /fileUploadAndDownload/findFile [post]
  66. func FindFile(c *gin.Context) {
  67. fileMd5 := c.Query("fileMd5")
  68. fileName := c.Query("fileName")
  69. chunkTotal, _ := strconv.Atoi(c.Query("chunkTotal"))
  70. err, file := service.FindOrCreateFile(fileMd5, fileName, chunkTotal)
  71. if err != nil {
  72. response.FailWithMessage("查找失败", c)
  73. } else {
  74. response.OkWithData(resp.FileResponse{File: file}, c)
  75. }
  76. }
  77. // @Tags ExaFileUploadAndDownload
  78. // @Summary 查找文件
  79. // @Security ApiKeyAuth
  80. // @accept multipart/form-data
  81. // @Produce application/json
  82. // @Param file formData file true "上传文件完成"
  83. // @Success 200 {string} string "{"success":true,"data":{},"msg":"file uploaded, 文件创建成功"}"
  84. // @Router /fileUploadAndDownload/findFile [post]
  85. func BreakpointContinueFinish(c *gin.Context) {
  86. fileMd5 := c.Query("fileMd5")
  87. fileName := c.Query("fileName")
  88. err, filePath := utils.MakeFile(fileName, fileMd5)
  89. if err != nil {
  90. response.FailWithDetailed(response.ERROR, resp.FilePathResponse{FilePath: filePath}, fmt.Sprintf("文件创建失败:%v", err), c)
  91. } else {
  92. response.OkDetailed(resp.FilePathResponse{FilePath: filePath}, "文件创建成功", c)
  93. }
  94. }
  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 {string} string "{"success":true,"data":{},"msg":"查找成功"}"
  102. // @Router /fileUploadAndDownload/removeChunk [post]
  103. func RemoveChunk(c *gin.Context) {
  104. fileMd5 := c.Query("fileMd5")
  105. fileName := c.Query("fileName")
  106. filePath := c.Query("filePath")
  107. err := utils.RemoveChunk(fileMd5)
  108. err = service.DeleteFileChunk(fileMd5, fileName, filePath)
  109. if err != nil {
  110. response.FailWithDetailed(response.ERROR, resp.FilePathResponse{FilePath: filePath}, fmt.Sprintf("缓存切片删除失败:%v", err), c)
  111. } else {
  112. response.OkDetailed(resp.FilePathResponse{FilePath: filePath}, "缓存切片删除成功", c)
  113. }
  114. }