port most routes

This commit is contained in:
orangix
2026-01-19 18:57:06 +01:00
parent 04fbc7f5f4
commit cd4a36c9f7
17 changed files with 310 additions and 213 deletions

View File

@@ -3,12 +3,13 @@ package pages
import (
"crypto/rand"
"fmt"
"net/http"
"strconv"
"strings"
"codeberg.org/rimgo/rimgo/api"
"codeberg.org/rimgo/rimgo/render"
"codeberg.org/rimgo/rimgo/utils"
"github.com/gofiber/fiber/v2"
)
// Cursed function
@@ -33,31 +34,31 @@ func nextInTag(client *api.Client, tagname, sort, page, I string) string {
return tag.Posts[i+1].Link
}
func HandlePost(c *fiber.Ctx) error {
utils.SetHeaders(c)
c.Set("X-Frame-Options", "DENY")
func HandlePost(w http.ResponseWriter, r *http.Request) error {
utils.SetHeaders(w)
w.Header().Set("X-Frame-Options", "DENY")
postId := c.Params("postID")
postId := r.PathValue("postID")
if strings.Contains(postId, "-") {
postId = postId[len(postId)-7:]
}
post, err := api.Album{}, error(nil)
switch {
case strings.HasPrefix(c.Path(), "/a"):
case strings.HasPrefix(r.URL.Path, "/a"):
post, err = ApiClient.FetchAlbum(postId)
case strings.HasPrefix(c.Path(), "/gallery"):
case strings.HasPrefix(r.URL.Path, "/gallery"):
post, err = ApiClient.FetchPosts(postId)
case strings.HasPrefix(c.Path(), "/t"):
case strings.HasPrefix(r.URL.Path, "/t"):
post, err = ApiClient.FetchPosts(postId)
default:
post, err = ApiClient.FetchMedia(postId)
}
if err != nil && err.Error() == "ratelimited by imgur" {
return utils.RenderError(c, 429)
return utils.RenderError(w, r, 429)
}
if err != nil && post.Id == "" && strings.Contains(err.Error(), "404") {
return utils.RenderError(c, 404)
return utils.RenderError(w, r, 404)
}
if err != nil {
return err
@@ -65,13 +66,13 @@ func HandlePost(c *fiber.Ctx) error {
comments := []api.Comment{}
if post.SharedWithCommunity {
c.Set("Cache-Control", "public,max-age=604800")
w.Header().Set("Cache-Control", "public,max-age=604800")
comments, err = ApiClient.FetchComments(postId)
if err != nil {
return err
}
} else {
c.Set("Cache-Control", "public,max-age=31557600")
w.Header().Set("Cache-Control", "public,max-age=31557600")
}
nonce := ""
@@ -82,16 +83,16 @@ func HandlePost(c *fiber.Ctx) error {
nonce = fmt.Sprintf("%x", b)
csp = csp + " 'nonce-" + nonce + "'"
}
c.Set("Content-Security-Policy", csp)
w.Header().Set("Content-Security-Policy", csp)
var next string
tagParam := strings.Split(c.Query("tag"), ".")
tagParam := strings.Split(r.URL.Query().Get("tag"), ".")
if len(tagParam) == 4 {
tag, sort, page, index := tagParam[0], tagParam[1], tagParam[2], tagParam[3]
next = nextInTag(ApiClient, tag, sort, page, index)
}
return c.Render("post", fiber.Map{
return render.Render(w, "post", map[string]any{
"post": post,
"next": next,
"comments": comments,