diff options
Diffstat (limited to 'compiler/src/FileProcessors.hs')
-rw-r--r-- | compiler/src/FileProcessors.hs | 59 |
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 | ||
19 | module FileProcessors | 19 | module 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) | |||
35 | import qualified System.Directory | 41 | import qualified System.Directory |
36 | 42 | ||
37 | import Config (Resolution(..)) | 43 | import Config (Resolution(..)) |
38 | import Resource (Resource(..), GalleryItemProps(..)) | 44 | import Resource (Resource(..), GalleryItemProps(..), Thumbnail(..)) |
39 | import Files | 45 | import Files |
40 | 46 | ||
41 | 47 | ||
42 | data ProcessingException = ProcessingException FilePath String deriving Show | 48 | data ProcessingException = ProcessingException FilePath String deriving Show |
43 | instance Exception ProcessingException | 49 | instance Exception ProcessingException |
44 | 50 | ||
45 | type FileProcessor = | 51 | type 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 | |||
58 | transformThenDescribe :: FileTransformer -> FileDescriber a -> FileProcessor a | ||
59 | transformThenDescribe transformer describer _itemPath resPath fsInPath fsOutPath = | ||
60 | transformer fsInPath fsOutPath >> describer resPath fsOutPath | ||
61 | |||
62 | copyResource :: (Resource -> a) -> FileProcessor a | ||
63 | copyResource resPropConstructor = | ||
64 | transformThenDescribe copyFileProcessor (getResProps resPropConstructor) | ||
65 | |||
66 | noopProcessor :: FileProcessor (Maybe a) | ||
67 | noopProcessor _ _ _ _ = return Nothing | ||
68 | |||
69 | |||
70 | type FileTransformer = | ||
46 | FileName -- ^ Input path | 71 | FileName -- ^ Input path |
47 | -> FileName -- ^ Output path | 72 | -> FileName -- ^ Output path |
48 | -> IO () | 73 | -> IO () |
49 | 74 | ||
50 | copyFileProcessor :: FileProcessor | 75 | copyFileProcessor :: FileTransformer |
51 | copyFileProcessor inputPath outputPath = | 76 | copyFileProcessor 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 | ||
55 | resizePictureUpTo :: Resolution -> FileProcessor | 80 | resizePictureUpTo :: Resolution -> FileTransformer |
56 | resizePictureUpTo maxResolution inputPath outputPath = | 81 | resizePictureUpTo 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 | ||
71 | resourceAt :: FilePath -> Path -> IO Resource | 96 | type FileDescriber a = |
72 | resourceAt fsPath resPath = Resource resPath <$> getModificationTime fsPath | 97 | Path -- ^ Target resource path |
98 | -> FilePath -- ^ Filesystem path | ||
99 | -> IO a | ||
73 | 100 | ||
74 | getImageResolution :: FilePath -> IO Resolution | 101 | getImageResolution :: FilePath -> IO Resolution |
75 | getImageResolution fsPath = | 102 | getImageResolution 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 | ||
115 | resourceAt :: FileDescriber Resource | ||
116 | resourceAt resPath fsPath = Resource resPath <$> getModificationTime fsPath | ||
117 | |||
118 | getResProps :: (Resource -> a) -> FileDescriber a | ||
119 | getResProps resPropsConstructor resPath fsPath = | ||
120 | resPropsConstructor <$> resourceAt resPath fsPath | ||
88 | 121 | ||
89 | type ItemDescriber = | 122 | getPictureProps :: FileDescriber GalleryItemProps |
90 | FilePath | 123 | getPictureProps resPath fsPath = |
91 | -> Resource | 124 | Picture <$> resourceAt resPath fsPath <*> getImageResolution fsPath |
92 | -> IO GalleryItemProps | ||
93 | 125 | ||
94 | getPictureProps :: ItemDescriber | 126 | getThumbnailProps :: FileDescriber (Maybe Thumbnail) |
95 | getPictureProps fsPath resource = Picture resource <$> getImageResolution fsPath | 127 | getThumbnailProps resPath fsPath = |
128 | Just <$> (Thumbnail <$> resourceAt resPath fsPath <*> getImageResolution fsPath) | ||