From f14b685e43ecf87d21e5c78cae5391c439c79f5f Mon Sep 17 00:00:00 2001 From: Max Richter Date: Fri, 31 Oct 2025 19:39:44 +0100 Subject: [PATCH] fix: make sure we dont try to decode jpgs into json --- server/internal/handler/handler.go | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/server/internal/handler/handler.go b/server/internal/handler/handler.go index 697e806..a57f9e3 100644 --- a/server/internal/handler/handler.go +++ b/server/internal/handler/handler.go @@ -69,10 +69,10 @@ func (h *Handler) post(w http.ResponseWriter, r *http.Request, target string) { defer r.Body.Close() contentType := r.Header.Get("Content-Type") - isJSON := strings.HasPrefix(contentType, "application/json") + isResource := strings.HasPrefix(contentType, "application/json") && strings.HasSuffix(target, ".md") var contentToWrite []byte - if strings.HasSuffix(target, ".md") && isJSON { + if isResource { renderedContent, err := renderer.RenderFile(body) if err != nil { writeError(w, http.StatusInternalServerError, fmt.Errorf("failed to render file: %w", err)) @@ -88,13 +88,16 @@ func (h *Handler) post(w http.ResponseWriter, r *http.Request, target string) { return } - var data map[string]any - if err := json.Unmarshal(body, &data); err != nil { - writeError(w, http.StatusInternalServerError, fmt.Errorf("failed to decode body: %w", err)) - return + if isResource { + var data map[string]any + if err := json.Unmarshal(body, &data); err != nil && strings.HasSuffix(target, ".md") { + writeError(w, http.StatusInternalServerError, fmt.Errorf("failed to decode body: %w", err)) + return + } + writeJSON(w, http.StatusOK, data) } - writeJSON(w, http.StatusOK, data) + writeJSON(w, http.StatusOK, map[string]any{"success": true}) } func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {