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.hs28
1 files changed, 23 insertions, 5 deletions
diff --git a/compiler/src/FileProcessors.hs b/compiler/src/FileProcessors.hs
index 5c4e1c8..78e7351 100644
--- a/compiler/src/FileProcessors.hs
+++ b/compiler/src/FileProcessors.hs
@@ -1,7 +1,7 @@
1-- ldgallery - A static generator which turns a collection of tagged 1-- ldgallery - A static generator which turns a collection of tagged
2-- pictures into a searchable web gallery. 2-- pictures into a searchable web gallery.
3-- 3--
4-- Copyright (C) 2019-2020 Pacien TRAN-GIRARD 4-- Copyright (C) 2019-2022 Pacien TRAN-GIRARD
5-- 5--
6-- This program is free software: you can redistribute it and/or modify 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 7-- it under the terms of the GNU Affero General Public License as
@@ -79,7 +79,7 @@ copyFileProcessor inputPath outputPath =
79 79
80resizePictureUpTo :: Resolution -> FileTransformer 80resizePictureUpTo :: Resolution -> FileTransformer
81resizePictureUpTo maxResolution inputPath outputPath = 81resizePictureUpTo maxResolution inputPath outputPath =
82 putStrLn ("Generating:\t" ++ outputPath) 82 putStrLn ("Processing:\t" ++ outputPath)
83 >> ensureParentDir (flip resize) outputPath inputPath 83 >> ensureParentDir (flip resize) outputPath inputPath
84 where 84 where
85 maxSize :: Resolution -> String 85 maxSize :: Resolution -> String
@@ -100,17 +100,35 @@ type FileDescriber a =
100 100
101getImageResolution :: FilePath -> IO Resolution 101getImageResolution :: FilePath -> IO Resolution
102getImageResolution fsPath = 102getImageResolution fsPath =
103 readProcess "magick" ["identify", "-format", "%w %h", firstFrame] [] 103 readProcess "magick"
104 >>= parseResolution . break (== ' ') 104 [ "identify"
105 , "-ping"
106 , "-format", "%[orientation] %w %h"
107 , firstFrame
108 ] []
109 >>= parseOutput . words
110
105 where 111 where
106 firstFrame :: FilePath 112 firstFrame :: FilePath
107 firstFrame = fsPath ++ "[0]" 113 firstFrame = fsPath ++ "[0]"
108 114
115 -- Flip the dimensions when necessary according to the metadata.
116 -- ImageMagick's `-auto-orient` flag does the same, but isn't compatible
117 -- with `-ping` and causes the whole image file to be loaded.
118 parseOutput :: [String] -> IO Resolution
119 parseOutput ["RightTop", w, h] = parseResolution (h, w)
120 parseOutput ["LeftBottom", w, h] = parseResolution (h, w)
121 parseOutput [_, w, h] = parseResolution (w, h)
122 parseOutput _ = throwIO failedRead
123
109 parseResolution :: (String, String) -> IO Resolution 124 parseResolution :: (String, String) -> IO Resolution
110 parseResolution (widthString, heightString) = 125 parseResolution (widthString, heightString) =
111 case (readMaybe widthString, readMaybe heightString) of 126 case (readMaybe widthString, readMaybe heightString) of
112 (Just w, Just h) -> return $ Resolution w h 127 (Just w, Just h) -> return $ Resolution w h
113 _ -> throwIO $ ProcessingException fsPath "Unable to read image resolution." 128 _ -> throwIO failedRead
129
130 failedRead :: ProcessingException
131 failedRead = ProcessingException fsPath "Unable to read image resolution."
114 132
115resourceAt :: FileDescriber Resource 133resourceAt :: FileDescriber Resource
116resourceAt resPath fsPath = Resource resPath <$> getModificationTime fsPath 134resourceAt resPath fsPath = Resource resPath <$> getModificationTime fsPath