2022-01-17 20:23:04 +00:00
|
|
|
package pages
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2022-06-05 00:44:54 +00:00
|
|
|
"os"
|
2022-01-17 20:48:24 +00:00
|
|
|
"strconv"
|
2022-06-05 00:44:54 +00:00
|
|
|
"strings"
|
2022-01-17 20:23:04 +00:00
|
|
|
|
2022-01-28 01:41:10 +00:00
|
|
|
"codeberg.org/video-prize-ranch/rimgo/utils"
|
2022-01-17 20:23:04 +00:00
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
func HandleMedia(c *fiber.Ctx) error {
|
2022-01-28 01:41:10 +00:00
|
|
|
c.Set("Cache-Control", "public,max-age=31557600")
|
|
|
|
return handleMedia(c, "https://i.imgur.com/" + c.Params("baseName") + "." + c.Params("extension"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func HandleUserCover(c *fiber.Ctx) error {
|
|
|
|
c.Set("Cache-Control", "public,max-age=604800")
|
|
|
|
return handleMedia(c, "https://imgur.com/user/" + c.Params("userID") + "/cover?maxwidth=2560")
|
|
|
|
};
|
|
|
|
|
|
|
|
func HandleUserAvatar(c *fiber.Ctx) error {
|
|
|
|
c.Set("Cache-Control", "public,max-age=604800")
|
|
|
|
return handleMedia(c, "https://imgur.com/user/" + c.Params("userID") + "/avatar")
|
|
|
|
};
|
|
|
|
|
|
|
|
func handleMedia(c *fiber.Ctx, url string) error {
|
|
|
|
utils.SetHeaders(c)
|
2022-04-10 15:21:41 +00:00
|
|
|
c.Set("Content-Security-Policy", "default-src 'none'; media-src 'self'; style-src 'self'; img-src 'self'; font-src 'self'; manifest-src 'self'; block-all-mixed-content")
|
2022-01-28 01:41:10 +00:00
|
|
|
|
2022-06-05 00:48:28 +00:00
|
|
|
if os.Getenv("FORCE_WEBP") == "1" && c.Query("no_webp") == "" && c.Accepts("image/webp") == "image/webp" {
|
2022-06-05 00:44:54 +00:00
|
|
|
url = strings.ReplaceAll(url, ".png", ".webp")
|
|
|
|
url = strings.ReplaceAll(url, ".jpg", ".webp")
|
|
|
|
url = strings.ReplaceAll(url, ".jpeg", ".webp")
|
|
|
|
}
|
|
|
|
|
2022-01-28 01:41:10 +00:00
|
|
|
res, err := http.Get(url)
|
2022-01-17 20:23:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-05-23 15:30:17 +00:00
|
|
|
if res.StatusCode == 404 {
|
|
|
|
c.Status(404)
|
|
|
|
return c.Render("errors/404", nil)
|
|
|
|
}
|
|
|
|
|
2022-01-17 20:23:04 +00:00
|
|
|
c.Set("Content-Type", res.Header.Get("Content-Type"));
|
2022-01-17 20:48:24 +00:00
|
|
|
contentLen, _ := strconv.Atoi(res.Header.Get("Content-Length"))
|
2022-01-28 01:41:10 +00:00
|
|
|
return c.SendStream(res.Body, contentLen)
|
2022-01-17 20:23:04 +00:00
|
|
|
}
|