upload now working
This commit is contained in:
parent
3b14082e5e
commit
56c1731d64
3 changed files with 84 additions and 5 deletions
|
@ -1,9 +1,79 @@
|
||||||
module Handler.Upload where
|
module Handler.Upload where
|
||||||
|
|
||||||
import Import
|
import Import as I
|
||||||
|
import Data.Time
|
||||||
|
import Data.Text
|
||||||
|
import System.FilePath
|
||||||
|
|
||||||
|
data TempMedium = TempMedium
|
||||||
|
{ tempMediumTitle :: Text
|
||||||
|
, tempMediumFile :: FileInfo
|
||||||
|
, tempMediumTime :: UTCTime
|
||||||
|
, tempMediumOwner :: Text
|
||||||
|
, tempMediumDesc :: Textarea
|
||||||
|
, tempMediumTags :: [Text]
|
||||||
|
}
|
||||||
|
|
||||||
getUploadR :: Handler Html
|
getUploadR :: Handler Html
|
||||||
getUploadR = error "Not yet implemented: getUploadR"
|
getUploadR = do
|
||||||
|
msu <- lookupSession "username"
|
||||||
|
case msu of
|
||||||
|
Just username -> do
|
||||||
|
(uploadWidget, enctype) <- generateFormPost (uploadForm username)
|
||||||
|
defaultLayout $ do
|
||||||
|
$(widgetFile "upload")
|
||||||
|
Nothing -> do
|
||||||
|
setMessage $ [shamlet|<pre>You need to be logged in|]
|
||||||
|
redirect $ LoginR
|
||||||
|
|
||||||
postUploadR :: Handler Html
|
postUploadR :: Handler Html
|
||||||
postUploadR = error "Not yet implemented: postUploadR"
|
postUploadR = do
|
||||||
|
msu <- lookupSession "username"
|
||||||
|
case msu of
|
||||||
|
Just username -> do
|
||||||
|
((result, uploadWidget), enctype) <- runFormPost (uploadForm username)
|
||||||
|
case result of
|
||||||
|
FormSuccess temp -> do
|
||||||
|
path <- writeOnDrive $ tempMediumFile temp
|
||||||
|
medium <- return $ Medium
|
||||||
|
(tempMediumTitle temp)
|
||||||
|
path
|
||||||
|
(tempMediumTime temp)
|
||||||
|
(tempMediumOwner temp)
|
||||||
|
(tempMediumDesc temp)
|
||||||
|
(tempMediumTags temp)
|
||||||
|
mId <- runDB $ insert medium
|
||||||
|
setMessage $ [shamlet|Image succesfully uploaded|]
|
||||||
|
redirect $ HomeR
|
||||||
|
_ -> do
|
||||||
|
setMessage $ [shamlet|There was an error uploading the file|]
|
||||||
|
redirect $ UploadR
|
||||||
|
Nothing -> do
|
||||||
|
setMessage $ [shamlet|<pre>You need to be logged in|]
|
||||||
|
redirect $ LoginR
|
||||||
|
|
||||||
|
writeOnDrive :: FileInfo -> Handler FilePath
|
||||||
|
writeOnDrive file = do
|
||||||
|
filename <- return $ fileName file
|
||||||
|
path <- return $ "static" </> (unpack filename)
|
||||||
|
liftIO $ fileMove file path
|
||||||
|
return path
|
||||||
|
|
||||||
|
uploadForm :: Text -> Form TempMedium
|
||||||
|
uploadForm username = renderDivs $ TempMedium
|
||||||
|
<$> areq textField "Title" Nothing
|
||||||
|
<*> areq fileField "Select file" Nothing
|
||||||
|
<*> lift (liftIO getCurrentTime)
|
||||||
|
<*> pure username
|
||||||
|
<*> areq textareaField "Description" Nothing
|
||||||
|
<*> areq tagField "Enter tags" Nothing
|
||||||
|
|
||||||
|
tagField :: Field Handler [Text]
|
||||||
|
tagField = Field
|
||||||
|
{ fieldParse = \rawVals _ -> do
|
||||||
|
case rawVals of
|
||||||
|
[x] -> return $ Right $ Just $ splitOn " " x
|
||||||
|
, fieldView = \idAttr nameAttr _ eResult isReq ->
|
||||||
|
[whamlet|<input id=#{idAttr} type="text" name=#{nameAttr}>|]
|
||||||
|
, fieldEnctype = UrlEncoded
|
||||||
|
}
|
||||||
|
|
|
@ -13,10 +13,9 @@ Album
|
||||||
Medium
|
Medium
|
||||||
title Text
|
title Text
|
||||||
path FilePath
|
path FilePath
|
||||||
thumbPath FilePath
|
|
||||||
time UTCTime
|
time UTCTime
|
||||||
owner Text
|
owner Text
|
||||||
description Text
|
description Textarea
|
||||||
tags [Text]
|
tags [Text]
|
||||||
|
|
||||||
-- By default this file is used in Model.hs (which is imported by Foundation.hs)
|
-- By default this file is used in Model.hs (which is imported by Foundation.hs)
|
||||||
|
|
10
templates/upload.hamlet
Normal file
10
templates/upload.hamlet
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
<h3>Image upload
|
||||||
|
|
||||||
|
Here you can upload your image.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
<form method=post enctype=#{enctype}>
|
||||||
|
^{uploadWidget}
|
||||||
|
<div>
|
||||||
|
<input type=submit value="Upload">
|
Loading…
Reference in a new issue