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.

95 lines
3.2 KiB

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