feat: remove authentication requirement for avatar and image upload endpoints;
fix: remove 500 error responses from upload endpoints; fix: return validation error strings instead of error lists; fix: handle invalid avatar upload IDs with 400 Bad Request response; fix: add missing S3Controller to controller initialization; fix: change avatar_upload_id to string type and update validation rules; chore: add license header to smtp.go; refactor: replace manual proxy implementation with httputil.ReverseProxy; fix: inject S3Service dependency into ProfileService; fix: set color and color_grad fields during profile update; fix: correct DTO mapping for profile and settings; fix: check object existence before copying in SaveUpload; fix: adjust profile DTO mapping function for proper pointer handling
This commit is contained in:
@@ -20,56 +20,50 @@ package minioclient
|
||||
import (
|
||||
"easywish/config"
|
||||
"fmt"
|
||||
"io"
|
||||
"maps"
|
||||
"net/http"
|
||||
"net/http/httputil"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func setupGinEndpoint(router *gin.Engine) {
|
||||
|
||||
cfg := config.GetConfig()
|
||||
minioHost := fmt.Sprintf("%s:%d", cfg.MinioHost, cfg.MinioPort)
|
||||
|
||||
proxy := &httputil.ReverseProxy{
|
||||
Director: func(req *http.Request) {
|
||||
path := strings.TrimPrefix(req.URL.Path, "/s3")
|
||||
targetURL := &url.URL{
|
||||
Scheme: "http",
|
||||
Host: minioHost,
|
||||
Path: path,
|
||||
RawQuery: req.URL.RawQuery,
|
||||
}
|
||||
|
||||
req.URL = targetURL
|
||||
req.Host = minioHost
|
||||
req.Header.Set("Host", minioHost)
|
||||
|
||||
req.Header.Del("X-Forwarded-For")
|
||||
req.Header.Del("X-Forwarded-Host")
|
||||
req.Header.Del("X-Forwarded-Proto")
|
||||
},
|
||||
Transport: &http.Transport{
|
||||
ResponseHeaderTimeout: time.Duration(cfg.MinioTimeout),
|
||||
},
|
||||
ErrorHandler: func(w http.ResponseWriter, r *http.Request, err error) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.Write([]byte(`{"error": "Failed to forward request"}`))
|
||||
fmt.Printf("Proxy error: %v\n", err)
|
||||
},
|
||||
}
|
||||
|
||||
s3group := router.Group("/s3")
|
||||
s3group.Any("/*path", func(c *gin.Context) {
|
||||
path := c.Param("path")
|
||||
minioURL := &url.URL{
|
||||
Scheme: "http",
|
||||
Host: minioHost,
|
||||
Path: path,
|
||||
RawQuery: c.Request.URL.RawQuery,
|
||||
}
|
||||
req, err := http.NewRequest(c.Request.Method, minioURL.String(), c.Request.Body); if err != nil {
|
||||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": "Failed to create request"})
|
||||
return
|
||||
}
|
||||
req = req.WithContext(c.Request.Context())
|
||||
maps.Copy(req.Header, c.Request.Header)
|
||||
req.Header.Set("Host", minioHost)
|
||||
client := &http.Client{
|
||||
Timeout: time.Duration(cfg.MinioTimeout),
|
||||
CheckRedirect: func(req *http.Request, via []*http.Request) error {
|
||||
return http.ErrUseLastResponse
|
||||
},
|
||||
}
|
||||
resp, err := client.Do(req); if err != nil {
|
||||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": "Failed to forward request"})
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
for key, values := range resp.Header {
|
||||
for _, value := range values {
|
||||
c.Writer.Header().Add(key, value)
|
||||
}
|
||||
}
|
||||
c.Status(resp.StatusCode)
|
||||
_, err = io.Copy(c.Writer, resp.Body); if err != nil {
|
||||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": "Failed to copy response body"})
|
||||
return
|
||||
}
|
||||
proxy.ServeHTTP(c.Writer, c.Request)
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user