rimgo/pages/embed.go

48 lines
1.4 KiB
Go
Raw Normal View History

package pages
import (
"strings"
2023-06-10 16:27:20 +00:00
"codeberg.org/rimgo/rimgo/api"
"codeberg.org/rimgo/rimgo/utils"
"github.com/gofiber/fiber/v2"
)
func HandleEmbed(c *fiber.Ctx) error {
utils.SetHeaders(c)
c.Set("Cache-Control", "public,max-age=31557600")
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-22 15:55:22 +00:00
post, err := api.Album{}, error(nil)
switch {
case strings.HasPrefix(c.Path(), "/a"):
2023-01-01 20:12:03 +00:00
post, err = ApiClient.FetchAlbum(c.Params("postID"))
case strings.HasPrefix(c.Path(), "/gallery"):
2023-01-01 20:12:03 +00:00
post, err = ApiClient.FetchPosts(c.Params("postID"))
default:
2023-01-01 20:12:03 +00:00
post, err = ApiClient.FetchMedia(c.Params("postID"))
}
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)
}
if err != nil {
return err
}
return c.Render("embed", fiber.Map{
"post": post,
})
}
func HandleGifv(c *fiber.Ctx) error {
utils.SetHeaders(c)
c.Set("Cache-Control", "public,max-age=31557600")
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")
return c.Render("gifv", fiber.Map{
"id": c.Params("postID"),
})
}