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.

87 lines
2.6 KiB

  1. package v1
  2. import (
  3. "fmt"
  4. "gin-vue-admin/global/response"
  5. "gin-vue-admin/model"
  6. "gin-vue-admin/service"
  7. "gin-vue-admin/utils"
  8. "github.com/gin-gonic/gin"
  9. )
  10. // @Tags SimpleUploader
  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 /simpleUploader/upload [post]
  18. func SimpleUploaderUpload(c *gin.Context) {
  19. var chunk model.ExaSimpleUploader
  20. _, header, err := c.Request.FormFile("file")
  21. chunk.Filename = c.PostForm("filename")
  22. chunk.ChunkNumber = c.PostForm("chunkNumber")
  23. chunk.CurrentChunkSize = c.PostForm("currentChunkSize")
  24. chunk.Identifier = c.PostForm("identifier")
  25. chunk.TotalSize = c.PostForm("totalSize")
  26. chunk.TotalChunks = c.PostForm("totalChunks")
  27. var chunkDir = "./chunk/" + chunk.Identifier + "/"
  28. hasDir, _ := utils.PathExists(chunkDir)
  29. if !hasDir {
  30. utils.CreateDir(chunkDir)
  31. }
  32. chunkPath := chunkDir + chunk.Filename + chunk.ChunkNumber
  33. err = c.SaveUploadedFile(header, chunkPath)
  34. if err != nil {
  35. response.FailWithMessage(fmt.Sprintf("切片创建失败,%v", err), c)
  36. return
  37. }
  38. chunk.CurrentChunkPath = chunkPath
  39. err = service.SaveChunk(chunk)
  40. if err != nil {
  41. response.FailWithMessage(fmt.Sprintf("切片创建失败,%v", err), c)
  42. return
  43. } else {
  44. response.Ok(c)
  45. }
  46. }
  47. // @Tags SimpleUploader
  48. // @Summary 断点续传插件版示例
  49. // @Security ApiKeyAuth
  50. // @Produce application/json
  51. // @Param md5 query string true "测试文件是否已经存在和判断已经上传过的切片"
  52. // @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
  53. // @Router /simpleUploader/checkFileMd5 [get]
  54. func CheckFileMd5(c *gin.Context) {
  55. md5 := c.Query("md5")
  56. err, chunks, isDone := service.CheckFileMd5(md5)
  57. if err != nil {
  58. response.FailWithMessage(fmt.Sprintf("md5读取失败,%v", err), c)
  59. } else {
  60. response.OkWithData(gin.H{
  61. "chunks": chunks,
  62. "isDone": isDone,
  63. }, c)
  64. }
  65. }
  66. // @Tags SimpleUploader
  67. // @Summary 合并文件
  68. // @Security ApiKeyAuth
  69. // @Produce application/json
  70. // @Param md5 query string true "合并文件"
  71. // @Success 200 {string} string "{"success":true,"data":{},"msg":"合并成功"}"
  72. // @Router /simpleUploader/mergeFileMd5 [get]
  73. func MergeFileMd5(c *gin.Context) {
  74. md5 := c.Query("md5")
  75. fileName := c.Query("fileName")
  76. err := service.MergeFileMd5(md5, fileName)
  77. if err != nil {
  78. response.FailWithMessage(fmt.Sprintf("md5读取失败,%v", err), c)
  79. } else {
  80. response.OkWithData(gin.H{}, c)
  81. }
  82. }