Skip to content

Add postMultipartForm - #77

Open
ocramz wants to merge 1 commit into
hspec:mainfrom
ocramz:file-upload-utils-#76
Open

Add postMultipartForm#77
ocramz wants to merge 1 commit into
hspec:mainfrom
ocramz:file-upload-utils-#76

Conversation

@ocramz

@ocramz ocramz commented Jan 1, 2024

Copy link
Copy Markdown

closes #76

@ocramz ocramz mentioned this pull request Jan 1, 2024
5 tasks
@ocramz

ocramz commented Jan 11, 2024

Copy link
Copy Markdown
Author

Hi @sol , how do you do? What do you think of this patch? I'd find it very useful for testing scotty, so I hope you'll accept it here. Thanks!

@ocramz

ocramz commented Jan 28, 2024

Copy link
Copy Markdown
Author

@sol ping!

2 similar comments
@ocramz

ocramz commented Feb 27, 2024

Copy link
Copy Markdown
Author

@sol ping!

@ocramz

ocramz commented Aug 18, 2024

Copy link
Copy Markdown
Author

@sol ping!

@sol sol left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unless you're willing to get this bikeshedded to death, a separate package (e.g. hspec-wai-multipart) might be a better option for this.

But if you're inclined to get this upstream, this could be a start:

data FormData = FormData {
  boundary :: ByteString
, parts :: [(String,  Part)]
}

data Part = Part {
  filename :: Maybe FilePath
, contentType :: Maybe MimeType
, body :: ByteString
}

instance IsString Part where
  fromString = Part Nothing Nothing . encodeUtf8

filePart :: FilePath -> ByteString -> Part
filePart name body = Part (Just name) (Just . defaultMimeLookup $ fromString name) body

postMultipartForm :: ByteString -> [(String, Part)] -> WaiSession st SResponse
postMultipartForm path = postFormData path . FormData defaultBoundary

defaultBoundary :: ByteString
defaultBoundary = "..." -- some (base 64?) encoded randomness from random.org that is large enough so that it is effectively unique

postFormData :: ByteString -> FormData -> WaiSession st SResponse
postFormData = ...

Usage:

postMultipartForm "/update-profile"
  [("name", "Joe Doe"), ("picture", filePart "me.jpg" imageData)]

Note that Part could also be

data Part = Part {
  filename :: Maybe FilePath
, contentType :: Maybe MimeType
, headers :: [Header]
, body :: ByteString
}

or even

data Part = Part {
  filename :: Maybe FilePath
, headers :: [Header]
, body :: ByteString
}

@ocramz

ocramz commented Aug 19, 2024

Copy link
Copy Markdown
Author

I appreciate the feedback, bikeshedding is part of what makes OSS great!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add file upload utilities

2 participants