2014-08-12 21:45:33 +00:00
|
|
|
module Handler.Upload where
|
|
|
|
|
2014-08-13 16:18:35 +00:00
|
|
|
import Import as I
|
|
|
|
import Data.Time
|
|
|
|
import Data.Text
|
|
|
|
import System.FilePath
|
2014-08-13 20:30:48 +00:00
|
|
|
import Database.Persist.Types
|
2014-08-13 16:18:35 +00:00
|
|
|
|
|
|
|
data TempMedium = TempMedium
|
|
|
|
{ tempMediumTitle :: Text
|
|
|
|
, tempMediumFile :: FileInfo
|
|
|
|
, tempMediumTime :: UTCTime
|
2014-08-13 20:30:48 +00:00
|
|
|
, tempMediumOwner :: UserId
|
2014-08-13 16:18:35 +00:00
|
|
|
, tempMediumDesc :: Textarea
|
|
|
|
, tempMediumTags :: [Text]
|
2014-08-13 22:52:32 +00:00
|
|
|
, tempMediumAlbum :: AlbumId
|
2014-08-13 16:18:35 +00:00
|
|
|
}
|
2014-08-12 21:45:33 +00:00
|
|
|
|
|
|
|
getUploadR :: Handler Html
|
2014-08-13 16:18:35 +00:00
|
|
|
getUploadR = do
|
2014-08-13 20:30:48 +00:00
|
|
|
msu <- lookupSession "userId"
|
2014-08-13 16:18:35 +00:00
|
|
|
case msu of
|
2014-08-13 20:30:48 +00:00
|
|
|
Just tempUserId -> do
|
|
|
|
userId <- lift $ getUserIdFromText tempUserId
|
|
|
|
(uploadWidget, enctype) <- generateFormPost (uploadForm userId)
|
2014-08-13 16:18:35 +00:00
|
|
|
defaultLayout $ do
|
|
|
|
$(widgetFile "upload")
|
|
|
|
Nothing -> do
|
|
|
|
setMessage $ [shamlet|<pre>You need to be logged in|]
|
|
|
|
redirect $ LoginR
|
2014-08-12 21:45:33 +00:00
|
|
|
|
2014-08-13 20:30:48 +00:00
|
|
|
--getUserIdFromText :: Text -> UserId
|
|
|
|
getUserIdFromText tempUserId =
|
|
|
|
Key $ PersistInt64 $ fromIntegral $ read $ unpack tempUserId
|
|
|
|
|
2014-08-12 21:45:33 +00:00
|
|
|
postUploadR :: Handler Html
|
2014-08-13 16:18:35 +00:00
|
|
|
postUploadR = do
|
2014-08-13 20:30:48 +00:00
|
|
|
msu <- lookupSession "userId"
|
2014-08-13 16:18:35 +00:00
|
|
|
case msu of
|
2014-08-13 20:30:48 +00:00
|
|
|
Just tempUserId -> do
|
|
|
|
userId <- lift $ getUserIdFromText tempUserId
|
|
|
|
((result, uploadWidget), enctype) <- runFormPost (uploadForm userId)
|
2014-08-13 16:18:35 +00:00
|
|
|
case result of
|
|
|
|
FormSuccess temp -> do
|
2014-08-13 20:30:48 +00:00
|
|
|
path <- writeOnDrive $ tempMediumFile temp
|
2014-08-13 16:18:35 +00:00
|
|
|
medium <- return $ Medium
|
|
|
|
(tempMediumTitle temp)
|
|
|
|
path
|
|
|
|
(tempMediumTime temp)
|
|
|
|
(tempMediumOwner temp)
|
|
|
|
(tempMediumDesc temp)
|
|
|
|
(tempMediumTags temp)
|
2014-08-13 22:52:32 +00:00
|
|
|
(tempMediumAlbum temp)
|
|
|
|
mId <- runDB $ I.insert medium
|
|
|
|
setMessage $ [shamlet|<pre>Image succesfully uploaded|]
|
2014-08-13 16:18:35 +00:00
|
|
|
redirect $ HomeR
|
|
|
|
_ -> do
|
2014-08-13 22:52:32 +00:00
|
|
|
setMessage $ [shamlet|<pre>There was an error uploading the file|]
|
2014-08-13 16:18:35 +00:00
|
|
|
redirect $ UploadR
|
|
|
|
Nothing -> do
|
|
|
|
setMessage $ [shamlet|<pre>You need to be logged in|]
|
|
|
|
redirect $ LoginR
|
|
|
|
|
2014-08-13 20:30:48 +00:00
|
|
|
writeOnDrive :: FileInfo -> Handler FilePath
|
|
|
|
writeOnDrive file = do
|
2014-08-13 16:18:35 +00:00
|
|
|
filename <- return $ fileName file
|
|
|
|
path <- return $ "static" </> (unpack filename)
|
|
|
|
liftIO $ fileMove file path
|
|
|
|
return path
|
|
|
|
|
2014-08-13 20:30:48 +00:00
|
|
|
uploadForm :: UserId -> Form TempMedium
|
|
|
|
uploadForm userId = renderDivs $ TempMedium
|
2014-08-13 16:18:35 +00:00
|
|
|
<$> areq textField "Title" Nothing
|
|
|
|
<*> areq fileField "Select file" Nothing
|
|
|
|
<*> lift (liftIO getCurrentTime)
|
2014-08-13 20:30:48 +00:00
|
|
|
<*> pure userId
|
2014-08-13 16:18:35 +00:00
|
|
|
<*> areq textareaField "Description" Nothing
|
|
|
|
<*> areq tagField "Enter tags" Nothing
|
2014-08-13 22:52:32 +00:00
|
|
|
<*> areq (selectField albums) "Album" Nothing
|
|
|
|
where
|
|
|
|
-- albums :: GHandler App App (OptionList AlbumId)
|
|
|
|
albums = do
|
|
|
|
entities <- runDB $ selectList [AlbumOwner ==. userId] [Desc AlbumTitle]
|
|
|
|
optionsPairs $ I.map (\alb -> (albumTitle $ entityVal alb, entityKey alb)) entities
|
2014-08-13 16:18:35 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|