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. _ "gin-vue-admin/model/response"
  6. resp "gin-vue-admin/model/response"
  7. "gin-vue-admin/service"
  8. "gin-vue-admin/utils"
  9. "github.com/gin-gonic/gin"
  10. "io/ioutil"
  11. "strconv"
  12. )
  13. // @Tags ExaFileUploadAndDownload
  14. // @Summary 断点续传到服务器
  15. // @Security ApiKeyAuth
  16. // @accept multipart/form-data
  17. // @Produce application/json
  18. // @Param file formData file true "an example for breakpoint resume, 断点续传示例"
  19. // @Success 200 {string} string "{"success":true,"data":{},"msg":"上传成功"}"
  20. // @Router /fileUploadAndDownload/breakpointContinue [post]
  21. func BreakpointContinue(c *gin.Context) {
  22. fileMd5 := c.Request.FormValue("fileMd5")
  23. fileName := c.Request.FormValue("fileName")
  24. chunkMd5 := c.Request.FormValue("chunkMd5")
  25. chunkNumber, _ := strconv.Atoi(c.Request.FormValue("chunkNumber"))
  26. chunkTotal, _ := strconv.Atoi(c.Request.FormValue("chunkTotal"))
  27. _, FileHeader, err := c.Request.FormFile("file")
  28. if err != nil {
  29. response.FailWithMessage(err.Error(), c)
  30. } else {
  31. f, err := FileHeader.Open()
  32. if err != nil {
  33. response.FailWithMessage(err.Error(), c)
  34. } else {
  35. cen, _ := ioutil.ReadAll(f)
  36. defer f.Close()
  37. if flag := utils.CheckMd5(cen, chunkMd5); flag {
  38. err, file := service.FindOrCreateFile(fileMd5, fileName, chunkTotal)
  39. if err != nil {
  40. response.FailWithMessage(err.Error(), c)
  41. } else {
  42. err, pathc := utils.BreakPointContinue(cen, fileName, chunkNumber, chunkTotal, fileMd5)
  43. if err != nil {
  44. response.FailWithMessage(err.Error(), c)
  45. } else {
  46. err = service.CreateFileChunk(file.ID, pathc, chunkNumber)
  47. if err != nil {
  48. response.FailWithMessage(err.Error(), c)
  49. } else {
  50. response.OkWithMessage("切片创建成功", c)
  51. }
  52. }
  53. }
  54. } else {
  55. }
  56. }
  57. }
  58. }
  59. // @Tags ExaFileUploadAndDownload
  60. // @Summary 查找文件
  61. // @Security ApiKeyAuth
  62. // @accept multipart/form-data
  63. // @Produce application/json
  64. // @Param file formData file true "Find the file, 查找文件"
  65. // @Success 200 {string} string "{"success":true,"data":{},"msg":"查找成功"}"
  66. // @Router /fileUploadAndDownload/findFile [post]
  67. func FindFile(c *gin.Context) {
  68. fileMd5 := c.Query("fileMd5")
  69. fileName := c.Query("fileName")
  70. chunkTotal, _ := strconv.Atoi(c.Query("chunkTotal"))
  71. err, file := service.FindOrCreateFile(fileMd5, fileName, chunkTotal)
  72. if err != nil {
  73. response.FailWithMessage("查找失败", c)
  74. } else {
  75. response.OkWithData(resp.FileResponse{File: file}, c)
  76. }
  77. }
  78. // @Tags ExaFileUploadAndDownload
  79. // @Summary 查找文件
  80. // @Security ApiKeyAuth
  81. // @accept multipart/form-data
  82. // @Produce application/json
  83. // @Param file formData file true "上传文件完成"
  84. // @Success 200 {string} string "{"success":true,"data":{},"msg":"file uploaded, 文件创建成功"}"
  85. // @Router /fileUploadAndDownload/findFile [post]
  86. func BreakpointContinueFinish(c *gin.Context) {
  87. fileMd5 := c.Query("fileMd5")
  88. fileName := c.Query("fileName")
  89. err, filePath := utils.MakeFile(fileName, fileMd5)
  90. if err != nil {
  91. response.FailWithDetailed(response.ERROR, resp.FilePathResponse{FilePath: filePath}, fmt.Sprintf("文件创建失败:%v", err), c)
  92. } else {
  93. response.OkDetailed(resp.FilePathResponse{FilePath: filePath}, "文件创建成功", c)
  94. }
  95. }
  96. // @Tags ExaFileUploadAndDownload
  97. // @Summary 删除切片
  98. // @Security ApiKeyAuth
  99. // @accept multipart/form-data
  100. // @Produce application/json
  101. // @Param file formData file true "删除缓存切片"
  102. // @Success 200 {string} string "{"success":true,"data":{},"msg":"查找成功"}"
  103. // @Router /fileUploadAndDownload/removeChunk [post]
  104. func RemoveChunk(c *gin.Context) {
  105. fileMd5 := c.Query("fileMd5")
  106. fileName := c.Query("fileName")
  107. filePath := c.Query("filePath")
  108. err := utils.RemoveChunk(fileMd5)
  109. err = service.DeleteFileChunk(fileMd5, fileName, filePath)
  110. if err != nil {
  111. response.FailWithDetailed(response.ERROR, resp.FilePathResponse{FilePath: filePath}, fmt.Sprintf("缓存切片删除失败:%v", err), c)
  112. } else {
  113. response.OkDetailed(resp.FilePathResponse{FilePath: filePath}, "缓存切片删除成功", c)
  114. }
  115. }