rimgo/pages/post.go

61 lines
1.4 KiB
Go
Raw Normal View History

package pages
import (
2022-07-22 16:20:51 +00:00
"crypto/rand"
"fmt"
2022-05-23 15:10:50 +00:00
"strings"
"codeberg.org/video-prize-ranch/rimgo/api"
"codeberg.org/video-prize-ranch/rimgo/utils"
"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-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"):
post, err = api.FetchAlbum(c.Params("postID"))
case strings.HasPrefix(c.Path(), "/gallery"):
post, err = api.FetchPosts(c.Params("postID"))
2022-05-23 15:10:50 +00:00
default:
post, err = api.FetchMedia(c.Params("postID"))
}
2022-05-23 15:30:17 +00:00
if post.Id == "" || (err != nil && strings.Contains(err.Error(), "404")) {
c.Status(404)
return c.Render("errors/404", nil)
}
2022-07-22 16:20:51 +00:00
if err != nil {
return err
}
2022-07-22 15:55:22 +00:00
comments := []api.Comment{}
if post.SharedWithCommunity {
c.Set("Cache-Control", "public,max-age=604800")
comments, err = api.FetchComments(c.Params("postID"))
if err != nil {
return err
}
} else {
c.Set("Cache-Control", "public,max-age=31557600")
}
2022-07-22 16:20:51 +00:00
nonce := ""
csp := "default-src 'none'; media-src 'self'; img-src 'self'; font-src 'self'; manifest-src 'self'; block-all-mixed-content; style-src 'self'"
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)
return c.Render("post", fiber.Map{
2022-07-22 16:20:51 +00:00
"post": post,
"comments": comments,
2022-07-22 16:20:51 +00:00
"nonce": nonce,
})
2022-07-22 16:20:51 +00:00
}