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.

93 lines
2.9 KiB

  1. package v1
  2. import (
  3. "gin-vue-admin/global"
  4. "gin-vue-admin/model"
  5. "gin-vue-admin/model/response"
  6. "gin-vue-admin/service"
  7. "gin-vue-admin/utils"
  8. "github.com/gin-gonic/gin"
  9. "go.uber.org/zap"
  10. )
  11. // @Tags SimpleUploader
  12. // @Summary 断点续传插件版示例
  13. // @Security ApiKeyAuth
  14. // @accept multipart/form-data
  15. // @Produce application/json
  16. // TODO @Param file formData file true "断点续传插件版示例"
  17. // @Success 200 {string} string "{"success":true,"data":{},"msg":"切片创建成功"}"
  18. // @Router /simpleUploader/upload [post]
  19. func SimpleUploaderUpload(c *gin.Context) {
  20. var chunk model.ExaSimpleUploader
  21. _, header, err := c.Request.FormFile("file")
  22. chunk.Filename = c.PostForm("filename")
  23. chunk.ChunkNumber = c.PostForm("chunkNumber")
  24. chunk.CurrentChunkSize = c.PostForm("currentChunkSize")
  25. chunk.Identifier = c.PostForm("identifier")
  26. chunk.TotalSize = c.PostForm("totalSize")
  27. chunk.TotalChunks = c.PostForm("totalChunks")
  28. var chunkDir = "./chunk/" + chunk.Identifier + "/"
  29. hasDir, _ := utils.PathExists(chunkDir)
  30. if !hasDir {
  31. if err := utils.CreateDir(chunkDir); err != nil {
  32. global.GVA_LOG.Error("创建目录失败!", zap.Any("err", err))
  33. }
  34. }
  35. chunkPath := chunkDir + chunk.Filename + chunk.ChunkNumber
  36. err = c.SaveUploadedFile(header, chunkPath)
  37. if err != nil {
  38. global.GVA_LOG.Error("切片创建失败!", zap.Any("err", err))
  39. response.FailWithMessage("切片创建失败", c)
  40. return
  41. }
  42. chunk.CurrentChunkPath = chunkPath
  43. err = service.SaveChunk(chunk)
  44. if err != nil {
  45. global.GVA_LOG.Error("切片创建失败!", zap.Any("err", err))
  46. response.FailWithMessage("切片创建失败", c)
  47. return
  48. } else {
  49. response.OkWithMessage("切片创建成功", c)
  50. }
  51. }
  52. // @Tags SimpleUploader
  53. // @Summary 断点续传插件版示例
  54. // @Security ApiKeyAuth
  55. // @Produce application/json
  56. // @Param md5 query string true "md5"
  57. // @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
  58. // @Router /simpleUploader/checkFileMd5 [get]
  59. func CheckFileMd5(c *gin.Context) {
  60. md5 := c.Query("md5")
  61. err, chunks, isDone := service.CheckFileMd5(md5)
  62. if err != nil {
  63. global.GVA_LOG.Error("md5读取失败!", zap.Any("err", err))
  64. response.FailWithMessage("md5读取失败", c)
  65. } else {
  66. response.OkWithDetailed(gin.H{
  67. "chunks": chunks,
  68. "isDone": isDone,
  69. },"查询成功", c)
  70. }
  71. }
  72. // @Tags SimpleUploader
  73. // @Summary 合并文件
  74. // @Security ApiKeyAuth
  75. // @Produce application/json
  76. // @Param md5 query string true "md5"
  77. // @Success 200 {string} string "{"success":true,"data":{},"msg":"合并成功"}"
  78. // @Router /simpleUploader/mergeFileMd5 [get]
  79. func MergeFileMd5(c *gin.Context) {
  80. md5 := c.Query("md5")
  81. fileName := c.Query("fileName")
  82. err := service.MergeFileMd5(md5, fileName)
  83. if err != nil {
  84. global.GVA_LOG.Error("md5读取失败!", zap.Any("err", err))
  85. response.FailWithMessage("md5读取失败", c)
  86. } else {
  87. response.OkWithMessage("合并成功", c)
  88. }
  89. }