From 373bf5c786fe4683531f275674e0068192c56b5a Mon Sep 17 00:00:00 2001 From: Wim Brand Date: Wed, 1 Apr 2026 19:39:42 +0200 Subject: [PATCH] Fix filesystem error handler --- webcontroller/filesystem.go | 25 +++++++++++++++---------- webcontroller/style.go | 2 +- webcontroller/templates.go | 18 +++++++++--------- 3 files changed, 25 insertions(+), 20 deletions(-) diff --git a/webcontroller/filesystem.go b/webcontroller/filesystem.go index c03c0f3..6fa9070 100644 --- a/webcontroller/filesystem.go +++ b/webcontroller/filesystem.go @@ -1,11 +1,13 @@ package webcontroller import ( + "errors" "fmt" "net/http" "strings" "fornaxian.tech/log" + "fornaxian.tech/pixeldrain_api_client/pixelapi" "fornaxian.tech/util" "github.com/julienschmidt/httprouter" ) @@ -25,16 +27,19 @@ func (wc *WebController) serveDirectory(w http.ResponseWriter, r *http.Request, node, err := td.PixelAPI.GetFilesystemPath(path) if err != nil { - if err.Error() == "not_found" || err.Error() == "path_not_found" { - wc.serveNotFound(w, r) - } else if err.Error() == "forbidden" { - wc.serveForbidden(w, r) - } else if err.Error() == "authentication_required" { - http.Redirect(w, r, "/login", http.StatusSeeOther) - } else if err.Error() == "unavailable_for_legal_reasons" { - wc.serveUnavailableForLegalReasons(w, r) - } else if err.Error() == "permission_denied" { - wc.serveForbidden(w, r) + if apiErr, ok := errors.AsType[pixelapi.Error](err); ok { + switch apiErr.StatusCode { + case "not_found", "path_not_found": + wc.serveNotFound(w, r) + case "forbidden": + wc.serveForbidden(w, r) + case "authentication_required": + http.Redirect(w, r, "/login", http.StatusSeeOther) + case "unavailable_for_legal_reasons": + wc.serveUnavailableForLegalReasons(w, r) + case "permission_denied": + wc.serveForbidden(w, r) + } } else { log.Error("Failed to get path: %s", err) wc.templates.Run(w, r, "500", td) diff --git a/webcontroller/style.go b/webcontroller/style.go index ba3a415..b1ce8f7 100644 --- a/webcontroller/style.go +++ b/webcontroller/style.go @@ -262,7 +262,7 @@ func (s styleSheet) String() string { s.BodyBackground.CSS(), s.BodyText.CSS(), s.Separator.CSS(), - s.BodyColor.WithAlpha(0.7).CSS(), // shaded_background + s.BodyColor.WithAlpha(0.9).CSS(), // shaded_background s.CardColor.CSS(), s.Chart1.CSS(), s.Chart2.CSS(), diff --git a/webcontroller/templates.go b/webcontroller/templates.go index 00a985a..134bdda 100644 --- a/webcontroller/templates.go +++ b/webcontroller/templates.go @@ -33,7 +33,7 @@ type TemplateData struct { Title string OGData ogData - Other interface{} + Other any URLQuery url.Values } @@ -232,15 +232,15 @@ func (tm *TemplateManager) pageNr(s string) (nr int) { } return nr } -func (tm *TemplateManager) add(a, b interface{}) float64 { return toFloat(a) + toFloat(b) } -func (tm *TemplateManager) sub(a, b interface{}) float64 { return toFloat(a) - toFloat(b) } -func (tm *TemplateManager) mul(a, b interface{}) float64 { return toFloat(a) * toFloat(b) } -func (tm *TemplateManager) div(a, b interface{}) float64 { return toFloat(a) / toFloat(b) } +func (tm *TemplateManager) add(a, b any) float64 { return toFloat(a) + toFloat(b) } +func (tm *TemplateManager) sub(a, b any) float64 { return toFloat(a) - toFloat(b) } +func (tm *TemplateManager) mul(a, b any) float64 { return toFloat(a) * toFloat(b) } +func (tm *TemplateManager) div(a, b any) float64 { return toFloat(a) / toFloat(b) } -func (tm *TemplateManager) formatData(i interface{}) string { +func (tm *TemplateManager) formatData(i any) string { return util.FormatData(detectInt(i)) } -func (tm *TemplateManager) formatDataBits(i interface{}) string { +func (tm *TemplateManager) formatDataBits(i any) string { var size = detectInt(i) * 8 var sizef = float64(size) @@ -302,7 +302,7 @@ func (tm *TemplateManager) noEscape(t string) template.HTML { return template.HT func (tm *TemplateManager) noEscapeJS(t string) template.JS { return template.JS(t) } func (tm *TemplateManager) slashes() template.HTML { return template.HTML("//") } -func detectInt(i interface{}) int { +func detectInt(i any) int { switch v := i.(type) { case int: return int(v) @@ -332,7 +332,7 @@ func detectInt(i interface{}) int { panic(fmt.Sprintf("%v is not an int", i)) } -func toFloat(i interface{}) float64 { +func toFloat(i any) float64 { switch v := i.(type) { case int: return float64(v)