eidolon/Handler/Upload.hs

98 lines
3.1 KiB
Haskell
Raw Normal View History

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
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
}
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
2014-08-13 22:52:57 +00:00
userId <- lift $ pure $ getUserIdFromText tempUserId
2014-08-15 12:50:03 +00:00
(uploadWidget, enctype) <- generateFormPost (uploadForm userId)
2014-08-13 16:18:35 +00:00
defaultLayout $ do
$(widgetFile "upload")
Nothing -> do
2014-08-15 13:47:16 +00:00
setMessage "You need to be logged in"
2014-08-13 16:18:35 +00:00
redirect $ LoginR
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
2014-08-13 22:52:57 +00:00
userId <- lift $ pure $ getUserIdFromText tempUserId
2014-08-15 12:50:03 +00:00
((result, uploadWidget), enctype) <- runFormPost (uploadForm userId)
2014-08-13 16:18:35 +00:00
case result of
FormSuccess temp -> do
path <- writeOnDrive (tempMediumFile temp) userId (tempMediumAlbum temp)
2014-08-18 01:11:05 +00:00
inAlbumId <- return $ tempMediumAlbum temp
2014-08-13 16:18:35 +00:00
medium <- return $ Medium
(tempMediumTitle temp)
2014-08-18 01:11:05 +00:00
('/' : path)
2014-08-13 16:18:35 +00:00
(tempMediumTime temp)
(tempMediumOwner temp)
(tempMediumDesc temp)
(tempMediumTags temp)
2014-08-18 01:11:05 +00:00
inAlbumId
2014-08-13 22:52:32 +00:00
mId <- runDB $ I.insert medium
2014-08-18 01:11:05 +00:00
inAlbum <- runDB $ getJust inAlbumId
newMediaList <- return $ mId : (albumContent inAlbum)
runDB $ update inAlbumId [AlbumContent =. newMediaList]
2014-08-15 13:47:16 +00:00
setMessage "Image succesfully uploaded"
2014-08-13 16:18:35 +00:00
redirect $ HomeR
_ -> do
2014-08-15 13:47:16 +00:00
setMessage "There was an error uploading the file"
2014-08-13 16:18:35 +00:00
redirect $ UploadR
Nothing -> do
2014-08-15 13:47:16 +00:00
setMessage "You need to be logged in"
2014-08-13 16:18:35 +00:00
redirect $ LoginR
writeOnDrive :: FileInfo -> UserId -> AlbumId -> Handler FilePath
writeOnDrive file userId albumId = do
2014-08-13 16:18:35 +00:00
filename <- return $ fileName file
2014-08-18 01:09:03 +00:00
path <- return $ "static" </> "data"
</> (unpack $ extractKey userId)
</> (unpack $ extractKey albumId)
</> (unpack filename)
2014-08-13 16:18:35 +00:00
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
2014-08-17 20:59:55 +00:00
_ -> return $ Left $ error "unexpected tag list"
2014-08-13 16:18:35 +00:00
, fieldView = \idAttr nameAttr _ eResult isReq ->
[whamlet|<input id=#{idAttr} type="text" name=#{nameAttr}>|]
, fieldEnctype = UrlEncoded
}