Skip to content

Commit

Permalink
fix curl upload issue
Browse files Browse the repository at this point in the history
  • Loading branch information
watzon committed Jan 13, 2025
1 parent 5f153a0 commit b76f210
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .air.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 20 additions & 8 deletions internal/server/services/paste.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit b76f210

Please sign in to comment.