svg upload now works

but only limited
This commit is contained in:
nek0 2016-11-19 22:13:02 +01:00
parent 08903dc28c
commit 1122e9a756
2 changed files with 45 additions and 33 deletions

View file

@ -26,9 +26,12 @@ import qualified System.FilePath as FP
import System.Directory import System.Directory
import Text.Blaze.Internal import Text.Blaze.Internal
import Codec.ImageType import Codec.ImageType
import Codec.Picture import Codec.Picture as P
import Codec.Picture.Metadata as PM import Codec.Picture.Metadata as PM
import Codec.Picture.ScaleDCT import Codec.Picture.ScaleDCT
import Graphics.Rasterific.Svg as SVG
import Graphics.Svg
import Graphics.Text.TrueType
getDirectUploadR :: AlbumId -> Handler Html getDirectUploadR :: AlbumId -> Handler Html
getDirectUploadR albumId = do getDirectUploadR albumId = do
@ -110,7 +113,7 @@ postDirectUploadR albumId = do
redirect HomeR redirect HomeR
else do else do
let justErrNames = map fromJust onlyErrNames let justErrNames = map fromJust onlyErrNames
let msg = Content $ Text $ "File type not supported of: " `T.append` T.intercalate ", " justErrNames let msg = Content $ Text.Blaze.Internal.Text $ "File type not supported of: " `T.append` T.intercalate ", " justErrNames
setMessage msg setMessage msg
redirect HomeR redirect HomeR
_ -> do _ -> do
@ -140,24 +143,28 @@ generateThumbs
-> Handler ThumbsMeta -- ^ Resulting metadata to store -> Handler ThumbsMeta -- ^ Resulting metadata to store
generateThumbs path uId aId = do generateThumbs path uId aId = do
eimg <- liftIO $ readImageWithMetadata path eimg <- liftIO $ readImageWithMetadata path
case eimg of orig <- case eimg of
Left e -> error e Left _ -> do
Right (orig, meta) -> do svg <- liftIO $ loadSvgFile path
(img, _) <- liftIO $ renderSvgDocument emptyFontCache Nothing 100 $ fromJust svg
return img
Right (img, _) -> do
return $ convertRGBA8 img
let thumbName = FP.takeBaseName path ++ "_thumb.jpg" let thumbName = FP.takeBaseName path ++ "_thumb.jpg"
prevName = FP.takeBaseName path ++ "_preview.jpg" prevName = FP.takeBaseName path ++ "_preview.jpg"
pathPrefix = "static" FP.</> "data" FP.</> T.unpack (extractKey uId) FP.</> T.unpack (extractKey aId) pathPrefix = "static" FP.</> "data" FP.</> T.unpack (extractKey uId) FP.</> T.unpack (extractKey aId)
tPath = pathPrefix FP.</> thumbName tPath = pathPrefix FP.</> thumbName
pPath = pathPrefix FP.</> prevName pPath = pathPrefix FP.</> prevName
origPix = convertRGBA8 orig -- origPix = convertRGBA8 orig
oWidth = fromIntegral $ fromJust $ PM.lookup Width meta :: Int oWidth = P.imageWidth orig :: Int
oHeight = fromIntegral $ fromJust $ PM.lookup Height meta :: Int oHeight = P.imageHeight orig :: Int
tWidth = floor (fromIntegral oWidth / fromIntegral oHeight * fromIntegral tHeight :: Double) tWidth = floor (fromIntegral oWidth / fromIntegral oHeight * fromIntegral tHeight :: Double)
tHeight = 230 :: Int tHeight = 230 :: Int
pHeight = 600 :: Int pHeight = 600 :: Int
pScale = (fromIntegral pHeight :: Double) / (fromIntegral oHeight :: Double) pScale = (fromIntegral pHeight :: Double) / (fromIntegral oHeight :: Double)
pWidth = floor (fromIntegral oWidth * pScale) pWidth = floor (fromIntegral oWidth * pScale)
tPix = scale (tWidth, tHeight) origPix tPix = scale (tWidth, tHeight) orig
pPix = scale (pWidth, pHeight) origPix pPix = scale (pWidth, pHeight) orig
liftIO $ saveJpgImage 95 tPath $ ImageRGBA8 tPix liftIO $ saveJpgImage 95 tPath $ ImageRGBA8 tPix
liftIO $ saveJpgImage 95 pPath $ ImageRGBA8 pPix liftIO $ saveJpgImage 95 pPath $ ImageRGBA8 pPix
return $ ThumbsMeta return $ ThumbsMeta
@ -165,7 +172,7 @@ generateThumbs path uId aId = do
, metaPreviewPath = pPath , metaPreviewPath = pPath
} }
checkCVE_2016_3714 :: FP.FilePath -> Text -> IO Bool checkCVE_2016_3714 :: FP.FilePath -> T.Text -> IO Bool
checkCVE_2016_3714 p m = checkCVE_2016_3714 p m =
case m of case m of
"image/jpeg" -> isJpeg p "image/jpeg" -> isJpeg p
@ -184,7 +191,8 @@ writeOnDrive :: FileInfo -> UserId -> AlbumId -> Handler FP.FilePath
writeOnDrive fil userId albumId = do writeOnDrive fil userId albumId = do
--filen <- return $ fileName fil --filen <- return $ fileName fil
album <- runDB $ getJust albumId album <- runDB $ getJust albumId
let filen = show $ (read $ show $ maximum $ albumContent album :: Int) + 1 let [PersistInt64 int] = keyToValues $ maximum $ albumContent album
let filen = show $ fromIntegral int + 1
let ext = FP.takeExtension $ T.unpack $ fileName fil let ext = FP.takeExtension $ T.unpack $ fileName fil
let path = "static" FP.</> "data" FP.</> T.unpack (extractKey userId) FP.</> T.unpack (extractKey albumId) FP.</> filen ++ ext let path = "static" FP.</> "data" FP.</> T.unpack (extractKey userId) FP.</> T.unpack (extractKey albumId) FP.</> filen ++ ext
dde <- liftIO $ doesDirectoryExist $ FP.dropFileName path dde <- liftIO $ doesDirectoryExist $ FP.dropFileName path
@ -205,16 +213,16 @@ dUploadForm userId albumId = FileBulk
<*> aopt textareaField (bfs ("Description" :: T.Text)) Nothing <*> aopt textareaField (bfs ("Description" :: T.Text)) Nothing
<*> areq tagField (bfs ("Enter tags" :: T.Text)) Nothing <*> areq tagField (bfs ("Enter tags" :: T.Text)) Nothing
<*> pure albumId <*> pure albumId
<* bootstrapSubmit ("Upload" :: BootstrapSubmit Text) <* bootstrapSubmit ("Upload" :: BootstrapSubmit T.Text)
data FileBulk = FileBulk data FileBulk = FileBulk
{ fileBulkPrefix :: Text { fileBulkPrefix :: T.Text
, fileBulkFiles :: [FileInfo] , fileBulkFiles :: [FileInfo]
, fileBulkTime :: UTCTime , fileBulkTime :: UTCTime
, fileBulkOwner :: UserId , fileBulkOwner :: UserId
, fileBulkDesc :: Maybe Textarea , fileBulkDesc :: Maybe Textarea
, fileBulkTags :: [Text] , fileBulkTags :: [T.Text]
, fileBulkAlbum :: AlbumId , fileBulkAlbum :: AlbumId
} }
@ -249,7 +257,7 @@ bulkUploadForm userId = (\a b c d e f g -> FileBulk b c d e f g a)
<*> pure userId <*> pure userId
<*> aopt textareaField (bfs ("Description" :: T.Text)) Nothing <*> aopt textareaField (bfs ("Description" :: T.Text)) Nothing
<*> areq tagField (bfs ("Enter tags" :: T.Text)) Nothing <*> areq tagField (bfs ("Enter tags" :: T.Text)) Nothing
<* bootstrapSubmit ("Upload" :: BootstrapSubmit Text) <* bootstrapSubmit ("Upload" :: BootstrapSubmit T.Text)
where where
albums = do albums = do
allEnts <- runDB $ selectList [] [Desc AlbumTitle] allEnts <- runDB $ selectList [] [Desc AlbumTitle]
@ -316,7 +324,7 @@ postUploadR = do
redirect HomeR redirect HomeR
else do else do
let justErrNames = map fromJust onlyErrNames let justErrNames = map fromJust onlyErrNames
let msg = Content $ Text $ "File type not supported of: " `T.append` T.intercalate ", " justErrNames let msg = Content $ Text.Blaze.Internal.Text $ "File type not supported of: " `T.append` T.intercalate ", " justErrNames
setMessage msg setMessage msg
redirect HomeR redirect HomeR
_ -> do _ -> do

View file

@ -123,6 +123,10 @@ library
, JuicyPixels >= 3.2.8 , JuicyPixels >= 3.2.8
, JuicyPixels-scale-dct , JuicyPixels-scale-dct
, classy-prelude-yesod >= 1.0 , classy-prelude-yesod >= 1.0
-- svg stuff
, rasterific-svg
, svg-tree
, FontyFruity
-- for Migrations -- for Migrations
, HDBC , HDBC
, HDBC-postgresql , HDBC-postgresql