aboutsummaryrefslogtreecommitdiff
path: root/compiler/src/Resource.hs
diff options
context:
space:
mode:
authorpacien2020-06-13 03:41:39 +0200
committerpacien2020-06-16 18:23:01 +0200
commitce2210e6deff1d981186b6d7ddb1176f27e41f49 (patch)
tree63d83660e733f16ef18d48debbc97c091c492c1f /compiler/src/Resource.hs
parent34b90f08a21fbe3f1928e16a8ea48f1fc7453e4e (diff)
downloadldgallery-ce2210e6deff1d981186b6d7ddb1176f27e41f49.tar.gz
compiler: make GalleryIndex loadable from JSON
Diffstat (limited to 'compiler/src/Resource.hs')
-rw-r--r--compiler/src/Resource.hs28
1 files changed, 15 insertions, 13 deletions
diff --git a/compiler/src/Resource.hs b/compiler/src/Resource.hs
index 607c7f6..fa139e0 100644
--- a/compiler/src/Resource.hs
+++ b/compiler/src/Resource.hs
@@ -31,14 +31,14 @@ import Data.Maybe (mapMaybe, fromMaybe)
31import Data.Function ((&)) 31import Data.Function ((&))
32import Data.Functor ((<&>)) 32import Data.Functor ((<&>))
33import qualified Data.Set as Set 33import qualified Data.Set as Set
34import Data.Text (pack) 34import Data.Text (pack, unpack, breakOn)
35import Data.Time.Clock (UTCTime) 35import Data.Time.Clock (UTCTime)
36import Data.Time.LocalTime (ZonedTime, utc, utcToZonedTime, zonedTimeToUTC) 36import Data.Time.LocalTime (ZonedTime, utc, utcToZonedTime, zonedTimeToUTC)
37import Data.Time.Format (formatTime, defaultTimeLocale) 37import Data.Time.Format (formatTime, parseTimeM, defaultTimeLocale)
38import Safe.Foldable (maximumByMay) 38import Safe.Foldable (maximumByMay)
39 39
40import GHC.Generics (Generic) 40import GHC.Generics (Generic)
41import Data.Aeson (ToJSON, genericToJSON, genericToEncoding) 41import Data.Aeson (ToJSON, FromJSON, genericToJSON, genericToEncoding, genericParseJSON)
42import qualified Data.Aeson as JSON 42import qualified Data.Aeson as JSON
43 43
44import Files 44import Files
@@ -70,6 +70,13 @@ instance ToJSON Resource where
70 where 70 where
71 timestamp = formatTime defaultTimeLocale "%s" modTime 71 timestamp = formatTime defaultTimeLocale "%s" modTime
72 72
73instance FromJSON Resource where
74 parseJSON = JSON.withText "Resource" (unpackRes . breakOn "?")
75 where
76 unpackRes (resPathStr, modTimeStr) =
77 Resource (fromWebPath $ unpack resPathStr)
78 <$> parseTimeM True defaultTimeLocale "?%s" (unpack modTimeStr)
79
73 80
74data GalleryItemProps = 81data GalleryItemProps =
75 Directory { items :: [GalleryItem] } 82 Directory { items :: [GalleryItem] }
@@ -87,15 +94,14 @@ instance ToJSON GalleryItemProps where
87 toJSON = genericToJSON encodingOptions 94 toJSON = genericToJSON encodingOptions
88 toEncoding = genericToEncoding encodingOptions 95 toEncoding = genericToEncoding encodingOptions
89 96
97instance FromJSON GalleryItemProps where
98 parseJSON = genericParseJSON encodingOptions
99
90 100
91data Thumbnail = Thumbnail 101data Thumbnail = Thumbnail
92 { resource :: Resource 102 { resource :: Resource
93 , resolution :: Resolution 103 , resolution :: Resolution
94 } deriving (Generic, Show) 104 } deriving (Generic, Show, ToJSON, FromJSON)
95
96instance ToJSON Thumbnail where
97 toJSON = genericToJSON encodingOptions
98 toEncoding = genericToEncoding encodingOptions
99 105
100 106
101data GalleryItem = GalleryItem 107data GalleryItem = GalleryItem
@@ -106,11 +112,7 @@ data GalleryItem = GalleryItem
106 , path :: Path 112 , path :: Path
107 , thumbnail :: Maybe Thumbnail 113 , thumbnail :: Maybe Thumbnail
108 , properties :: GalleryItemProps 114 , properties :: GalleryItemProps
109 } deriving (Generic, Show) 115 } deriving (Generic, Show, ToJSON, FromJSON)
110
111instance ToJSON GalleryItem where
112 toJSON = genericToJSON encodingOptions
113 toEncoding = genericToEncoding encodingOptions
114 116
115 117
116type ItemProcessor = Path -> IO GalleryItemProps 118type ItemProcessor = Path -> IO GalleryItemProps