diff options
author | pacien | 2020-06-13 03:41:39 +0200 |
---|---|---|
committer | pacien | 2020-06-16 18:23:01 +0200 |
commit | ce2210e6deff1d981186b6d7ddb1176f27e41f49 (patch) | |
tree | 63d83660e733f16ef18d48debbc97c091c492c1f /compiler/src/Resource.hs | |
parent | 34b90f08a21fbe3f1928e16a8ea48f1fc7453e4e (diff) | |
download | ldgallery-ce2210e6deff1d981186b6d7ddb1176f27e41f49.tar.gz |
compiler: make GalleryIndex loadable from JSON
Diffstat (limited to 'compiler/src/Resource.hs')
-rw-r--r-- | compiler/src/Resource.hs | 28 |
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) | |||
31 | import Data.Function ((&)) | 31 | import Data.Function ((&)) |
32 | import Data.Functor ((<&>)) | 32 | import Data.Functor ((<&>)) |
33 | import qualified Data.Set as Set | 33 | import qualified Data.Set as Set |
34 | import Data.Text (pack) | 34 | import Data.Text (pack, unpack, breakOn) |
35 | import Data.Time.Clock (UTCTime) | 35 | import Data.Time.Clock (UTCTime) |
36 | import Data.Time.LocalTime (ZonedTime, utc, utcToZonedTime, zonedTimeToUTC) | 36 | import Data.Time.LocalTime (ZonedTime, utc, utcToZonedTime, zonedTimeToUTC) |
37 | import Data.Time.Format (formatTime, defaultTimeLocale) | 37 | import Data.Time.Format (formatTime, parseTimeM, defaultTimeLocale) |
38 | import Safe.Foldable (maximumByMay) | 38 | import Safe.Foldable (maximumByMay) |
39 | 39 | ||
40 | import GHC.Generics (Generic) | 40 | import GHC.Generics (Generic) |
41 | import Data.Aeson (ToJSON, genericToJSON, genericToEncoding) | 41 | import Data.Aeson (ToJSON, FromJSON, genericToJSON, genericToEncoding, genericParseJSON) |
42 | import qualified Data.Aeson as JSON | 42 | import qualified Data.Aeson as JSON |
43 | 43 | ||
44 | import Files | 44 | import 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 | ||
73 | instance 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 | ||
74 | data GalleryItemProps = | 81 | data 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 | ||
97 | instance FromJSON GalleryItemProps where | ||
98 | parseJSON = genericParseJSON encodingOptions | ||
99 | |||
90 | 100 | ||
91 | data Thumbnail = Thumbnail | 101 | data 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 | |||
96 | instance ToJSON Thumbnail where | ||
97 | toJSON = genericToJSON encodingOptions | ||
98 | toEncoding = genericToEncoding encodingOptions | ||
99 | 105 | ||
100 | 106 | ||
101 | data GalleryItem = GalleryItem | 107 | data 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 | |||
111 | instance ToJSON GalleryItem where | ||
112 | toJSON = genericToJSON encodingOptions | ||
113 | toEncoding = genericToEncoding encodingOptions | ||
114 | 116 | ||
115 | 117 | ||
116 | type ItemProcessor = Path -> IO GalleryItemProps | 118 | type ItemProcessor = Path -> IO GalleryItemProps |