diff options
| author | Xe Iaso <me@xeiaso.net> | 2025-01-11 15:59:27 -0500 |
|---|---|---|
| committer | Xe Iaso <me@xeiaso.net> | 2025-01-11 15:59:35 -0500 |
| commit | e0a74532d30ad2cd7c93ba688b3119a28c3ea28e (patch) | |
| tree | f1fae63952c5bf9d08bf64555012397b8841fc76 /cmd | |
| parent | 2e6c3d168258cdcbc46a45bd356c4415a441fe7f (diff) | |
| download | x-e0a74532d30ad2cd7c93ba688b3119a28c3ea28e.tar.xz x-e0a74532d30ad2cd7c93ba688b3119a28c3ea28e.zip | |
cmd/stickers: serve error images with error codes
Signed-off-by: Xe Iaso <me@xeiaso.net>
Diffstat (limited to 'cmd')
| -rw-r--r-- | cmd/stickers/main.go | 44 |
1 files changed, 42 insertions, 2 deletions
diff --git a/cmd/stickers/main.go b/cmd/stickers/main.go index 67f1e83..2921ddf 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 } |
