2022-01-17 20:23:04 +00:00
|
|
|
package pages
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2022-06-05 00:44:54 +00:00
|
|
|
"os"
|
|
|
|
"strings"
|
2022-01-17 20:23:04 +00:00
|
|
|
|
2023-06-10 16:27:20 +00:00
|
|
|
"codeberg.org/rimgo/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")
|
2024-02-05 02:24:55 +00:00
|
|
|
c.Set("Content-Security-Policy", "default-src 'none'; style-src 'self'; img-src 'self'")
|
2022-09-30 20:17:24 +00:00
|
|
|
if strings.HasPrefix(c.Path(), "/stack") {
|
2024-02-05 02:24:55 +00:00
|
|
|
return handleMedia(c, "https://i.stack.imgur.com/"+strings.ReplaceAll(c.Params("baseName"), "stack/", "")+"."+c.Params("extension"))
|
2022-09-30 20:17:24 +00:00
|
|
|
} else {
|
2024-02-05 02:24:55 +00:00
|
|
|
return handleMedia(c, "https://i.imgur.com/"+c.Params("baseName")+"."+c.Params("extension"))
|
2022-09-30 20:17:24 +00:00
|
|
|
}
|
2022-01-28 01:41:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func HandleUserCover(c *fiber.Ctx) error {
|
|
|
|
c.Set("Cache-Control", "public,max-age=604800")
|
2024-02-05 02:24:55 +00:00
|
|
|
c.Set("Content-Security-Policy", "default-src 'none'")
|
|
|
|
return handleMedia(c, "https://imgur.com/user/"+c.Params("userID")+"/cover?maxwidth=2560")
|
|
|
|
}
|
2022-01-28 01:41:10 +00:00
|
|
|
|
|
|
|
func HandleUserAvatar(c *fiber.Ctx) error {
|
|
|
|
c.Set("Cache-Control", "public,max-age=604800")
|
2024-02-05 02:24:55 +00:00
|
|
|
c.Set("Content-Security-Policy", "default-src 'none'")
|
|
|
|
return handleMedia(c, "https://imgur.com/user/"+c.Params("userID")+"/avatar")
|
|
|
|
}
|
2022-01-28 01:41:10 +00:00
|
|
|
|
|
|
|
func handleMedia(c *fiber.Ctx, url string) error {
|
|
|
|
utils.SetHeaders(c)
|
|
|
|
|
2022-10-06 02:04:21 +00:00
|
|
|
if os.Getenv("FORCE_WEBP") == "1" && c.Query("no_webp") == "" && c.Accepts("image/webp") == "image/webp" && !strings.HasPrefix(c.Path(), "/stack") {
|
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-10-06 10:53:24 +00:00
|
|
|
if strings.HasPrefix(c.Path(), "/stack") && strings.Contains(c.OriginalURL(), "?") {
|
2022-10-06 02:24:28 +00:00
|
|
|
url = url + "?" + strings.Split(c.OriginalURL(), "?")[1]
|
|
|
|
}
|
|
|
|
|
2022-06-25 19:23:13 +00:00
|
|
|
req, err := http.NewRequest("GET", url, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-02-05 02:24:55 +00:00
|
|
|
|
2023-05-26 19:45:59 +00:00
|
|
|
utils.SetReqHeaders(req)
|
2022-06-25 19:23:13 +00:00
|
|
|
|
|
|
|
if c.Get("Range") != "" {
|
|
|
|
req.Header.Set("Range", c.Get("Range"))
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := http.DefaultClient.Do(req)
|
2022-01-17 20:23:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-02-05 02:24:55 +00:00
|
|
|
if res.StatusCode == 404 || strings.Contains(res.Request.URL.String(), "error/404") {
|
|
|
|
return utils.RenderError(c, 404)
|
2023-05-26 19:45:59 +00:00
|
|
|
} else if res.StatusCode == 429 {
|
2024-02-05 02:24:55 +00:00
|
|
|
return utils.RenderError(c, 429)
|
2022-05-23 15:30:17 +00:00
|
|
|
}
|
2024-02-05 02:24:55 +00:00
|
|
|
|
2022-06-25 19:23:13 +00:00
|
|
|
c.Set("Accept-Ranges", "bytes")
|
2024-02-05 02:24:55 +00:00
|
|
|
c.Set("Content-Type", res.Header.Get("Content-Type"))
|
2022-06-25 19:23:13 +00:00
|
|
|
c.Set("Content-Length", res.Header.Get("Content-Length"))
|
|
|
|
if res.Header.Get("Content-Range") != "" {
|
|
|
|
c.Set("Content-Range", res.Header.Get("Content-Range"))
|
|
|
|
}
|
2024-02-05 02:24:55 +00:00
|
|
|
|
2022-10-15 00:27:37 +00:00
|
|
|
return c.SendStream(res.Body)
|
2024-02-05 02:24:55 +00:00
|
|
|
}
|