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.

118 lines
4.1 KiB

  1. package v1
  2. import (
  3. "fmt"
  4. "gin-vue-admin/global/response"
  5. "gin-vue-admin/model"
  6. "gin-vue-admin/utils"
  7. "github.com/gin-gonic/gin"
  8. "io/ioutil"
  9. "strconv"
  10. )
  11. // @Tags ExaFileUploadAndDownload
  12. // @Summary 断点续传到服务器
  13. // @Security ApiKeyAuth
  14. // @accept multipart/form-data
  15. // @Produce application/json
  16. // @Param file formData file true "an example for breakpoint resume, 断点续传示例"
  17. // @Success 200 {string} string "{"success":true,"data":{},"msg":"上传成功"}"
  18. // @Router /fileUploadAndDownload/breakpointContinue [post]
  19. func BreakpointContinue(c *gin.Context) {
  20. fileMd5 := c.Request.FormValue("fileMd5")
  21. fileName := c.Request.FormValue("fileName")
  22. chunkMd5 := c.Request.FormValue("chunkMd5")
  23. chunkNumber, _ := strconv.Atoi(c.Request.FormValue("chunkNumber"))
  24. chunkTotal, _ := strconv.Atoi(c.Request.FormValue("chunkTotal"))
  25. _, FileHeader, err := c.Request.FormFile("file")
  26. if err != nil {
  27. response.Result(response.SUCCESS, nil, fmt.Sprintf("%v", err), c)
  28. } else {
  29. f, err := FileHeader.Open()
  30. if err != nil {
  31. response.Result(response.ERROR, nil, fmt.Sprintf("%v", err), c)
  32. } else {
  33. cen, _ := ioutil.ReadAll(f)
  34. defer f.Close()
  35. if flag := utils.CheckMd5(cen, chunkMd5); flag {
  36. err, file := new(model.ExaFile).FindOrCreateFile(fileMd5, fileName, chunkTotal)
  37. if err != nil {
  38. response.Result(response.ERROR, nil, fmt.Sprintf("%v", err), c)
  39. } else {
  40. err, pathc := utils.BreakPointContinue(cen, fileName, chunkNumber, chunkTotal, fileMd5)
  41. if err != nil {
  42. response.Result(response.ERROR, nil, fmt.Sprintf("%v", err), c)
  43. } else {
  44. err = file.CreateFileChunk(pathc, chunkNumber)
  45. if err != nil {
  46. response.Result(response.ERROR, nil, fmt.Sprintf("%v", err), c)
  47. } else {
  48. response.Result(response.SUCCESS, nil, "切片创建成功", c)
  49. }
  50. }
  51. }
  52. } else {
  53. }
  54. }
  55. }
  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 := new(model.ExaFile).FindOrCreateFile(fileMd5, fileName, chunkTotal)
  70. if err != nil {
  71. response.Result(response.ERROR, nil, fmt.Sprintf("查找失败:%v", err), c)
  72. } else {
  73. response.Result(response.SUCCESS, gin.H{"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.Result(response.ERROR, gin.H{"filePath": filePath}, fmt.Sprintf("文件创建失败:%v", err), c)
  90. } else {
  91. response.Result(response.SUCCESS, gin.H{"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 = new(model.ExaFile).DeleteFileChunk(fileMd5, fileName, filePath)
  108. if err != nil {
  109. response.Result(response.ERROR, gin.H{"filePath": filePath}, fmt.Sprintf("缓存切片删除失败:%v", err), c)
  110. } else {
  111. response.Result(response.SUCCESS, gin.H{"filePath": filePath}, "缓存切片删除成功", c)
  112. }
  113. }