fix: make sure we dont try to decode jpgs into json
All checks were successful
Build and Push Server / build-and-push (push) Successful in 3m17s

This commit is contained in:
Max Richter
2025-10-31 19:39:44 +01:00
parent fd6aeb8a27
commit f14b685e43

View File

@@ -69,10 +69,10 @@ func (h *Handler) post(w http.ResponseWriter, r *http.Request, target string) {
defer r.Body.Close() defer r.Body.Close()
contentType := r.Header.Get("Content-Type") 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 var contentToWrite []byte
if strings.HasSuffix(target, ".md") && isJSON { if isResource {
renderedContent, err := renderer.RenderFile(body) renderedContent, err := renderer.RenderFile(body)
if err != nil { if err != nil {
writeError(w, http.StatusInternalServerError, fmt.Errorf("failed to render file: %w", err)) 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 return
} }
if isResource {
var data map[string]any var data map[string]any
if err := json.Unmarshal(body, &data); err != nil { if err := json.Unmarshal(body, &data); err != nil && strings.HasSuffix(target, ".md") {
writeError(w, http.StatusInternalServerError, fmt.Errorf("failed to decode body: %w", err)) writeError(w, http.StatusInternalServerError, fmt.Errorf("failed to decode body: %w", err))
return 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) { func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {