2022-02-23 00:06:39 +00:00
|
|
|
package pages
|
|
|
|
|
|
|
|
import (
|
2022-07-16 19:59:06 +00:00
|
|
|
"strings"
|
|
|
|
|
2023-06-10 16:27:20 +00:00
|
|
|
"codeberg.org/rimgo/rimgo/api"
|
|
|
|
"codeberg.org/rimgo/rimgo/utils"
|
2022-02-23 00:06:39 +00:00
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
func HandleEmbed(c *fiber.Ctx) error {
|
|
|
|
utils.SetHeaders(c)
|
|
|
|
c.Set("Cache-Control", "public,max-age=31557600")
|
2023-06-13 13:37:00 +00:00
|
|
|
c.Set("Content-Security-Policy", "default-src 'none'; base-uri 'none'; form-action 'none'; media-src 'self' https://i.imgur.com https://i.stack.imgur.com; style-src 'self'; img-src 'self' https://i.imgur.com https://i.stack.imgur.com; block-all-mixed-content")
|
2022-02-23 00:06:39 +00:00
|
|
|
|
2022-07-22 15:55:22 +00:00
|
|
|
post, err := api.Album{}, error(nil)
|
2022-07-16 19:59:06 +00:00
|
|
|
switch {
|
|
|
|
case strings.HasPrefix(c.Path(), "/a"):
|
2023-01-01 20:12:03 +00:00
|
|
|
post, err = ApiClient.FetchAlbum(c.Params("postID"))
|
2022-07-16 19:59:06 +00:00
|
|
|
case strings.HasPrefix(c.Path(), "/gallery"):
|
2023-01-01 20:12:03 +00:00
|
|
|
post, err = ApiClient.FetchPosts(c.Params("postID"))
|
2022-07-16 19:59:06 +00:00
|
|
|
default:
|
2023-01-01 20:12:03 +00:00
|
|
|
post, err = ApiClient.FetchMedia(c.Params("postID"))
|
2022-07-16 19:59:06 +00:00
|
|
|
}
|
2022-09-10 14:11:17 +00:00
|
|
|
if err != nil && err.Error() == "ratelimited by imgur" {
|
2024-02-05 02:24:55 +00:00
|
|
|
return utils.RenderError(c, 429)
|
2022-09-10 14:11:17 +00:00
|
|
|
}
|
2022-09-13 20:31:55 +00:00
|
|
|
if err != nil && post.Id == "" && strings.Contains(err.Error(), "404") {
|
2024-02-05 02:24:55 +00:00
|
|
|
return utils.RenderError(c, 404)
|
2022-07-16 19:59:06 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-02-23 00:06:39 +00:00
|
|
|
return c.Render("embed", fiber.Map{
|
2022-07-16 19:59:06 +00:00
|
|
|
"post": post,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func HandleGifv(c *fiber.Ctx) error {
|
|
|
|
utils.SetHeaders(c)
|
|
|
|
c.Set("Cache-Control", "public,max-age=31557600")
|
2022-11-18 17:08:18 +00:00
|
|
|
c.Set("Content-Security-Policy", "default-src 'none'; base-uri 'none'; form-action 'none'; media-src 'self'; style-src 'self'; img-src 'self'; block-all-mixed-content")
|
2022-07-16 19:59:06 +00:00
|
|
|
|
|
|
|
return c.Render("gifv", fiber.Map{
|
2022-02-23 00:06:39 +00:00
|
|
|
"id": c.Params("postID"),
|
|
|
|
})
|
2023-06-13 13:37:00 +00:00
|
|
|
}
|