package handler import ( "io" "net/http" "strings" "github.com/gin-gonic/gin" "github.com/zhilv666/go-gin-scaffold/pkg/common" ) func Proxy(c *gin.Context) { _url := c.Query("url") if _url == "" { common.Fail(c, "missing url") return } // 请求目标 URL resp, err := http.Get(_url) if err != nil { common.Fail(c, "unable to fetch image") return } defer resp.Body.Close() // 检查 Content-Type 是否为图片 contentType := resp.Header.Get("Content-Type") if !strings.HasPrefix(contentType, "image/") { common.Fail(c, "not an image file") return } // 设置响应 Content-Type c.Writer.Header().Set("Content-Type", contentType) // 将图片数据直接写入响应 _, err = io.Copy(c.Writer, resp.Body) if err != nil { common.Fail(c, "failed to write image") return } }