From c8dea48bb4a0ec137bafba3bec79352eae2f48c0 Mon Sep 17 00:00:00 2001 From: pacien Date: Mon, 6 Jan 2020 00:01:53 +0100 Subject: compiler: default item date to filesystem last mod date GitHub: closes #14 --- compiler/package.yaml | 2 ++ compiler/src/Resource.hs | 32 ++++++++++++++++++++++++++------ compiler/stack.yaml | 6 ++++++ compiler/stack.yaml.lock | 30 +++++++++++++++++++++++++++++- 4 files changed, 63 insertions(+), 7 deletions(-) (limited to 'compiler') diff --git a/compiler/package.yaml b/compiler/package.yaml index 0922c36..fd44ccc 100644 --- a/compiler/package.yaml +++ b/compiler/package.yaml @@ -27,6 +27,8 @@ dependencies: - JuicyPixels-extra - parallel-io - Glob +- safe +- time >= 1.9.3 && < 1.10 default-extensions: - DuplicateRecordFields diff --git a/compiler/src/Resource.hs b/compiler/src/Resource.hs index 261191b..207239f 100644 --- a/compiler/src/Resource.hs +++ b/compiler/src/Resource.hs @@ -30,6 +30,10 @@ import Data.Char (toLower) import Data.Maybe (mapMaybe, fromMaybe) import Data.Function ((&)) import qualified Data.Set as Set +import Data.Time.LocalTime (ZonedTime, utc, utcToZonedTime, zonedTimeToUTC) +import Data.Time.Format.ISO8601 (iso8601ParseM) +import System.Directory (getModificationTime) +import Safe.Foldable (maximumByMay) import GHC.Generics (Generic) import Data.Aeson (FromJSON, ToJSON, genericToJSON, genericToEncoding) @@ -75,7 +79,7 @@ instance ToJSON GalleryItemProps where data GalleryItem = GalleryItem { title :: String - , date :: String -- TODO: checked ISO8601 date + , date :: ZonedTime , description :: String , tags :: [Tag] , path :: Path @@ -103,18 +107,22 @@ buildGalleryTree processItem processThumbnail tagsFromDirectories galleryName in do properties <- processItem path processedThumbnail <- processThumbnail path + fileModTime <- lastModTime path return GalleryItem { title = itemTitle - , date = optMeta date "" -- TODO: check and normalise dates + , date = fromMaybe fileModTime itemDate , description = optMeta description "" , tags = (optMeta tags []) ++ implicitParentTags parents , path = parents >= iso8601ParseM + optMeta :: (Sidecar -> Maybe a) -> a -> a optMeta get fallback = fromMaybe fallback $ get sidecar @@ -122,11 +130,10 @@ buildGalleryTree processItem processThumbnail tagsFromDirectories galleryName in do processedThumbnail <- maybeThumbnail dirThumbnailPath processedItems <- parallel $ map (mkGalleryItem Nothing itemPath) items + dirModTime <- lastModTime path return GalleryItem { title = itemTitle - -- TODO: consider using the most recent item's date? what if empty? - , date = "" - -- TODO: consider allowing metadata sidecars for directories too + , date = fromMaybe dirModTime $ mostRecentChildModTime processedItems , description = "" , tags = (aggregateChildTags processedItems) ++ implicitParentTags parents , path = itemPath @@ -143,6 +150,13 @@ buildGalleryTree processItem processThumbnail tagsFromDirectories galleryName in maybeThumbnail Nothing = return Nothing maybeThumbnail (Just thumbnailPath) = processThumbnail thumbnailPath + mostRecentChildModTime :: [GalleryItem] -> Maybe ZonedTime + mostRecentChildModTime = + maximumByMay comparingDates . map (date::(GalleryItem -> ZonedTime)) + + comparingDates :: ZonedTime -> ZonedTime -> Ordering + comparingDates l r = compare (zonedTimeToUTC l) (zonedTimeToUTC r) + aggregateChildTags :: [GalleryItem] -> [Tag] aggregateChildTags = unique . concatMap (\item -> tags (item::GalleryItem)) @@ -152,6 +166,12 @@ buildGalleryTree processItem processThumbnail tagsFromDirectories galleryName in implicitParentTags :: Path -> [Tag] implicitParentTags (Path elements) = take tagsFromDirectories elements + lastModTime :: Path -> IO ZonedTime + lastModTime path = + localPath path + & getModificationTime + >>= return . utcToZonedTime utc + flattenGalleryTree :: GalleryItem -> [GalleryItem] flattenGalleryTree dir@(GalleryItem _ _ _ _ _ _ (Directory items)) = diff --git a/compiler/stack.yaml b/compiler/stack.yaml index 29f8539..146f46b 100644 --- a/compiler/stack.yaml +++ b/compiler/stack.yaml @@ -41,6 +41,12 @@ packages: # # extra-deps: [] +extra-deps: +- time-1.9.3@sha256:8f1b5448722a12a952248b356c9eb366e351226543d9086a2da71270522d5f45,5679 +- directory-1.3.4.0@sha256:500019f04494324d1df16cf83eefeb3f809b2b20b32a32ccd755ee0439c18bfd,2829 +- process-1.6.7.0@sha256:305bcf44c42a96425e77af1748183f505a701648f68cc299d5ad8ac1b866a6a1,2468 +- unix-2.7.2.2@sha256:e69269a17b9fa26cb77f3f55e86c39d0a4940ccfa0c4bc20826919d2572076ad,3496 + # Override default flag values for local packages and extra-deps # flags: {} diff --git a/compiler/stack.yaml.lock b/compiler/stack.yaml.lock index fc538c1..8e833c5 100644 --- a/compiler/stack.yaml.lock +++ b/compiler/stack.yaml.lock @@ -3,7 +3,35 @@ # For more information, please see the documentation at: # https://docs.haskellstack.org/en/stable/lock_files -packages: [] +packages: +- completed: + hackage: time-1.9.3@sha256:8f1b5448722a12a952248b356c9eb366e351226543d9086a2da71270522d5f45,5679 + pantry-tree: + size: 6558 + sha256: a1043c1719491764f0fa37a1fd70d9451080548a41632fee88d8e1b8db4942d6 + original: + hackage: time-1.9.3@sha256:8f1b5448722a12a952248b356c9eb366e351226543d9086a2da71270522d5f45,5679 +- completed: + hackage: directory-1.3.4.0@sha256:500019f04494324d1df16cf83eefeb3f809b2b20b32a32ccd755ee0439c18bfd,2829 + pantry-tree: + size: 3365 + sha256: 00c09e0c014d29ebfb921b64c1459e61a0ad6f10e70128d795246a47c06394b0 + original: + hackage: directory-1.3.4.0@sha256:500019f04494324d1df16cf83eefeb3f809b2b20b32a32ccd755ee0439c18bfd,2829 +- completed: + hackage: process-1.6.7.0@sha256:305bcf44c42a96425e77af1748183f505a701648f68cc299d5ad8ac1b866a6a1,2468 + pantry-tree: + size: 1211 + sha256: 49c3e531d2473fe455c1cde655f074a320fa4ec8569d650262bf382f9c5796fb + original: + hackage: process-1.6.7.0@sha256:305bcf44c42a96425e77af1748183f505a701648f68cc299d5ad8ac1b866a6a1,2468 +- completed: + hackage: unix-2.7.2.2@sha256:e69269a17b9fa26cb77f3f55e86c39d0a4940ccfa0c4bc20826919d2572076ad,3496 + pantry-tree: + size: 3536 + sha256: c355f7924ce67e5bf8f20767462af18f09b8c0d1f7161117221cbb94c15deee3 + original: + hackage: unix-2.7.2.2@sha256:e69269a17b9fa26cb77f3f55e86c39d0a4940ccfa0c4bc20826919d2572076ad,3496 snapshots: - completed: size: 524799 -- cgit v1.2.3