upload.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package upload
  2. import (
  3. "fmt"
  4. "kpt-pasture/config"
  5. "kpt-pasture/http/middleware"
  6. "net/http"
  7. "os"
  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. res, err := middleware.BackendOperation(c).OpsService.Photos(c, files)
  25. if err != nil {
  26. apierr.AbortBadRequest(c, http.StatusBadRequest, err)
  27. return
  28. }
  29. c.JSON(http.StatusOK, gin.H{"code": http.StatusOK, "Msg": "ok", "data": res})
  30. }
  31. func Files(c *gin.Context) {
  32. }
  33. func OssVideo(c *gin.Context) {
  34. filename := c.Param("name")
  35. videoPath := fmt.Sprintf("%s/files/video/%s", config.WorkDir, filename)
  36. // 打开视频文件
  37. file, err := os.Open(videoPath)
  38. if err != nil {
  39. c.JSON(http.StatusNotFound, gin.H{"error": "Video not found"})
  40. return
  41. }
  42. defer file.Close()
  43. fileInfo, err := file.Stat()
  44. if err != nil {
  45. c.JSON(http.StatusInternalServerError, gin.H{"error": "Could not get file info"})
  46. return
  47. }
  48. // 设置响应头
  49. c.Header("Content-Type", "video/mp4")
  50. c.Header("Content-Length", fmt.Sprintf("%d", fileInfo.Size()))
  51. // 流式传输文件内容
  52. http.ServeContent(c.Writer, c.Request, fileInfo.Name(), fileInfo.ModTime(), file)
  53. }