From 7dde692101a7e36e0a431aeb864cbf3a0c0e96f8 Mon Sep 17 00:00:00 2001 From: pacien Date: Fri, 31 Jan 2020 19:43:24 +0100 Subject: compiler: add thumbnail size to index --- compiler/src/Processors.hs | 21 +++++++++++++++------ compiler/src/Resource.hs | 28 ++++++++++++++++++++++------ 2 files changed, 37 insertions(+), 12 deletions(-) (limited to 'compiler/src') diff --git a/compiler/src/Processors.hs b/compiler/src/Processors.hs index f2ade63..9ddc6ee 100644 --- a/compiler/src/Processors.hs +++ b/compiler/src/Processors.hs @@ -27,16 +27,17 @@ module Processors import Control.Exception (Exception) import Data.Function ((&)) import Data.Char (toLower) +import Data.List (break) import System.Directory hiding (copyFile) import qualified System.Directory import System.FilePath -import System.Process (callProcess) +import System.Process (callProcess, readProcess) import Resource ( ItemProcessor, ThumbnailProcessor - , GalleryItemProps(..), Resolution(..), Resource(..) ) + , GalleryItemProps(..), Resolution(..), Resource(..), Thumbnail(..) ) import Files @@ -119,6 +120,12 @@ withCached processor inputPath outputPath = resourceAt :: FilePath -> Path -> IO Resource resourceAt fsPath resPath = getModificationTime fsPath >>= return . Resource resPath +getImageResolution :: FilePath -> IO Resolution +getImageResolution fsPath = + readProcess "identify" ["-format", "%w %h", fsPath] [] + >>= return . break (== ' ') + >>= return . \(w, h) -> Resolution (read w) (read h) + type ItemFileProcessor = FileName -- ^ Input base path @@ -158,12 +165,14 @@ thumbnailFileProcessor maxRes cached inputBase outputBase resClass inputRes = inPath = localPath $ inputBase /> inputRes outPath = localPath $ outputBase /> relOutPath - process :: Maybe FileProcessor -> IO (Maybe Resource) + process :: Maybe FileProcessor -> IO (Maybe Thumbnail) process Nothing = return Nothing process (Just proc) = - proc inPath outPath - >> resourceAt outPath relOutPath - >>= return . Just + do + proc inPath outPath + resource <- resourceAt outPath relOutPath + resolution <- getImageResolution outPath + return $ Just $ Thumbnail resource resolution processorFor :: Format -> Maybe FileProcessor processorFor PictureFormat = Just $ resizePictureUpTo maxRes diff --git a/compiler/src/Resource.hs b/compiler/src/Resource.hs index c0ef317..33f3cf0 100644 --- a/compiler/src/Resource.hs +++ b/compiler/src/Resource.hs @@ -18,7 +18,7 @@ module Resource ( ItemProcessor, ThumbnailProcessor - , GalleryItem(..), GalleryItemProps(..), Resolution(..), Resource(..) + , GalleryItem(..), GalleryItemProps(..), Resolution(..), Resource(..), Thumbnail(..) , buildGalleryTree, galleryCleanupResourceDir ) where @@ -90,13 +90,23 @@ instance ToJSON GalleryItemProps where toEncoding = genericToEncoding encodingOptions +data Thumbnail = Thumbnail + { resource :: Resource + , resolution :: Resolution + } deriving (Generic, Show) + +instance ToJSON Thumbnail where + toJSON = genericToJSON encodingOptions + toEncoding = genericToEncoding encodingOptions + + data GalleryItem = GalleryItem { title :: String , datetime :: ZonedTime , description :: String , tags :: [Tag] , path :: Path - , thumbnail :: Maybe Resource + , thumbnail :: Maybe Thumbnail , properties :: GalleryItemProps } deriving (Generic, Show) @@ -106,7 +116,7 @@ instance ToJSON GalleryItem where type ItemProcessor = Path -> IO GalleryItemProps -type ThumbnailProcessor = Path -> IO (Maybe Resource) +type ThumbnailProcessor = Path -> IO (Maybe Thumbnail) buildGalleryTree :: @@ -150,7 +160,7 @@ buildGalleryTree processItem processThumbnail tagsFromDirectories galleryName in subItemsParents :: [String] subItemsParents = (maybeToList $ fileName path) ++ parentTitles - maybeThumbnail :: Maybe Path -> IO (Maybe Resource) + maybeThumbnail :: Maybe Path -> IO (Maybe Thumbnail) maybeThumbnail Nothing = return Nothing maybeThumbnail (Just thumbnailPath) = processThumbnail thumbnailPath @@ -197,10 +207,16 @@ galleryOutputDiff resources ref = resPath :: GalleryItemProps -> Maybe Path resPath Directory{} = Nothing - resPath resourceProps = Just (resourcePath $ resource resourceProps) + resPath resourceProps = + Just + $ resourcePath + $ (resource :: (GalleryItemProps -> Resource)) resourceProps thumbnailPaths :: [GalleryItem] -> [Path] - thumbnailPaths = (map resourcePath) . (mapMaybe thumbnail) + thumbnailPaths = + map resourcePath + . map (resource :: (Thumbnail -> Resource)) + . mapMaybe thumbnail galleryCleanupResourceDir :: GalleryItem -> FileName -> IO () -- cgit v1.2.3