0
Fork 0
mirror of https://codeberg.org/SafeTwitch/safetwitch-backend.git synced 2024-12-22 05:02:58 -05:00

Add image proxy

This commit is contained in:
dragongoose 2023-05-22 18:11:58 -04:00
parent 129d731a95
commit d6d820cdc1
No known key found for this signature in database
GPG key ID: 50DB99B921579009
2 changed files with 33 additions and 0 deletions

31
routes/proxy/proxy.go Normal file
View file

@ -0,0 +1,31 @@
package proxy
import (
b64 "encoding/base64"
"net/http"
"github.com/gin-gonic/gin"
)
func Routes(route *gin.Engine) {
auth := route.Group("/proxy")
auth.GET("/img/:url", func(context *gin.Context) {
decodedUrl, err := b64.StdEncoding.DecodeString(context.Param("url"))
if err != nil {
context.Error(err)
}
imageResp, err := http.Get(string(decodedUrl))
if err != nil {
context.Error(err)
}
reader := imageResp.Body
contentLength := imageResp.ContentLength
contentType := imageResp.Header.Get("Content-Type")
context.DataFromReader(200, contentLength, contentType, reader, map[string]string{})
context.JSON(imageResp.StatusCode, imageResp.Body)
})
}

View file

@ -1,6 +1,7 @@
package routes
import (
"safetwitch-backend/routes/proxy"
"safetwitch-backend/routes/users"
"github.com/gin-gonic/gin"
@ -8,4 +9,5 @@ import (
func SetRoutes(router *gin.Engine) {
users.Routes(router)
proxy.Routes(router)
}