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.

119 lines
3.9 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. return
  40. }
  41. err, file := service.FindOrCreateFile(fileMd5, fileName, chunkTotal)
  42. if err != nil {
  43. response.FailWithMessage(err.Error(), c)
  44. return
  45. }
  46. err, pathc := utils.BreakPointContinue(cen, fileName, chunkNumber, chunkTotal, fileMd5)
  47. if err != nil {
  48. response.FailWithMessage(err.Error(), c)
  49. return
  50. }
  51. if err = service.CreateFileChunk(file.ID, pathc, chunkNumber); err != nil {
  52. response.FailWithMessage(err.Error(), c)
  53. return
  54. }
  55. response.OkWithMessage("切片创建成功", c)
  56. }
  57. // @Tags ExaFileUploadAndDownload
  58. // @Summary 查找文件
  59. // @Security ApiKeyAuth
  60. // @accept multipart/form-data
  61. // @Produce application/json
  62. // @Param file formData file true "Find the file, 查找文件"
  63. // @Success 200 {string} string "{"success":true,"data":{},"msg":"查找成功"}"
  64. // @Router /fileUploadAndDownload/findFile [post]
  65. func FindFile(c *gin.Context) {
  66. fileMd5 := c.Query("fileMd5")
  67. fileName := c.Query("fileName")
  68. chunkTotal, _ := strconv.Atoi(c.Query("chunkTotal"))
  69. err, file := service.FindOrCreateFile(fileMd5, fileName, chunkTotal)
  70. if err != nil {
  71. response.FailWithMessage("查找失败", c)
  72. } else {
  73. response.OkWithData(resp.FileResponse{File: file}, c)
  74. }
  75. }
  76. // @Tags ExaFileUploadAndDownload
  77. // @Summary 查找文件
  78. // @Security ApiKeyAuth
  79. // @accept multipart/form-data
  80. // @Produce application/json
  81. // @Param file formData file true "上传文件完成"
  82. // @Success 200 {string} string "{"success":true,"data":{},"msg":"file uploaded, 文件创建成功"}"
  83. // @Router /fileUploadAndDownload/findFile [post]
  84. func BreakpointContinueFinish(c *gin.Context) {
  85. fileMd5 := c.Query("fileMd5")
  86. fileName := c.Query("fileName")
  87. err, filePath := utils.MakeFile(fileName, fileMd5)
  88. if err != nil {
  89. response.FailWithDetailed(response.ERROR, resp.FilePathResponse{FilePath: filePath}, fmt.Sprintf("文件创建失败:%v", err), c)
  90. } else {
  91. response.OkDetailed(resp.FilePathResponse{FilePath: filePath}, "文件创建成功", c)
  92. }
  93. }
  94. // @Tags ExaFileUploadAndDownload
  95. // @Summary 删除切片
  96. // @Security ApiKeyAuth
  97. // @accept multipart/form-data
  98. // @Produce application/json
  99. // @Param file formData file true "删除缓存切片"
  100. // @Success 200 {string} string "{"success":true,"data":{},"msg":"查找成功"}"
  101. // @Router /fileUploadAndDownload/removeChunk [post]
  102. func RemoveChunk(c *gin.Context) {
  103. fileMd5 := c.Query("fileMd5")
  104. fileName := c.Query("fileName")
  105. filePath := c.Query("filePath")
  106. err := utils.RemoveChunk(fileMd5)
  107. err = service.DeleteFileChunk(fileMd5, fileName, filePath)
  108. if err != nil {
  109. response.FailWithDetailed(response.ERROR, resp.FilePathResponse{FilePath: filePath}, fmt.Sprintf("缓存切片删除失败:%v", err), c)
  110. } else {
  111. response.OkDetailed(resp.FilePathResponse{FilePath: filePath}, "缓存切片删除成功", c)
  112. }
  113. }