diff options
Diffstat (limited to 'compiler/src/ItemProcessors.hs')
-rw-r--r-- | compiler/src/ItemProcessors.hs | 121 |
1 files changed, 121 insertions, 0 deletions
diff --git a/compiler/src/ItemProcessors.hs b/compiler/src/ItemProcessors.hs new file mode 100644 index 0000000..6035477 --- /dev/null +++ b/compiler/src/ItemProcessors.hs | |||
@@ -0,0 +1,121 @@ | |||
1 | -- ldgallery - A static generator which turns a collection of tagged | ||
2 | -- pictures into a searchable web gallery. | ||
3 | -- | ||
4 | -- Copyright (C) 2019-2022 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 | |||
19 | module ItemProcessors | ||
20 | ( ItemProcessor | ||
21 | , itemFileProcessor | ||
22 | , thumbnailFileProcessor | ||
23 | ) where | ||
24 | |||
25 | |||
26 | import Data.Char (toLower) | ||
27 | import System.FilePath (takeExtension) | ||
28 | |||
29 | import Config (Resolution(..)) | ||
30 | import Resource (ItemProcessor, Thumbnail(..), GalleryItemProps(..)) | ||
31 | import Caching (Cache) | ||
32 | import FileProcessors | ||
33 | import Files | ||
34 | |||
35 | |||
36 | data Format = | ||
37 | PictureFormat | ||
38 | | PlainTextFormat | ||
39 | | MarkdownFormat | ||
40 | | PortableDocumentFormat | ||
41 | | EPUBFormat | ||
42 | | VideoFormat | ||
43 | | AudioFormat | ||
44 | | Unknown | ||
45 | |||
46 | formatFromPath :: Path -> Format | ||
47 | formatFromPath = | ||
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 | ".webp" -> PictureFormat | ||
60 | ".txt" -> PlainTextFormat | ||
61 | ".md" -> MarkdownFormat | ||
62 | ".pdf" -> PortableDocumentFormat | ||
63 | ".epub" -> EPUBFormat | ||
64 | ".wav" -> AudioFormat | ||
65 | ".oga" -> AudioFormat | ||
66 | ".ogg" -> AudioFormat | ||
67 | ".spx" -> AudioFormat | ||
68 | ".opus" -> AudioFormat | ||
69 | ".flac" -> AudioFormat | ||
70 | ".m4a" -> AudioFormat | ||
71 | ".mp3" -> AudioFormat | ||
72 | ".ogv" -> VideoFormat | ||
73 | ".ogx" -> VideoFormat | ||
74 | ".webm" -> VideoFormat | ||
75 | ".mkv" -> VideoFormat | ||
76 | ".mp4" -> VideoFormat | ||
77 | _ -> Unknown | ||
78 | |||
79 | |||
80 | type ItemFileProcessor a = | ||
81 | FilePath -- ^ Filesystem input base path | ||
82 | -> FilePath -- ^ Filesystem output base path | ||
83 | -> FileName -- ^ Output class (subdir) | ||
84 | -> ItemProcessor a | ||
85 | |||
86 | |||
87 | callFileProcessor :: (Path -> FileProcessor a) -> Cache a -> ItemFileProcessor a | ||
88 | callFileProcessor processorProvider withCache inputBase outputBase resClass itemPath resPath = | ||
89 | withCache (processorProvider resPath) | ||
90 | itemPath | ||
91 | (resClass /> resPath) | ||
92 | (localPath $ inputBase /> resPath) | ||
93 | (localPath $ outputBase /> (resClass /> resPath)) | ||
94 | |||
95 | |||
96 | itemFileProcessor :: Maybe Resolution -> Cache GalleryItemProps -> ItemFileProcessor GalleryItemProps | ||
97 | itemFileProcessor maxResolution = | ||
98 | callFileProcessor (flip processorFor maxResolution . formatFromPath) | ||
99 | where | ||
100 | processorFor :: Format -> Maybe Resolution -> FileProcessor GalleryItemProps | ||
101 | processorFor PictureFormat (Just maxRes) = | ||
102 | transformThenDescribe (resizePictureUpTo maxRes) getPictureProps | ||
103 | processorFor PictureFormat Nothing = | ||
104 | transformThenDescribe copyFileProcessor getPictureProps | ||
105 | processorFor PlainTextFormat _ = copyResource PlainText | ||
106 | processorFor MarkdownFormat _ = copyResource Markdown | ||
107 | processorFor PortableDocumentFormat _ = copyResource PDF | ||
108 | processorFor EPUBFormat _ = copyResource EPUB | ||
109 | processorFor VideoFormat _ = copyResource Video | ||
110 | processorFor AudioFormat _ = copyResource Audio | ||
111 | processorFor Unknown _ = copyResource Other | ||
112 | -- TODO: handle video reencoding and others? | ||
113 | |||
114 | |||
115 | thumbnailFileProcessor :: Resolution -> Cache (Maybe Thumbnail) -> ItemFileProcessor (Maybe Thumbnail) | ||
116 | thumbnailFileProcessor maxRes = | ||
117 | callFileProcessor (processorFor . formatFromPath) | ||
118 | where | ||
119 | processorFor :: Format -> FileProcessor (Maybe Thumbnail) | ||
120 | processorFor PictureFormat = transformThenDescribe (resizePictureUpTo maxRes) getThumbnailProps | ||
121 | processorFor _ = noopProcessor | ||