postFileNewR :: AssetId -> Handler RepHtml
postFileNewR aid = do
  _asset <- runDB $ get404 aid
  user <- requireAuth
  wreq <- waiRequest
  yreq <- getRequest
  (params, files) <- lift $ WAI.parseRequestBody WAI.tempFileBackEnd wreq

  let lookupWaiPar p = Data.List.lookup (Data.Text.Encoding.encodeUtf8 p) params
      lookupWaiFile f = Data.List.lookup (Data.Text.Encoding.encodeUtf8 f) files

      token = lookupWaiPar "_token"
      comment = lookupWaiPar "f3" -- FIXME -- hard coded string
      file = lookupWaiFile "f2" -- FIXME -- hard coded string

      tokenOK = (fmap Data.Text.Encoding.encodeUtf8 . reqToken $ yreq) == token

  -- when (not tokenOK) (redirect $ FileNewR aid) -- FIXME -- is this correct way to handle it?

  case (comment,file, tokenOK) of
    (Just comm, Just waiFileInfo, True) -> do
      fn <- liftIO randomFileName -- generate new name
      liftIO $ renameFile (WAI.fileContent waiFileInfo) fn -- rename received file
      fsize <- liftIO $ getFileSize fn -- get file size
      crc <- liftIO (fmap crc32 (LBS.readFile fn))
      let bs2t = Data.Text.Encoding.decodeUtf8With lenientDecode
          comment'ta = Textarea (bs2t comm)
          uid = entityKey user
          declared'fname = bs2t (WAI.fileName waiFileInfo)
          declared'ctype = bs2t (WAI.fileContentType waiFileInfo)
          
          new = File aid uid fn comment'ta declared'fname declared'ctype fsize crc
      fid <- runDB $ insert new
      redirect (FileViewR fid)

    _ -> do
      (formWidget, formEnctype) <- generateFormPost fileUploadForm
      defaultLayout $ do
             setTitle "Upload new file."
             $(widgetFile "file/new")

-- view attributes
postFileNewR :: AssetId -> Handler RepHtml
postFileNewR aid = do
  _asset <- runDB $ get404 aid
  user <- requireAuth
  wreq <- waiRequest
  yreq <- getRequest
  (params, files) <- lift $ WAI.parseRequestBody WAI.tempFileBackEnd wreq

  let lookupWaiPar p = Data.List.lookup (Data.Text.Encoding.encodeUtf8 p) params
      lookupWaiFile f = Data.List.lookup (Data.Text.Encoding.encodeUtf8 f) files

      token = lookupWaiPar "_token"
      comment = lookupWaiPar "f3" -- FIXME -- hard coded string
      file = lookupWaiFile "f2" -- FIXME -- hard coded string

      tokenOK = (fmap Data.Text.Encoding.encodeUtf8 . reqToken $ yreq) == token

  -- when (not tokenOK) (redirect $ FileNewR aid) -- FIXME -- is this correct way to handle it?

  case (comment,file, tokenOK) of
    (Just comm, Just waiFileInfo, True) -> do
      fn <- liftIO randomFileName -- generate new name
      liftIO $ renameFile (WAI.fileContent waiFileInfo) fn -- rename received file
      fsize <- liftIO $ getFileSize fn -- get file size
      crc <- liftIO (fmap crc32 (LBS.readFile fn))
      let bs2t = Data.Text.Encoding.decodeUtf8With lenientDecode
          comment'ta = Textarea (bs2t comm)
          uid = entityKey user
          declared'fname = bs2t (WAI.fileName waiFileInfo)
          declared'ctype = bs2t (WAI.fileContentType waiFileInfo)
          
          new = File aid uid fn comment'ta declared'fname declared'ctype fsize crc
      fid <- runDB $ insert new
      redirect (FileViewR fid)

    _ -> do
      (formWidget, formEnctype) <- generateFormPost fileUploadForm
      defaultLayout $ do
             setTitle "Upload new file."
             $(widgetFile "file/new")

-- view attributes
postFileNewR aid = do
  _asset <- runDB $ get404 aid
  user <- requireAuth
  wreq <- waiRequest
  yreq <- getRequest
  (params, files) <- lift $ WAI.parseRequestBody WAI.tempFileBackEnd wreq

  let lookupWaiPar p = Data.List.lookup (Data.Text.Encoding.encodeUtf8 p) params
      lookupWaiFile f = Data.List.lookup (Data.Text.Encoding.encodeUtf8 f) files

      token = lookupWaiPar "_token"
      comment = lookupWaiPar "f3" -- FIXME -- hard coded string
      file = lookupWaiFile "f2" -- FIXME -- hard coded string

      tokenOK = (fmap Data.Text.Encoding.encodeUtf8 . reqToken $ yreq) == token

  -- when (not tokenOK) (redirect $ FileNewR aid) -- FIXME -- is this correct way to handle it?

  case (comment,file, tokenOK) of
    (Just comm, Just waiFileInfo, True) -> do
      fn <- liftIO randomFileName -- generate new name
      liftIO $ renameFile (WAI.fileContent waiFileInfo) fn -- rename received file
      fsize <- liftIO $ getFileSize fn -- get file size
      crc <- liftIO (fmap crc32 (LBS.readFile fn))
      let bs2t = Data.Text.Encoding.decodeUtf8With lenientDecode
          comment'ta = Textarea (bs2t comm)
          uid = entityKey user
          declared'fname = bs2t (WAI.fileName waiFileInfo)
          declared'ctype = bs2t (WAI.fileContentType waiFileInfo)
          
          new = File aid uid fn comment'ta declared'fname declared'ctype fsize crc
      fid <- runDB $ insert new
      redirect (FileViewR fid)

    _ -> do
      (formWidget, formEnctype) <- generateFormPost fileUploadForm
      defaultLayout $ do
             setTitle "Upload new file."
             $(widgetFile "file/new")

-- view attributes