file.go 881 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package daytask
  2. import (
  3. "app/commons/config"
  4. "github.com/gin-gonic/gin"
  5. )
  6. // UploadFile 上传文件到MinIO
  7. func (s *Server) UploadFile(c *gin.Context) {
  8. ctx := s.FromContext(c)
  9. // 获取上传的文件
  10. file, err := c.FormFile("file")
  11. if err != nil {
  12. ctx.Fail("file_required")
  13. return
  14. }
  15. // 检查文件大小 (最大10MB)
  16. if file.Size > 10*1024*1024 {
  17. ctx.Fail("file_too_large")
  18. return
  19. }
  20. // 检查文件类型
  21. contentType := file.Header.Get("Content-Type")
  22. allowedTypes := map[string]bool{
  23. "image/jpeg": true,
  24. "image/png": true,
  25. "image/gif": true,
  26. "image/webp": true,
  27. "image/bmp": true,
  28. }
  29. if !allowedTypes[contentType] {
  30. ctx.Fail("invalid_file_type")
  31. return
  32. }
  33. // 上传到存储(MinIO/S3)
  34. url, err := config.UploadFileToStorage(file)
  35. if err != nil {
  36. ctx.Fail("upload_failed")
  37. return
  38. }
  39. ctx.OK(gin.H{
  40. "url": url,
  41. })
  42. }