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
3.9 KiB

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