2022-02-17 21:07:15 +00:00
|
|
|
package pages
|
|
|
|
|
|
|
|
import (
|
2022-07-22 16:20:51 +00:00
|
|
|
"crypto/rand"
|
|
|
|
"fmt"
|
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"
|
|
|
|
)
|
|
|
|
|
|
|
|
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" {
|
2023-06-10 16:04:29 +00:00
|
|
|
return c.Status(429).Render("errors/429", fiber.Map{
|
|
|
|
"path": c.Path(),
|
|
|
|
})
|
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") {
|
2022-09-10 14:11:17 +00:00
|
|
|
return c.Status(404).Render("errors/404", nil)
|
2022-02-23 00:06:39 +00:00
|
|
|
}
|
2022-09-13 20:31:55 +00:00
|
|
|
if err != nil {
|
2022-02-17 21:07:15 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-07-22 15:55:22 +00:00
|
|
|
comments := []api.Comment{}
|
2022-02-17 21:07:15 +00:00
|
|
|
if post.SharedWithCommunity {
|
|
|
|
c.Set("Cache-Control", "public,max-age=604800")
|
2023-01-01 20:12:03 +00:00
|
|
|
comments, err = ApiClient.FetchComments(c.Params("postID"))
|
2022-02-17 21:07:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
c.Set("Cache-Control", "public,max-age=31557600")
|
|
|
|
}
|
|
|
|
|
2022-07-22 16:20:51 +00:00
|
|
|
nonce := ""
|
2022-11-18 17:08:18 +00:00
|
|
|
csp := "default-src 'none'; frame-ancestors 'none'; base-uri 'none'; form-action 'none'; media-src 'self'; img-src 'self'; manifest-src 'self'; block-all-mixed-content; style-src 'self'"
|
2022-07-22 16:20:51 +00:00
|
|
|
if len(post.Tags) != 0 {
|
|
|
|
b := make([]byte, 8)
|
|
|
|
rand.Read(b)
|
|
|
|
nonce = fmt.Sprintf("%x", b)
|
|
|
|
csp = csp + " 'nonce-" + nonce + "'"
|
|
|
|
}
|
|
|
|
c.Set("Content-Security-Policy", csp)
|
|
|
|
|
2022-02-17 21:07:15 +00:00
|
|
|
return c.Render("post", fiber.Map{
|
2022-07-22 16:20:51 +00:00
|
|
|
"post": post,
|
2022-02-17 21:07:15 +00:00
|
|
|
"comments": comments,
|
2022-07-22 16:20:51 +00:00
|
|
|
"nonce": nonce,
|
2022-02-17 21:07:15 +00:00
|
|
|
})
|
2022-07-22 16:20:51 +00:00
|
|
|
}
|