0

I'm struggling to split a POST response (multipart) apart, what should be used to put the contents of some files sent to the Yesod server into a database (after some further processing). My current code:

import qualified Data.ByteString.Lazy as LZ
import qualified Data.ByteString.Lazy.Char8 as LC
...
processLines :: String -> [String] -> String
processLines delim (l:rest) = do
    case l of
        delim -> ""
        _     -> l ++ "\n" ++ processLines delim rest

processFile :: [String] -> String
processFile (delim:some:other:line:txt) = processLines delim txt

postImpexR :: SystemsId -> Handler RepPlain
postImpexR sysid = do
    wr <- waiRequest
    bss <- lift $ requestBody wr $$ consume
    let file = LZ.fromChunks bss
    return $ RepPlain $ toContent $ processFile $ map LC.unpack $ LC.lines file

Edit: Managed to fix one problem, seems I'm on the way to understand handlers. What's the problem with the types here?? Is there a more elegant way to get this done than this?

1 Answers1

1

If you're looking for multipart support, that's built into Yesod, there's no need to resort to manual parsing. Consider using fileField or lookupFile.

Michael Snoyman
  • 31,100
  • 3
  • 48
  • 77
  • Thanks a lot. Seems I have most trouble getting correct information. – user2054578 Feb 12 '13 at 20:06
  • OK, but when I use fInfo <- lookupFiles in the handler, it wants an additional text argument. If I append "filename" (as the POST parameter name is), I get an empty list. – user2054578 Feb 13 '13 at 08:56
  • I can't explain why that's happening without knowing the details of the request, but file uploads should be fully supported. If something's wrong, then you should file a bug report. – Michael Snoyman Feb 13 '13 at 09:48
  • OK, found a problem with the request generator, my fault. Thanks a lot. – user2054578 Feb 13 '13 at 09:56