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.hs115
1 files changed, 115 insertions, 0 deletions
diff --git a/compiler/src/ItemProcessors.hs b/compiler/src/ItemProcessors.hs
new file mode 100644
index 0000000..f967954
--- /dev/null
+++ b/compiler/src/ItemProcessors.hs
@@ -0,0 +1,115 @@
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 , thumbnailFileProcessor
23 ) where
24
25
26import Data.Char (toLower)
27import System.FilePath (takeExtension)
28
29import Config (Resolution(..))
30import Resource (ItemProcessor, Thumbnail(..), GalleryItemProps(..))
31import Caching (Cache)
32import FileProcessors
33import Files
34
35
36data Format =
37 PictureFormat
38 | PlainTextFormat
39 | PortableDocumentFormat
40 | VideoFormat
41 | AudioFormat
42 | Unknown
43
44formatFromPath :: Path -> Format
45formatFromPath =
46 maybe Unknown ((fromExt . map toLower) . takeExtension) . fileName
47 where
48 fromExt :: String -> Format
49 fromExt ext = case ext of
50 ".bmp" -> PictureFormat
51 ".jpg" -> PictureFormat
52 ".jpeg" -> PictureFormat
53 ".png" -> PictureFormat
54 ".tiff" -> PictureFormat
55 ".hdr" -> PictureFormat
56 ".gif" -> PictureFormat
57 ".txt" -> PlainTextFormat
58 ".md" -> PlainTextFormat -- TODO: handle markdown separately
59 ".pdf" -> PortableDocumentFormat
60 ".wav" -> AudioFormat
61 ".oga" -> AudioFormat
62 ".ogg" -> AudioFormat
63 ".spx" -> AudioFormat
64 ".opus" -> AudioFormat
65 ".flac" -> AudioFormat
66 ".m4a" -> AudioFormat
67 ".mp3" -> AudioFormat
68 ".ogv" -> VideoFormat
69 ".ogx" -> VideoFormat
70 ".webm" -> VideoFormat
71 ".mkv" -> VideoFormat
72 ".mp4" -> VideoFormat
73 _ -> Unknown
74
75
76type ItemFileProcessor a =
77 FilePath -- ^ Filesystem input base path
78 -> FilePath -- ^ Filesystem output base path
79 -> FileName -- ^ Output class (subdir)
80 -> ItemProcessor a
81
82
83callFileProcessor :: (Path -> FileProcessor a) -> Cache a -> ItemFileProcessor a
84callFileProcessor processorProvider withCache inputBase outputBase resClass itemPath resPath =
85 withCache (processorProvider resPath)
86 itemPath
87 (resClass /> resPath)
88 (localPath $ inputBase /> resPath)
89 (localPath $ outputBase /> (resClass /> resPath))
90
91
92itemFileProcessor :: Maybe Resolution -> Cache GalleryItemProps -> ItemFileProcessor GalleryItemProps
93itemFileProcessor maxResolution =
94 callFileProcessor (flip processorFor maxResolution . formatFromPath)
95 where
96 processorFor :: Format -> Maybe Resolution -> FileProcessor GalleryItemProps
97 processorFor PictureFormat (Just maxRes) =
98 transformThenDescribe (resizePictureUpTo maxRes) getPictureProps
99 processorFor PictureFormat Nothing =
100 transformThenDescribe copyFileProcessor getPictureProps
101 processorFor PlainTextFormat _ = copyResource PlainText
102 processorFor PortableDocumentFormat _ = copyResource PDF
103 processorFor VideoFormat _ = copyResource Video
104 processorFor AudioFormat _ = copyResource Audio
105 processorFor Unknown _ = copyResource Other
106 -- TODO: handle video reencoding and others?
107
108
109thumbnailFileProcessor :: Resolution -> Cache (Maybe Thumbnail) -> ItemFileProcessor (Maybe Thumbnail)
110thumbnailFileProcessor maxRes =
111 callFileProcessor (processorFor . formatFromPath)
112 where
113 processorFor :: Format -> FileProcessor (Maybe Thumbnail)
114 processorFor PictureFormat = transformThenDescribe (resizePictureUpTo maxRes) getThumbnailProps
115 processorFor _ = noopProcessor