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.hs95
1 files changed, 95 insertions, 0 deletions
diff --git a/compiler/src/FileProcessors.hs b/compiler/src/FileProcessors.hs
new file mode 100644
index 0000000..8ea04d1
--- /dev/null
+++ b/compiler/src/FileProcessors.hs
@@ -0,0 +1,95 @@
1-- ldgallery - A static generator which turns a collection of tagged
2-- pictures into a searchable web gallery.
3--
4-- Copyright (C) 2019-2020 Pacien TRAN-GIRARD
5--
6-- This program is free software: you can redistribute it and/or modify
7-- it under the terms of the GNU Affero General Public License as
8-- published by the Free Software Foundation, either version 3 of the
9-- License, or (at your option) any later version.
10--
11-- This program is distributed in the hope that it will be useful,
12-- but WITHOUT ANY WARRANTY; without even the implied warranty of
13-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14-- GNU Affero General Public License for more details.
15--
16-- You should have received a copy of the GNU Affero General Public License
17-- along with this program. If not, see <https://www.gnu.org/licenses/>.
18
19module FileProcessors
20 ( FileProcessor
21 , copyFileProcessor
22 , resizePictureUpTo
23 , resourceAt
24 , getImageResolution
25 , ItemDescriber
26 , getPictureProps
27 ) where
28
29
30import Control.Exception (Exception, throwIO)
31import System.Process (readProcess, callProcess)
32import Text.Read (readMaybe)
33
34import System.Directory (getModificationTime)
35import qualified System.Directory
36
37import Config (Resolution(..))
38import Resource (Resource(..), GalleryItemProps(..))
39import Files
40
41
42data ProcessingException = ProcessingException FilePath String deriving Show
43instance Exception ProcessingException
44
45type FileProcessor =
46 FileName -- ^ Input path
47 -> FileName -- ^ Output path
48 -> IO ()
49
50copyFileProcessor :: FileProcessor
51copyFileProcessor inputPath outputPath =
52 putStrLn ("Copying:\t" ++ outputPath)
53 >> ensureParentDir (flip System.Directory.copyFile) outputPath inputPath
54
55resizePictureUpTo :: Resolution -> FileProcessor
56resizePictureUpTo maxResolution inputPath outputPath =
57 putStrLn ("Generating:\t" ++ outputPath)
58 >> ensureParentDir (flip resize) outputPath inputPath
59 where
60 maxSize :: Resolution -> String
61 maxSize Resolution{width, height} = show width ++ "x" ++ show height ++ ">"
62
63 resize :: FileName -> FileName -> IO ()
64 resize input output = callProcess "magick"
65 [ input
66 , "-auto-orient"
67 , "-resize", maxSize maxResolution
68 , output ]
69
70
71resourceAt :: FilePath -> Path -> IO Resource
72resourceAt fsPath resPath = Resource resPath <$> getModificationTime fsPath
73
74getImageResolution :: FilePath -> IO Resolution
75getImageResolution fsPath =
76 readProcess "magick" ["identify", "-format", "%w %h", firstFrame] []
77 >>= parseResolution . break (== ' ')
78 where
79 firstFrame :: FilePath
80 firstFrame = fsPath ++ "[0]"
81
82 parseResolution :: (String, String) -> IO Resolution
83 parseResolution (widthString, heightString) =
84 case (readMaybe widthString, readMaybe heightString) of
85 (Just w, Just h) -> return $ Resolution w h
86 _ -> throwIO $ ProcessingException fsPath "Unable to read image resolution."
87
88
89type ItemDescriber =
90 FilePath
91 -> Resource
92 -> IO GalleryItemProps
93
94getPictureProps :: ItemDescriber
95getPictureProps fsPath resource = Picture resource <$> getImageResolution fsPath