upload.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package controller
  2. import (
  3. "cold-delivery/common/file_store"
  4. "cold-delivery/conf"
  5. "fmt"
  6. "github.com/gin-gonic/gin"
  7. "github.com/google/uuid"
  8. "gogs.baozhida.cn/zoie/OAuth-core/api"
  9. "os"
  10. "path/filepath"
  11. "strings"
  12. "time"
  13. )
  14. type UploadController struct {
  15. api.Api
  16. }
  17. // FilesUpload 文件上传
  18. // @Summary 文件上传
  19. // @Description 文件上传
  20. // @Tags 工具
  21. // @Success 200 {object} response.Response{data=string,id=string,msg=string} "{"code": 200, "data": [...]}"
  22. // @Router /api/upload [get]
  23. func (e UploadController) FilesUpload(c *gin.Context) {
  24. err := e.MakeContext(c).Errors
  25. if err != nil {
  26. e.Error(500, err, "服务初始化失败!")
  27. return
  28. }
  29. // 处理文件上传逻辑
  30. file, err := c.FormFile("file")
  31. if err != nil {
  32. c.JSON(400, gin.H{"error": "获取文件失败!"})
  33. return
  34. }
  35. uploadFileName := file.Filename // 获取文件名
  36. fileType := filepath.Ext(uploadFileName) // 获取文件类型(扩展名)
  37. if err != nil {
  38. c.JSON(400, gin.H{"error": err.Error()})
  39. return
  40. }
  41. filePath := file_store.GetCurrentDirectory() + "/uploads/" + uploadFileName
  42. err = c.SaveUploadedFile(file, filePath)
  43. if err != nil {
  44. c.JSON(400, gin.H{"error": err.Error()})
  45. return
  46. }
  47. defer func() {
  48. os.Remove(filePath)
  49. }()
  50. filename := fmt.Sprintf("%s/%s%s", time.Now().Format("2006-01-02"), strings.Replace(uuid.New().String(), "-", "", -1), fileType)
  51. err = file_store.QiniuFileStore.UpLoad(filename, filePath)
  52. if err != nil {
  53. c.JSON(400, gin.H{"error": "文件上传失败"})
  54. return
  55. }
  56. e.Custom(gin.H{
  57. "code": 200,
  58. "data": conf.ExtConfig.Qiniu.Endpoint + filename,
  59. "msg": "success",
  60. })
  61. }
  62. // ApkUpload 文件上传
  63. // @Summary 文件上传
  64. // @Description 文件上传
  65. // @Tags 工具
  66. // @Success 200 {object} response.Response{data=string,id=string,msg=string} "{"code": 200, "data": [...]}"
  67. // @Router /api/apk-upload [get]
  68. func (e UploadController) ApkUpload(c *gin.Context) {
  69. err := e.MakeContext(c).Errors
  70. if err != nil {
  71. e.Error(500, err, "服务初始化失败!")
  72. return
  73. }
  74. // 处理文件上传逻辑
  75. file, err := c.FormFile("file")
  76. uploadFileName := file.Filename // 获取文件名
  77. fileType := filepath.Ext(uploadFileName) // 获取文件类型(扩展名)
  78. if err != nil {
  79. c.JSON(500, gin.H{"error": err.Error()})
  80. return
  81. }
  82. if fileType != ".apk" {
  83. c.JSON(500, gin.H{"code": 200,
  84. "data": conf.ExtConfig.Apk.Path + "/" + uploadFileName,
  85. "msg": "success"})
  86. return
  87. }
  88. filePath := conf.ExtConfig.Apk.Path + "/" + uploadFileName
  89. err = c.SaveUploadedFile(file, filePath)
  90. if err != nil {
  91. c.JSON(500, gin.H{"error": err.Error()})
  92. return
  93. }
  94. e.Custom(gin.H{
  95. "code": 200,
  96. "data": conf.ExtConfig.Apk.Path + "/" + uploadFileName,
  97. "msg": "success",
  98. })
  99. }