upload.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package upload
  2. import (
  3. "fmt"
  4. "kpt-pasture/config"
  5. "net/http"
  6. "os"
  7. "time"
  8. "gitee.com/xuyiping_admin/pkg/apierr"
  9. "gitee.com/xuyiping_admin/pkg/xerr"
  10. "github.com/gin-gonic/gin"
  11. )
  12. func Photos(c *gin.Context) {
  13. form, err := c.MultipartForm()
  14. if err != nil {
  15. apierr.AbortBadRequest(c, http.StatusBadRequest, xerr.Customf("No multipartForm: %s", err.Error()))
  16. return
  17. }
  18. files := form.File["uploads"]
  19. // 验证文件数量
  20. if len(files) == 0 {
  21. apierr.AbortBadRequest(c, http.StatusBadRequest, xerr.Custom("No files selected"))
  22. return
  23. }
  24. workDir := fmt.Sprintf("%s", config.WorkDir)
  25. pathDir := fmt.Sprintf("/files/photos/%s", time.Now().Format("20060102"))
  26. saveDir := fmt.Sprintf("%s/%s", workDir, pathDir)
  27. if _, err = os.Stat(saveDir); os.IsNotExist(err) {
  28. if err = os.MkdirAll(saveDir, 0755); err != nil {
  29. apierr.AbortBadRequest(c, http.StatusBadRequest, xerr.Customf("创建目录失败: %s", err.Error()))
  30. return
  31. }
  32. }
  33. // 处理每个文件
  34. filePaths := make([]string, len(files))
  35. timestamp := time.Now().Unix()
  36. for i, file := range files {
  37. if file.Header.Get("Content-Type") != "image/jpeg" &&
  38. file.Header.Get("Content-Type") != "image/png" &&
  39. file.Header.Get("Content-Type") != "image/gif" {
  40. apierr.AbortBadRequest(c, http.StatusBadRequest, xerr.Customf("图片格式错误: %s", file.Filename))
  41. return
  42. }
  43. if file.Size > 1024*1024*5 {
  44. apierr.AbortBadRequest(c, http.StatusBadRequest, xerr.Custom("单个图片文件不能超过5MB"))
  45. return
  46. }
  47. fpath := fmt.Sprintf("%s/%d_%d_%s", saveDir, timestamp, i+1, file.Filename)
  48. urlPath := fmt.Sprintf("%s/%d_%d_%s", pathDir, timestamp, i+1, file.Filename)
  49. if err = c.SaveUploadedFile(file, fpath); err != nil {
  50. apierr.AbortBadRequest(c, http.StatusBadRequest, xerr.Customf("保存文件失败: %s", err.Error()))
  51. return
  52. }
  53. filePaths[i] = urlPath
  54. }
  55. c.JSON(http.StatusOK, gin.H{"code": http.StatusOK, "Msg": "ok", "data": filePaths})
  56. }
  57. func Files(c *gin.Context) {
  58. }
  59. func OssVideo(c *gin.Context) {
  60. filename := c.Param("name")
  61. videoPath := fmt.Sprintf("%s/files/video/%s", config.WorkDir, filename)
  62. // 打开视频文件
  63. file, err := os.Open(videoPath)
  64. if err != nil {
  65. c.JSON(http.StatusNotFound, gin.H{"error": "Video not found"})
  66. return
  67. }
  68. defer file.Close()
  69. fileInfo, err := file.Stat()
  70. if err != nil {
  71. c.JSON(http.StatusInternalServerError, gin.H{"error": "Could not get file info"})
  72. return
  73. }
  74. // 设置响应头
  75. c.Header("Content-Type", "video/mp4")
  76. c.Header("Content-Length", fmt.Sprintf("%d", fileInfo.Size()))
  77. // 流式传输文件内容
  78. http.ServeContent(c.Writer, c.Request, fileInfo.Name(), fileInfo.ModTime(), file)
  79. }