eidolon/Handler/Upload.hs

229 lines
8.8 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.Maybe
2014-09-24 21:03:18 +00:00
import qualified Data.Text as T
2014-12-02 07:02:40 +00:00
import qualified System.FilePath as FP
import qualified Filesystem.Path as FSP
import Filesystem.Path.CurrentOS
import Graphics.ImageMagick.MagickWand
import Control.Monad.Trans.Resource
2014-10-01 22:58:36 +00:00
import Foreign
import Foreign.C.Types
import Foreign.C.String
2014-12-03 21:01:57 +00:00
import Helper
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
}
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
2014-09-24 21:03:18 +00:00
setTitle "Eidolon :: Upload Medium"
2014-08-13 16:18:35 +00:00
$(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
2014-12-03 21:01:57 +00:00
fil <- return $ tempMediumFile temp
case (fileContentType fil) `elem` acceptedTypes of
True -> do
path <- writeOnDrive fil userId (tempMediumAlbum temp)
thumbPath <- generateThumb path userId (tempMediumAlbum temp)
inAlbumId <- return $ tempMediumAlbum temp
medium <- return $ Medium
(tempMediumTitle temp)
('/' : path)
('/' : thumbPath)
(tempMediumTime temp)
(tempMediumOwner temp)
(tempMediumDesc temp)
(tempMediumTags temp)
inAlbumId
mId <- runDB $ I.insert medium
inAlbum <- runDB $ getJust inAlbumId
newMediaList <- return $ mId : (albumContent inAlbum)
runDB $ update inAlbumId [AlbumContent =. newMediaList]
setMessage "Image succesfully uploaded"
redirect $ HomeR
_ -> do
setMessage "This filetype is not supported"
redirect $ UploadR
2014-08-13 16:18:35 +00:00
_ -> 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
2014-08-26 02:39:13 +00:00
getDirectUploadR :: AlbumId -> Handler Html
getDirectUploadR albumId = do
tempAlbum <- runDB $ get albumId
case tempAlbum of -- does the requested album exist
Just album -> do
ownerId <- return $ albumOwner album
owner <- runDB $ getJust ownerId
ownerName <- return $ userName owner
msu <- lookupSession "userId"
case msu of -- is anybody logged in
Just tempUserId -> do
userId <- return $ getUserIdFromText tempUserId
presence <- return $ (userId == ownerId) || (userId `elem` (albumShares album))
case presence of -- is the owner present or a user with whom the album is shared
2014-08-26 02:39:13 +00:00
True -> do
(dUploadWidget, enctype) <- generateFormPost $ dUploadForm userId albumId
defaultLayout $ do
2014-09-24 21:03:18 +00:00
setTitle $ toHtml ("Eidolon :: Upload medium to " `T.append` (albumTitle album))
2014-08-26 02:39:13 +00:00
$(widgetFile "dUpload")
False -> do
setMessage "You must own this album to upload"
redirect $ AlbumR albumId
Nothing -> do
setMessage "You must be logged in to upload"
redirect $ LoginR
Nothing -> do
setMessage "This album does not exist"
redirect $ HomeR
postDirectUploadR :: AlbumId -> Handler Html
postDirectUploadR albumId = do
tempAlbum <- runDB $ get albumId
case tempAlbum of -- does the album exist
Just album -> do
ownerId <- return $ albumOwner album
owner <- runDB $ getJust ownerId
ownerName <- return $ userName owner
msu <- lookupSession "userId"
case msu of -- is anybody logged in
Just tempUserId -> do
userId <- return $ getUserIdFromText tempUserId
presence <- return $ (userId == ownerId) || (userId `elem` (albumShares album))
case presence of -- is the logged in user the owner or is the album shared with him
2014-08-26 02:39:13 +00:00
True -> do
((result, dUploadWidget), enctype) <- runFormPost (dUploadForm userId albumId)
case result of
FormSuccess temp -> do
2014-12-03 21:01:57 +00:00
fil <- return $ tempMediumFile temp
case (fileContentType fil) `elem` acceptedTypes of
True -> do
path <- writeOnDrive fil userId albumId
thumbPath <- generateThumb path ownerId albumId
medium <- return $ Medium
(tempMediumTitle temp)
('/' : path)
('/' : thumbPath)
(tempMediumTime temp)
(tempMediumOwner temp)
(tempMediumDesc temp)
(tempMediumTags temp)
albumId
mId <- runDB $ insert medium
inAlbum <- runDB $ getJust albumId
newMediaList <- return $ mId : (albumContent inAlbum)
runDB $ update albumId [AlbumContent =. newMediaList]
setMessage "Image successfully uploaded"
redirect $ AlbumR albumId
_ -> do
setMessage "This filetype is not supported"
redirect $ DirectUploadR albumId
2014-08-26 02:39:13 +00:00
_ -> do
setMessage "There was an error uploading the file"
redirect $ DirectUploadR albumId
False -> do -- owner is not present
setMessage "You must own this album to upload"
redirect $ AlbumR albumId
Nothing -> do
setMessage "You must be logged in to upload"
redirect $ AlbumR albumId
Nothing -> do
setMessage "This Album does not exist"
redirect $ AlbumR albumId
2014-12-02 07:02:40 +00:00
generateThumb :: FP.FilePath -> UserId -> AlbumId -> Handler FP.FilePath
2014-10-01 22:58:36 +00:00
generateThumb path userId albumId = do
2014-12-02 07:02:40 +00:00
newName <- return $ (FP.takeBaseName path) ++ "_thumb" ++ (FP.takeExtension path)
newPath <- return $ "static" FP.</> "data"
FP.</> (T.unpack $ extractKey userId)
FP.</> (T.unpack $ extractKey albumId)
FP.</> newName
liftIO $ withMagickWandGenesis $ do
(_ , w) <- magickWand
readImage w (decodeString path)
w1 <- getImageWidth w
h1 <- getImageHeight w
h2 <- return 220
w2 <- return $ floor (((fromIntegral w1) / (fromIntegral h1)) * (fromIntegral h2))
resizeImage w w2 h2 lanczosFilter 1
setImageCompressionQuality w 95
writeImage w (Just (decodeString newPath))
return newPath
2014-10-01 22:58:36 +00:00
2014-12-02 07:02:40 +00:00
writeOnDrive :: FileInfo -> UserId -> AlbumId -> Handler FP.FilePath
writeOnDrive fil userId albumId = do
2014-12-03 22:30:13 +00:00
--filen <- return $ fileName fil
album <- runDB $ getJust albumId
filen <- return $ show $ (length $ albumContent album) + 1
ext <- return $ FP.takeExtension $ T.unpack $ fileName fil
2014-12-02 07:02:40 +00:00
path <- return $ "static" FP.</> "data"
FP.</> (T.unpack $ extractKey userId)
FP.</> (T.unpack $ extractKey albumId)
2014-12-03 22:30:13 +00:00
FP.</> filen ++ ext
2014-12-02 07:02:40 +00:00
liftIO $ fileMove fil path
2014-08-13 16:18:35 +00:00
return path
2014-08-13 20:30:48 +00:00
uploadForm :: UserId -> Form TempMedium
uploadForm userId = renderDivs $ (\a b c d e f g -> TempMedium b c d e f g a)
<$> areq (selectField albums) "Album" Nothing
<*> areq textField "Title" Nothing
2014-08-13 16:18:35 +00:00
<*> 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
where
-- albums :: GHandler App App (OptionList AlbumId)
albums = do
allEnts <- runDB $ selectList [] [Desc AlbumTitle]
entities <- return $
map fromJust $
removeItem Nothing $ map
(\ent -> do
case (userId == (albumOwner $ entityVal ent)) || (userId `elem` (albumShares $ entityVal ent)) of
True -> Just ent
False -> Nothing
) allEnts
-- entities <- runDB $ selectList [AlbumOwner ==. userId] [Desc AlbumTitle]
2014-08-13 22:52:32 +00:00
optionsPairs $ I.map (\alb -> (albumTitle $ entityVal alb, entityKey alb)) entities
2014-08-13 16:18:35 +00:00
2014-08-26 02:39:13 +00:00
dUploadForm :: UserId -> AlbumId -> Form TempMedium
dUploadForm userId albumId = renderDivs $ TempMedium
<$> areq textField "Title" Nothing
<*> areq fileField "Select file" Nothing
<*> lift (liftIO getCurrentTime)
<*> pure userId
<*> areq textareaField "Description" Nothing
<*> areq tagField "Enter tags" Nothing
<*> pure albumId