From e0a74532d30ad2cd7c93ba688b3119a28c3ea28e Mon Sep 17 00:00:00 2001 From: Xe Iaso Date: Sat, 11 Jan 2025 15:59:27 -0500 Subject: [PATCH] cmd/stickers: serve error images with error codes Signed-off-by: Xe Iaso --- cmd/stickers/main.go | 44 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 42 insertions(+), 2 deletions(-) diff --git a/cmd/stickers/main.go b/cmd/stickers/main.go index 67f1e835..2921ddfd 100644 --- a/cmd/stickers/main.go +++ b/cmd/stickers/main.go @@ -5,6 +5,8 @@ import ( "embed" "flag" "fmt" + "io" + "io/fs" "log" "log/slog" "net/http" @@ -95,7 +97,26 @@ func main() { }) if err != nil { slog.Error("can't head key", "format", format, "bucket", *bucketName, "key", key, "err", err) - http.ServeFileFS(w, r, static, "data/not_found."+format) + + st, err := fs.Stat(static, "data/not_found."+format) + if err != nil { + http.Error(w, "internal stat error, sorry", http.StatusInternalServerError) + return + } + + fin, err := static.Open("data/not_found." + format) + if err != nil { + http.Error(w, "internal fopen error, sorry", http.StatusInternalServerError) + return + } + defer fin.Close() + + w.Header().Add("Content-Length", fmt.Sprint(st.Size())) + w.Header().Add("Content-Type", "image/"+format) + w.WriteHeader(http.StatusNotFound) + + io.Copy(w, fin) + return } @@ -105,7 +126,26 @@ func main() { req, err := presigner.GetObject(r.Context(), key, 3600) if err != nil { slog.Error("can't presign get for key", "format", format, "bucket", *bucketName, "key", key, "err", err) - http.ServeFileFS(w, r, static, "data/error."+format) + + st, err := fs.Stat(static, "data/error."+format) + if err != nil { + http.Error(w, "internal stat error, sorry", http.StatusInternalServerError) + return + } + + fin, err := static.Open("data/error." + format) + if err != nil { + http.Error(w, "internal fopen error, sorry", http.StatusInternalServerError) + return + } + defer fin.Close() + + w.Header().Add("Content-Length", fmt.Sprint(st.Size())) + w.Header().Add("Content-Type", "image/"+format) + w.WriteHeader(http.StatusInternalServerError) + + io.Copy(w, fin) + return }