aboutsummaryrefslogtreecommitdiff
path: root/compiler/src/ItemProcessors.hs
diff options
context:
space:
mode:
Diffstat (limited to 'compiler/src/ItemProcessors.hs')
-rw-r--r--compiler/src/ItemProcessors.hs132
1 files changed, 132 insertions, 0 deletions
diff --git a/compiler/src/ItemProcessors.hs b/compiler/src/ItemProcessors.hs
new file mode 100644
index 0000000..209bc2a
--- /dev/null
+++ b/compiler/src/ItemProcessors.hs
@@ -0,0 +1,132 @@
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 ItemProcessors
20 ( ItemProcessor
21 , itemFileProcessor
22 , ThumbnailProcessor
23 , thumbnailFileProcessor
24 ) where
25
26
27import Data.Function ((&))
28import Data.Char (toLower)
29import System.FilePath (takeExtension)
30
31import Config (Resolution(..))
32import Resource (ItemProcessor, ThumbnailProcessor, Thumbnail(..), GalleryItemProps(..))
33import Caching (Cache)
34import FileProcessors
35import Files
36
37
38data Format =
39 PictureFormat
40 | PlainTextFormat
41 | PortableDocumentFormat
42 | VideoFormat
43 | AudioFormat
44 | Unknown
45
46formatFromPath :: Path -> Format
47formatFromPath =
48 maybe Unknown ((fromExt . map toLower) . takeExtension) . fileName
49 where
50 fromExt :: String -> Format
51 fromExt ext = case ext of
52 ".bmp" -> PictureFormat
53 ".jpg" -> PictureFormat
54 ".jpeg" -> PictureFormat
55 ".png" -> PictureFormat
56 ".tiff" -> PictureFormat
57 ".hdr" -> PictureFormat
58 ".gif" -> PictureFormat
59 ".txt" -> PlainTextFormat
60 ".md" -> PlainTextFormat -- TODO: handle markdown separately
61 ".pdf" -> PortableDocumentFormat
62 ".wav" -> AudioFormat
63 ".oga" -> AudioFormat
64 ".ogg" -> AudioFormat
65 ".spx" -> AudioFormat
66 ".opus" -> AudioFormat
67 ".flac" -> AudioFormat
68 ".m4a" -> AudioFormat
69 ".mp3" -> AudioFormat
70 ".ogv" -> VideoFormat
71 ".ogx" -> VideoFormat
72 ".webm" -> VideoFormat
73 ".mkv" -> VideoFormat
74 ".mp4" -> VideoFormat
75 _ -> Unknown
76
77
78type ItemFileProcessor =
79 FileName -- ^ Input base path
80 -> FileName -- ^ Output base path
81 -> FileName -- ^ Output class (subdir)
82 -> ItemProcessor
83
84itemFileProcessor :: Maybe Resolution -> Cache -> ItemFileProcessor
85itemFileProcessor maxResolution cached inputBase outputBase resClass inputRes =
86 cached processor inPath outPath
87 >> resourceAt outPath relOutPath
88 >>= descriptor outPath
89 where
90 relOutPath = resClass /> inputRes
91 inPath = localPath $ inputBase /> inputRes
92 outPath = localPath $ outputBase /> relOutPath
93 (processor, descriptor) = processorFor (formatFromPath inputRes) maxResolution
94
95 processorFor :: Format -> Maybe Resolution -> (FileProcessor, ItemDescriber)
96 processorFor PictureFormat (Just maxRes) = (resizePictureUpTo maxRes, getPictureProps)
97 processorFor PictureFormat Nothing = (copyFileProcessor, getPictureProps)
98 processorFor PlainTextFormat _ = (copyFileProcessor, const $ return . PlainText)
99 processorFor PortableDocumentFormat _ = (copyFileProcessor, const $ return . PDF)
100 processorFor VideoFormat _ = (copyFileProcessor, const $ return . Video)
101 processorFor AudioFormat _ = (copyFileProcessor, const $ return . Audio)
102 -- TODO: handle video reencoding and others?
103 processorFor Unknown _ = (copyFileProcessor, const $ return . Other)
104
105
106type ThumbnailFileProcessor =
107 FileName -- ^ Input base path
108 -> FileName -- ^ Output base path
109 -> FileName -- ^ Output class (subdir)
110 -> ThumbnailProcessor
111
112thumbnailFileProcessor :: Resolution -> Cache -> ThumbnailFileProcessor
113thumbnailFileProcessor maxRes cached inputBase outputBase resClass inputRes =
114 cached <$> processorFor (formatFromPath inputRes)
115 & process
116 where
117 relOutPath = resClass /> inputRes
118 inPath = localPath $ inputBase /> inputRes
119 outPath = localPath $ outputBase /> relOutPath
120
121 process :: Maybe FileProcessor -> IO (Maybe Thumbnail)
122 process Nothing = return Nothing
123 process (Just proc) =
124 do
125 proc inPath outPath
126 resource <- resourceAt outPath relOutPath
127 resolution <- getImageResolution outPath
128 return $ Just $ Thumbnail resource resolution
129
130 processorFor :: Format -> Maybe FileProcessor
131 processorFor PictureFormat = Just $ resizePictureUpTo maxRes
132 processorFor _ = Nothing