2022-02-17 21:07:15 +00:00
|
|
|
package pages
|
|
|
|
|
|
|
|
import (
|
2024-02-05 22:47:04 +00:00
|
|
|
"strconv"
|
2022-05-23 15:10:50 +00:00
|
|
|
"strings"
|
2022-02-23 00:06:39 +00:00
|
|
|
|
2023-06-10 16:27:20 +00:00
|
|
|
"codeberg.org/rimgo/rimgo/api"
|
|
|
|
"codeberg.org/rimgo/rimgo/utils"
|
2022-02-17 21:07:15 +00:00
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
)
|
|
|
|
|
2024-02-05 22:47:04 +00:00
|
|
|
// Cursed function
|
|
|
|
func nextInTag(client *api.Client, tagname, sort, page, I string) string {
|
|
|
|
i, err := strconv.Atoi(I)
|
|
|
|
if err != nil || i < 0 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
tag, err := client.FetchTag(tagname, sort, page)
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
if i >= len(tag.Posts)-1 {
|
|
|
|
pageNumber, _ := strconv.Atoi(page)
|
|
|
|
tagn, err := client.FetchTag(tagname, sort, strconv.Itoa(pageNumber+1))
|
2024-02-07 01:13:29 +00:00
|
|
|
// Check length - Imgur will not return an error if there are no more posts and you request the next page
|
|
|
|
if err != nil || len(tagn.Posts) < 1 {
|
2024-02-05 22:47:04 +00:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return tagn.Posts[0].Link
|
|
|
|
}
|
|
|
|
return tag.Posts[i+1].Link
|
|
|
|
}
|
|
|
|
|
2022-02-17 21:07:15 +00:00
|
|
|
func HandlePost(c *fiber.Ctx) error {
|
|
|
|
utils.SetHeaders(c)
|
2022-07-16 20:02:59 +00:00
|
|
|
c.Set("X-Frame-Options", "DENY")
|
2022-02-17 21:07:15 +00:00
|
|
|
|
2022-07-22 15:55:22 +00:00
|
|
|
post, err := api.Album{}, error(nil)
|
2022-05-23 15:10:50 +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-05-23 15:10:50 +00:00
|
|
|
case strings.HasPrefix(c.Path(), "/gallery"):
|
2023-01-01 20:12:03 +00:00
|
|
|
post, err = ApiClient.FetchPosts(c.Params("postID"))
|
2023-06-29 22:23:14 +00:00
|
|
|
case strings.HasPrefix(c.Path(), "/t"):
|
|
|
|
post, err = ApiClient.FetchPosts(c.Params("postID"))
|
2022-05-23 15:10:50 +00:00
|
|
|
default:
|
2023-01-01 20:12:03 +00:00
|
|
|
post, err = ApiClient.FetchMedia(c.Params("postID"))
|
2022-05-23 15:10:50 +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-02-23 00:06:39 +00:00
|
|
|
}
|
2024-02-05 02:24:55 +00:00
|
|
|
if err != nil {
|
2022-02-17 21:07:15 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-05-27 14:42:14 +00:00
|
|
|
c.Set("Cache-Control", "public,max-age=31557600")
|
2022-02-17 21:07:15 +00:00
|
|
|
|
2023-06-13 13:37:00 +00:00
|
|
|
csp := "default-src 'none'; frame-ancestors 'none'; base-uri 'none'; form-action 'self'; media-src 'self' https://i.imgur.com https://i.stack.imgur.com; img-src 'self' https://i.imgur.com https://i.stack.imgur.com; manifest-src 'self'; block-all-mixed-content; style-src 'self'"
|
2022-07-22 16:20:51 +00:00
|
|
|
c.Set("Content-Security-Policy", csp)
|
|
|
|
|
2024-02-05 22:47:04 +00:00
|
|
|
var next string
|
|
|
|
tagParam := strings.Split(c.Query("tag"), ".")
|
|
|
|
if len(tagParam) == 4 {
|
|
|
|
tag, sort, page, index := tagParam[0], tagParam[1], tagParam[2], tagParam[3]
|
|
|
|
next = nextInTag(ApiClient, tag, sort, page, index)
|
|
|
|
}
|
|
|
|
|
2022-02-17 21:07:15 +00:00
|
|
|
return c.Render("post", fiber.Map{
|
2022-07-22 16:20:51 +00:00
|
|
|
"post": post,
|
2024-02-05 22:47:04 +00:00
|
|
|
"next": next,
|
2022-02-17 21:07:15 +00:00
|
|
|
})
|
2022-07-22 16:20:51 +00:00
|
|
|
}
|