aboutsummaryrefslogtreecommitdiff
path: root/compiler/src/FileProcessors.hs
diff options
context:
space:
mode:
Diffstat (limited to 'compiler/src/FileProcessors.hs')
-rw-r--r--compiler/src/FileProcessors.hs59
1 files changed, 46 insertions, 13 deletions
diff --git a/compiler/src/FileProcessors.hs b/compiler/src/FileProcessors.hs
index 8ea04d1..5c4e1c8 100644
--- a/compiler/src/FileProcessors.hs
+++ b/compiler/src/FileProcessors.hs
@@ -18,12 +18,18 @@
18 18
19module FileProcessors 19module FileProcessors
20 ( FileProcessor 20 ( FileProcessor
21 , transformThenDescribe
22 , copyResource
23 , noopProcessor
24 , FileTransformer
21 , copyFileProcessor 25 , copyFileProcessor
22 , resizePictureUpTo 26 , resizePictureUpTo
23 , resourceAt 27 , resourceAt
24 , getImageResolution 28 , getImageResolution
25 , ItemDescriber 29 , FileDescriber
30 , getResProps
26 , getPictureProps 31 , getPictureProps
32 , getThumbnailProps
27 ) where 33 ) where
28 34
29 35
@@ -35,24 +41,43 @@ import System.Directory (getModificationTime)
35import qualified System.Directory 41import qualified System.Directory
36 42
37import Config (Resolution(..)) 43import Config (Resolution(..))
38import Resource (Resource(..), GalleryItemProps(..)) 44import Resource (Resource(..), GalleryItemProps(..), Thumbnail(..))
39import Files 45import Files
40 46
41 47
42data ProcessingException = ProcessingException FilePath String deriving Show 48data ProcessingException = ProcessingException FilePath String deriving Show
43instance Exception ProcessingException 49instance Exception ProcessingException
44 50
45type FileProcessor = 51type FileProcessor a =
52 Path -- ^ Item path
53 -> Path -- ^ Target resource path
54 -> FilePath -- ^ Filesystem input path
55 -> FilePath -- ^ Filesystem output path
56 -> IO a
57
58transformThenDescribe :: FileTransformer -> FileDescriber a -> FileProcessor a
59transformThenDescribe transformer describer _itemPath resPath fsInPath fsOutPath =
60 transformer fsInPath fsOutPath >> describer resPath fsOutPath
61
62copyResource :: (Resource -> a) -> FileProcessor a
63copyResource resPropConstructor =
64 transformThenDescribe copyFileProcessor (getResProps resPropConstructor)
65
66noopProcessor :: FileProcessor (Maybe a)
67noopProcessor _ _ _ _ = return Nothing
68
69
70type FileTransformer =
46 FileName -- ^ Input path 71 FileName -- ^ Input path
47 -> FileName -- ^ Output path 72 -> FileName -- ^ Output path
48 -> IO () 73 -> IO ()
49 74
50copyFileProcessor :: FileProcessor 75copyFileProcessor :: FileTransformer
51copyFileProcessor inputPath outputPath = 76copyFileProcessor inputPath outputPath =
52 putStrLn ("Copying:\t" ++ outputPath) 77 putStrLn ("Copying:\t" ++ outputPath)
53 >> ensureParentDir (flip System.Directory.copyFile) outputPath inputPath 78 >> ensureParentDir (flip System.Directory.copyFile) outputPath inputPath
54 79
55resizePictureUpTo :: Resolution -> FileProcessor 80resizePictureUpTo :: Resolution -> FileTransformer
56resizePictureUpTo maxResolution inputPath outputPath = 81resizePictureUpTo maxResolution inputPath outputPath =
57 putStrLn ("Generating:\t" ++ outputPath) 82 putStrLn ("Generating:\t" ++ outputPath)
58 >> ensureParentDir (flip resize) outputPath inputPath 83 >> ensureParentDir (flip resize) outputPath inputPath
@@ -68,8 +93,10 @@ resizePictureUpTo maxResolution inputPath outputPath =
68 , output ] 93 , output ]
69 94
70 95
71resourceAt :: FilePath -> Path -> IO Resource 96type FileDescriber a =
72resourceAt fsPath resPath = Resource resPath <$> getModificationTime fsPath 97 Path -- ^ Target resource path
98 -> FilePath -- ^ Filesystem path
99 -> IO a
73 100
74getImageResolution :: FilePath -> IO Resolution 101getImageResolution :: FilePath -> IO Resolution
75getImageResolution fsPath = 102getImageResolution fsPath =
@@ -85,11 +112,17 @@ getImageResolution fsPath =
85 (Just w, Just h) -> return $ Resolution w h 112 (Just w, Just h) -> return $ Resolution w h
86 _ -> throwIO $ ProcessingException fsPath "Unable to read image resolution." 113 _ -> throwIO $ ProcessingException fsPath "Unable to read image resolution."
87 114
115resourceAt :: FileDescriber Resource
116resourceAt resPath fsPath = Resource resPath <$> getModificationTime fsPath
117
118getResProps :: (Resource -> a) -> FileDescriber a
119getResProps resPropsConstructor resPath fsPath =
120 resPropsConstructor <$> resourceAt resPath fsPath
88 121
89type ItemDescriber = 122getPictureProps :: FileDescriber GalleryItemProps
90 FilePath 123getPictureProps resPath fsPath =
91 -> Resource 124 Picture <$> resourceAt resPath fsPath <*> getImageResolution fsPath
92 -> IO GalleryItemProps
93 125
94getPictureProps :: ItemDescriber 126getThumbnailProps :: FileDescriber (Maybe Thumbnail)
95getPictureProps fsPath resource = Picture resource <$> getImageResolution fsPath 127getThumbnailProps resPath fsPath =
128 Just <$> (Thumbnail <$> resourceAt resPath fsPath <*> getImageResolution fsPath)