forked from jerith666/elbum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check-pics.hs
194 lines (165 loc) · 4.96 KB
/
check-pics.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import System.Environment
import System.IO
import System.Exit
import System.Directory
import System.FilePath
import System.Posix.Files
import Data.Either
import Data.List
import Data.Maybe
import Control.Monad
import Text.Regex
import Codec.Picture hiding (Image)
import Codec.Picture.Types hiding (Image)
import Codec.Picture.Metadata hiding (Image)
import Vision.Primitive
import Vision.Primitive.Shape
import Vision.Image hiding (Image, map)
import Vision.Image.Transform
import Vision.Image.JuicyPixels
import Data.Aeson (encode)
import qualified Data.ByteString.Lazy.Char8 as C
import AlbumTypes
--
-- main & usage
--
main = do
args <- getArgs
case args of
src:[] -> writeAlbumOrList src
_ -> usage
usage = do
putStrLn "usage: check-pics <src>"
exitWith $ ExitFailure 1
--
-- configuration
--
thumbFilename = "thumbnail"
--
-- Album & AlbumList functions
--
writeAlbumOrList :: String -> IO ()
writeAlbumOrList src = do
eAlbumOrList <- genAlbumOrList src src True
case eAlbumOrList of
Left err -> do
putStrLn ""
putStrLn err
exitWith $ ExitFailure 1
Right albumOrList ->
putStrLn "all images are okay"
genAlbumOrList :: FilePath -> FilePath -> Bool -> IO (Either String ())
genAlbumOrList srcRoot src autoThumb = do
files <- filter (`notElem` [".","..",thumbFilename]) <$> getDirectoryContents src
let afiles = map (\f -> src </> f) (sort files)
epimgs <- procImgsOnly srcRoot afiles
case epimgs of
Left e -> do
return $ Left e
Right _ -> do
subdirs <- dirsOnly afiles
if length subdirs > 0 then do
en <- genNode srcRoot src autoThumb subdirs
case en of
Left err ->
return $ Left $ err
Right _ ->
return $ Right ()
else do
return $ Right ()
genNode :: FilePath -> FilePath -> Bool -> [FilePath] -> IO (Either String ())
genNode srcRoot src autoThumb dirs = do
ecFirst <- genAlbumOrList srcRoot (head dirs) False
case ecFirst of
Left err ->
return $ Left $ err
Right cFirst -> do
ecRest <- mapM (\dir -> genAlbumOrList srcRoot dir False) (tail dirs)
case lefts ecRest of
[] -> do
return $ Right ()
errs -> do
return $ Left $ concat $ intersperse "\n" errs
--
-- Single-image functions
--
procImgsOnly :: FilePath -> [FilePath] -> IO (Either String ())
procImgsOnly _ [] = return $ Right ()
procImgsOnly s (f:fs) = do
mi <- imgOnly f
case mi of
Nothing -> do
is <- procImgsOnly s fs
return is
Just pdi -> do
-- this ordering is key to ensuring memory usage remains relatively constant
-- we have to process the first image completely (load it, save it out at all
-- sizes, and convert to an Image) before moving on to the others
epi <- procImage s pdi
eis <- procImgsOnly s fs
case epi of
Left e -> do
case eis of
Left e2 -> do
return $ Left $ e ++ "\n" ++ e2
Right _ -> do
return $ Left $ e
Right pi -> do
case eis of
Left e ->
return $ Left e
Right _ ->
return $ Right $ ()
imgOnly :: FilePath -> IO (Maybe (FilePath, (DynamicImage, Metadatas)))
imgOnly f = do
loadResult <- readImageWithMetadata f
case loadResult of
Left err -> do return Nothing
Right img ->
do
putStrSameLn $ "checking " ++ (show f)
return $ Just (f, img)
procImage :: FilePath -> (FilePath, (DynamicImage, Metadatas)) -> IO (Either String ())
procImage s (f,i) = do
let w = dynamicMap imageWidth $ fst i
h = dynamicMap imageHeight $ fst i
mw = Codec.Picture.Metadata.lookup Width $ snd i
mh = Codec.Picture.Metadata.lookup Height $ snd i
mwh = maybeTuple (mw, mh)
t = takeBaseName f
case mwh of
Nothing ->
return $ Left $ "image " ++ f ++ " has no size metadata, album regen will silently drop it"
Just (ww, hh) ->
if fromIntegral ww == w && fromIntegral hh == h then do
return $ Right ()
else do
return $ Left $ "image " ++ f ++ " has intrinsic w*h of " ++ (show w) ++ "*" ++ (show h) ++ " but metadata w*h of " ++ (show ww) ++ "*" ++ (show hh) ++ "; to repair, load in The Gimp, then choose file -> overwrite"
--
-- file/directory utilities
--
dirsOnly :: [FilePath] -> IO [FilePath]
dirsOnly = filterM doesDirectoryExist
--
-- I/O utilities
--
putStrSameLn :: String -> IO ()
putStrSameLn s = do
putStr "\r "
putStr "\r"
putStr s
hFlush stdout
--
-- basic utilities
--
maybeTuple :: (Maybe a, Maybe b) -> Maybe (a, b)
maybeTuple (ma, mb) =
case ma of
Just a ->
case mb of
Just b ->
Just (a, b)
Nothing ->
Nothing
Nothing ->
Nothing