From b76f2101d5d22f4c7763cb3a77aa93d4fd1c3ed1 Mon Sep 17 00:00:00 2001 From: Chris Watson Date: Mon, 13 Jan 2025 12:53:06 -0700 Subject: [PATCH] fix curl upload issue --- .air.toml | 2 +- internal/server/services/paste.go | 28 ++++++++++++++++++++-------- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/.air.toml b/.air.toml index 66df3b8..601bade 100644 --- a/.air.toml +++ b/.air.toml @@ -7,7 +7,7 @@ tmp_dir = "tmp" bin = "./tmp/main" cmd = "go build -o ./tmp/main ." delay = 1000 - exclude_dir = ["assets", "tmp", "vendor", "testdata"] + exclude_dir = ["assets", "tmp", "vendor", "testdata", "uploads"] exclude_file = [] exclude_regex = ["_test.go"] exclude_unchanged = false diff --git a/internal/server/services/paste.go b/internal/server/services/paste.go index 0767852..c4e857f 100644 --- a/internal/server/services/paste.go +++ b/internal/server/services/paste.go @@ -52,12 +52,24 @@ func (s *PasteService) UploadPaste(c *fiber.Ctx) error { zap.String("body", string(c.Body()))) p := new(PasteOptions) - if err := c.BodyParser(p); err != nil { - s.logger.Error("Failed to parse request body", - zap.Error(err), - zap.String("content-type", c.Get("Content-Type")), - zap.String("body", string(c.Body()))) - return fiber.NewError(fiber.StatusBadRequest, "Invalid request body") + contentType := c.Get("Content-Type") + + // Handle form data differently from JSON/other formats + if strings.Contains(contentType, "multipart/form-data") || strings.Contains(contentType, "application/x-www-form-urlencoded") { + // Parse form values + if err := c.BodyParser(p); err != nil { + s.logger.Error("Failed to parse form values", + zap.Error(err)) + } + } else { + // For JSON and other formats + if err := c.BodyParser(p); err != nil { + s.logger.Error("Failed to parse request body", + zap.Error(err), + zap.String("content-type", c.Get("Content-Type")), + zap.String("body", string(c.Body()))) + return fiber.NewError(fiber.StatusBadRequest, "Invalid request body") + } } s.logger.Debug("Parsed paste options", @@ -142,8 +154,8 @@ func (s *PasteService) UploadPaste(c *fiber.Ctx) error { } // If this is a browser form submission, redirect to the paste view - contentType := c.Get("Content-Type") - if strings.Contains(contentType, "application/x-www-form-urlencoded") || strings.Contains(contentType, "multipart/form-data") { + acceptHeader := c.Get("Accept") + if strings.Contains(acceptHeader, "text/html") { // Store the deletion URL in the session for display after redirect c.Cookie(&fiber.Cookie{ Name: "deletion_url",