| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- package daytask
- import (
- "app/commons/config"
- "github.com/gin-gonic/gin"
- )
- // UploadFile 上传文件到MinIO
- func (s *Server) UploadFile(c *gin.Context) {
- ctx := s.FromContext(c)
- // 获取上传的文件
- file, err := c.FormFile("file")
- if err != nil {
- ctx.Fail("file_required")
- return
- }
- // 检查文件大小 (最大10MB)
- if file.Size > 10*1024*1024 {
- ctx.Fail("file_too_large")
- return
- }
- // 检查文件类型
- contentType := file.Header.Get("Content-Type")
- allowedTypes := map[string]bool{
- "image/jpeg": true,
- "image/png": true,
- "image/gif": true,
- "image/webp": true,
- "image/bmp": true,
- }
- if !allowedTypes[contentType] {
- ctx.Fail("invalid_file_type")
- return
- }
- // 上传到存储(MinIO/S3)
- url, err := config.UploadFileToStorage(file)
- if err != nil {
- ctx.Fail("upload_failed")
- return
- }
- ctx.OK(gin.H{
- "url": url,
- })
- }
|