Errors in image format (closes #165)

This commit is contained in:
video-prize-ranch 2024-02-04 21:24:55 -05:00
parent 6d528c7d0a
commit 1579e59dca
No known key found for this signature in database
11 changed files with 73 additions and 64 deletions

18
main.go
View File

@ -52,14 +52,7 @@ func main() {
code = e.Code code = e.Code
} }
err = ctx.Status(code).Render("errors/error", fiber.Map{ return utils.RenderError(ctx, code)
"err": err,
})
if err != nil {
return ctx.Status(fiber.StatusInternalServerError).SendString("Internal Server Error")
}
return nil
}, },
}) })
@ -77,14 +70,23 @@ func main() {
app.Get("/errors/429", func(c *fiber.Ctx) error { app.Get("/errors/429", func(c *fiber.Ctx) error {
return c.Render("errors/429", nil) return c.Render("errors/429", nil)
}) })
app.Get("/errors/429/img", func(c *fiber.Ctx) error {
return c.Redirect("/static/img/error-429.png")
})
app.Get("/errors/404", func(c *fiber.Ctx) error { app.Get("/errors/404", func(c *fiber.Ctx) error {
return c.Render("errors/404", nil) return c.Render("errors/404", nil)
}) })
app.Get("/errors/404/img", func(c *fiber.Ctx) error {
return c.Redirect("/static/img/error-404.png")
})
app.Get("/errors/error", func(c *fiber.Ctx) error { app.Get("/errors/error", func(c *fiber.Ctx) error {
return c.Render("errors/error", fiber.Map{ return c.Render("errors/error", fiber.Map{
"err": "Test error", "err": "Test error",
}) })
}) })
app.Get("/errors/error/img", func(c *fiber.Ctx) error {
return c.Redirect("/static/img/error-generic.png")
})
} else { } else {
app.Use("/static", filesystem.New(filesystem.Config{ app.Use("/static", filesystem.New(filesystem.Config{
MaxAge: 2592000, MaxAge: 2592000,

View File

@ -23,10 +23,10 @@ func HandleEmbed(c *fiber.Ctx) error {
post, err = ApiClient.FetchMedia(c.Params("postID")) post, err = ApiClient.FetchMedia(c.Params("postID"))
} }
if err != nil && err.Error() == "ratelimited by imgur" { if err != nil && err.Error() == "ratelimited by imgur" {
return c.Status(429).Render("errors/429", nil) return utils.RenderError(c, 429)
} }
if err != nil && post.Id == "" && strings.Contains(err.Error(), "404") { if err != nil && post.Id == "" && strings.Contains(err.Error(), "404") {
return c.Status(404).Render("errors/404", nil) return utils.RenderError(c, 404)
} }
if err != nil { if err != nil {
return err return err

View File

@ -11,22 +11,25 @@ import (
func HandleMedia(c *fiber.Ctx) error { func HandleMedia(c *fiber.Ctx) error {
c.Set("Cache-Control", "public,max-age=31557600") c.Set("Cache-Control", "public,max-age=31557600")
c.Set("Content-Security-Policy", "default-src 'none'; style-src 'self'; img-src 'self'")
if strings.HasPrefix(c.Path(), "/stack") { if strings.HasPrefix(c.Path(), "/stack") {
return handleMedia(c, "https://i.stack.imgur.com/" + strings.ReplaceAll(c.Params("baseName"), "stack/", "") + "." + c.Params("extension")) return handleMedia(c, "https://i.stack.imgur.com/"+strings.ReplaceAll(c.Params("baseName"), "stack/", "")+"."+c.Params("extension"))
} else { } else {
return handleMedia(c, "https://i.imgur.com/" + c.Params("baseName") + "." + c.Params("extension")) return handleMedia(c, "https://i.imgur.com/"+c.Params("baseName")+"."+c.Params("extension"))
} }
} }
func HandleUserCover(c *fiber.Ctx) error { func HandleUserCover(c *fiber.Ctx) error {
c.Set("Cache-Control", "public,max-age=604800") c.Set("Cache-Control", "public,max-age=604800")
return handleMedia(c, "https://imgur.com/user/" + c.Params("userID") + "/cover?maxwidth=2560") c.Set("Content-Security-Policy", "default-src 'none'")
}; return handleMedia(c, "https://imgur.com/user/"+c.Params("userID")+"/cover?maxwidth=2560")
}
func HandleUserAvatar(c *fiber.Ctx) error { func HandleUserAvatar(c *fiber.Ctx) error {
c.Set("Cache-Control", "public,max-age=604800") c.Set("Cache-Control", "public,max-age=604800")
return handleMedia(c, "https://imgur.com/user/" + c.Params("userID") + "/avatar") c.Set("Content-Security-Policy", "default-src 'none'")
}; return handleMedia(c, "https://imgur.com/user/"+c.Params("userID")+"/avatar")
}
func handleMedia(c *fiber.Ctx, url string) error { func handleMedia(c *fiber.Ctx, url string) error {
utils.SetHeaders(c) utils.SetHeaders(c)
@ -45,7 +48,7 @@ func handleMedia(c *fiber.Ctx, url string) error {
if err != nil { if err != nil {
return err return err
} }
utils.SetReqHeaders(req) utils.SetReqHeaders(req)
if c.Get("Range") != "" { if c.Get("Range") != "" {
@ -57,23 +60,18 @@ func handleMedia(c *fiber.Ctx, url string) error {
return err return err
} }
c.Status(res.StatusCode) if res.StatusCode == 404 || strings.Contains(res.Request.URL.String(), "error/404") {
if res.StatusCode == 404 { return utils.RenderError(c, 404)
return c.Render("errors/404", fiber.Map{
"path": c.Path(),
})
} else if res.StatusCode == 429 { } else if res.StatusCode == 429 {
return c.Render("errors/429", fiber.Map{ return utils.RenderError(c, 429)
"path": c.Path(),
})
} }
c.Set("Accept-Ranges", "bytes") c.Set("Accept-Ranges", "bytes")
c.Set("Content-Type", res.Header.Get("Content-Type")); c.Set("Content-Type", res.Header.Get("Content-Type"))
c.Set("Content-Length", res.Header.Get("Content-Length")) c.Set("Content-Length", res.Header.Get("Content-Length"))
if res.Header.Get("Content-Range") != "" { if res.Header.Get("Content-Range") != "" {
c.Set("Content-Range", res.Header.Get("Content-Range")) c.Set("Content-Range", res.Header.Get("Content-Range"))
} }
return c.SendStream(res.Body) return c.SendStream(res.Body)
} }

View File

@ -26,14 +26,12 @@ func HandlePost(c *fiber.Ctx) error {
post, err = ApiClient.FetchMedia(c.Params("postID")) post, err = ApiClient.FetchMedia(c.Params("postID"))
} }
if err != nil && err.Error() == "ratelimited by imgur" { if err != nil && err.Error() == "ratelimited by imgur" {
return c.Status(429).Render("errors/429", fiber.Map{ return utils.RenderError(c, 429)
"path": c.Path(),
})
} }
if err != nil && post.Id == "" && strings.Contains(err.Error(), "404") { if err != nil && post.Id == "" && strings.Contains(err.Error(), "404") {
return c.Status(404).Render("errors/404", nil) return utils.RenderError(c, 404)
} }
if err != nil { if err != nil {
return err return err
} }

View File

@ -9,6 +9,7 @@ import (
func HandlePrivacy(c *fiber.Ctx) error { func HandlePrivacy(c *fiber.Ctx) error {
utils.SetHeaders(c) utils.SetHeaders(c)
c.Set("X-Frame-Options", "DENY") c.Set("X-Frame-Options", "DENY")
c.Set("Content-Security-Policy", "default-src 'none'; form-action 'self'; style-src 'self'; img-src 'self'; manifest-src 'self'; block-all-mixed-content")
return c.Render("privacy", fiber.Map{ return c.Render("privacy", fiber.Map{
"config": utils.Config, "config": utils.Config,

View File

@ -25,21 +25,19 @@ func HandleTag(c *fiber.Ctx) error {
tag, err := ApiClient.FetchTag(c.Params("tag"), c.Query("sort"), page) tag, err := ApiClient.FetchTag(c.Params("tag"), c.Query("sort"), page)
if err != nil && err.Error() == "ratelimited by imgur" { if err != nil && err.Error() == "ratelimited by imgur" {
return c.Status(429).Render("errors/429", fiber.Map{ return utils.RenderError(c, 429)
"path": c.Path(),
})
} }
if err != nil { if err != nil {
return err return err
} }
if tag.Display == "" { if tag.Display == "" {
return c.Status(404).Render("errors/404", nil) return utils.RenderError(c, 404)
} }
return c.Render("tag", fiber.Map{ return c.Render("tag", fiber.Map{
"tag": tag, "tag": tag,
"page": page, "page": page,
"nextPage": pageNumber + 1, "nextPage": pageNumber + 1,
"prevPage": pageNumber - 1, "prevPage": pageNumber - 1,
}) })
} }

View File

@ -25,23 +25,19 @@ func HandleUser(c *fiber.Ctx) error {
user, err := ApiClient.FetchUser(c.Params("userID")) user, err := ApiClient.FetchUser(c.Params("userID"))
if err != nil && err.Error() == "ratelimited by imgur" { if err != nil && err.Error() == "ratelimited by imgur" {
return c.Status(429).Render("errors/429", fiber.Map{ return utils.RenderError(c, 429)
"path": c.Path(),
})
} }
if err != nil { if err != nil {
return err return err
} }
if user.Username == "" { if user.Username == "" {
return c.Status(404).Render("errors/404", nil) return utils.RenderError(c, 404)
} }
submissions, err := ApiClient.FetchSubmissions(c.Params("userID"), "newest", page) submissions, err := ApiClient.FetchSubmissions(c.Params("userID"), "newest", page)
if err != nil && err.Error() == "ratelimited by imgur" { if err != nil && err.Error() == "ratelimited by imgur" {
c.Status(429) c.Status(429)
return c.Render("errors/429", fiber.Map{ return utils.RenderError(c, 429)
"path": c.Path(),
})
} }
if err != nil { if err != nil {
return err return err
@ -64,23 +60,19 @@ func HandleUserComments(c *fiber.Ctx) error {
user, err := ApiClient.FetchUser(c.Params("userID")) user, err := ApiClient.FetchUser(c.Params("userID"))
if err != nil && err.Error() == "ratelimited by imgur" { if err != nil && err.Error() == "ratelimited by imgur" {
return c.Status(429).Render("errors/429", fiber.Map{ return utils.RenderError(c, 429)
"path": c.Path(),
})
} }
if err != nil { if err != nil {
return err return err
} }
if user.Username == "" { if user.Username == "" {
return c.Status(404).Render("errors/404", nil) return utils.RenderError(c, 404)
} }
comments, err := ApiClient.FetchUserComments(c.Params("userID")) comments, err := ApiClient.FetchUserComments(c.Params("userID"))
if err != nil && err.Error() == "ratelimited by imgur" { if err != nil && err.Error() == "ratelimited by imgur" {
c.Status(429) c.Status(429)
return c.Render("errors/429", fiber.Map{ return utils.RenderError(c, 429)
"path": c.Path(),
})
} }
if err != nil { if err != nil {
return err return err
@ -110,23 +102,18 @@ func HandleUserFavorites(c *fiber.Ctx) error {
user, err := ApiClient.FetchUser(c.Params("userID")) user, err := ApiClient.FetchUser(c.Params("userID"))
if err != nil && err.Error() == "ratelimited by imgur" { if err != nil && err.Error() == "ratelimited by imgur" {
return c.Status(429).Render("errors/429", fiber.Map{ return utils.RenderError(c, 429)
"path": c.Path(),
})
} }
if err != nil { if err != nil {
return err return err
} }
if user.Username == "" { if user.Username == "" {
return c.Status(404).Render("errors/404", nil) return utils.RenderError(c, 404)
} }
favorites, err := ApiClient.FetchUserFavorites(c.Params("userID"), "newest", page) favorites, err := ApiClient.FetchUserFavorites(c.Params("userID"), "newest", page)
if err != nil && err.Error() == "ratelimited by imgur" { if err != nil && err.Error() == "ratelimited by imgur" {
c.Status(429) return utils.RenderError(c, 429)
return c.Render("errors/429", fiber.Map{
"path": c.Path(),
})
} }
if err != nil { if err != nil {
return err return err

BIN
static/img/error-404.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

BIN
static/img/error-429.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 19 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

25
utils/error.go Normal file
View File

@ -0,0 +1,25 @@
package utils
import (
"strconv"
"strings"
"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") {
codeStr := "generic"
if code != 0 {
codeStr = strconv.Itoa(code)
}
img, _ := static.GetFiles().ReadFile("img/error-" + codeStr + ".png")
c.Set("Content-Type", "image/png")
return c.Status(code).Send(img)
} else {
return c.Status(code).Render("errors/" + strconv.Itoa(code), fiber.Map{
"path": c.Path(),
})
}
}