mirror of
https://codeberg.org/video-prize-ranch/rimgo.git
synced 2026-01-28 01:21:16 +00:00
port most routes
This commit is contained in:
@@ -1,25 +1,38 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"codeberg.org/rimgo/rimgo/render"
|
||||
"codeberg.org/rimgo/rimgo/static"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
func RenderError(c *fiber.Ctx, code int) error {
|
||||
if !strings.Contains(c.Get("Accept"), "html") && c.Params("extension") != "" {
|
||||
func RenderError(w http.ResponseWriter, r *http.Request, code int) error {
|
||||
if !Accepts(r, "text/html") && r.PathValue("extension") != "" {
|
||||
codeStr := "generic"
|
||||
if code != 0 {
|
||||
codeStr = strconv.Itoa(code)
|
||||
}
|
||||
img, _ := ReadFile("img/error-"+codeStr+".png", static.GetFiles())
|
||||
c.Set("Content-Type", "image/png")
|
||||
return c.Status(code).Send(img)
|
||||
w.Header().Set("Content-Type", "image/png")
|
||||
w.WriteHeader(code)
|
||||
file, _ := static.GetFiles().Open("img/error-" + codeStr + ".png")
|
||||
defer file.Close()
|
||||
_, err := io.Copy(w, file)
|
||||
if err != nil {
|
||||
// panic on error to avoid a loop
|
||||
panic(err)
|
||||
}
|
||||
} else {
|
||||
return c.Status(code).Render("errors/"+strconv.Itoa(code), fiber.Map{
|
||||
"path": c.Path(),
|
||||
w.WriteHeader(code)
|
||||
err := render.Render(w, "errors/"+strconv.Itoa(code), map[string]any{
|
||||
"path": r.URL.Path,
|
||||
})
|
||||
if err != nil {
|
||||
// panic on error to avoid a loop
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user