forked from jerith666/elbum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen-album.hs
591 lines (542 loc) · 20.6 KB
/
gen-album.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# OPTIONS_GHC -Wall #-}
{-# OPTIONS_GHC -Wcompat #-}
{-# OPTIONS_GHC -Werror #-}
{-# OPTIONS_GHC -Wincomplete-record-updates #-}
{-# OPTIONS_GHC -Wincomplete-uni-patterns #-}
{-# OPTIONS_GHC -Wno-type-defaults #-}
{-# OPTIONS_GHC -Wredundant-constraints #-}
import AlbumTypes
import Codec.Picture (DynamicImage (ImageRGBF), PixelRGBF, convertRGB8, dynamicMap, imageHeight, imageWidth, readImageWithMetadata, savePngImage)
import Codec.Picture.Metadata
import qualified Codec.Picture.Types
import qualified Codec.Picture.Types as M
import Control.Applicative
import Control.Concurrent.Async
import Control.Monad
import Control.Parallel.Strategies
import Data.Aeson (decode, encode)
import qualified Data.ByteString.Lazy.Char8 as C
import Data.Either
import Data.List (find, intercalate, sort)
import Data.Maybe
import Data.Time.Clock
import Data.Tuple
import Safe (readEitherSafe)
import ShrinkImage (scaleDownBoxAverage, shrinkImg)
import System.Directory
import System.Environment
import System.Exit
import System.FilePath
import System.IO
import System.Posix.Files
import Text.Regex
--
-- main & usage
--
main :: IO ()
main = do
args <- getArgs
case args of
[src, dest] -> writeAlbumOrList src dest
["shrink", newWidthStr, newHeightStr, oneImg] ->
case readEitherSafe newWidthStr of
Right newWidth ->
case readEitherSafe newHeightStr of
Right newHeight ->
shrinkImg newWidth newHeight oneImg
Left err ->
putStrLn $ "could not parse new height '" ++ newHeightStr ++ "': " ++ err
Left err -> putStrLn $ "could not parse new width '" ++ newWidthStr ++ "': " ++ err
_ -> usage
usage :: IO ()
usage = do
putStrLn "usage: gen-album <src> <dest>"
exitWith $ ExitFailure 1
--
-- configuration
--
thumbFilename :: String
thumbFilename = "thumbnail"
sizes :: Int -> [Int]
sizes maxWidth =
filter (maxWidth >) [1600, 1400, 1200, 1000, 800, 400, 200]
--
-- Album & AlbumList functions
--
writeAlbumOrList :: String -> String -> IO ()
writeAlbumOrList src dest = do
existingAlbumData <- findExistingAlbumData dest "album.json"
case existingAlbumData of
Nothing ->
putStrLn "album metadata not found; all image metadata will be read from source"
Just (_, modDate) ->
putStrLn $ "album metadata dated " ++ show modDate ++ " found; will use it for source images modified earlier than this"
putStrLn ""
eAlbumOrList <- genAlbumOrList src src dest existingAlbumData ChooseAutomatically
case eAlbumOrList of
Left errs -> do
putStrLn ""
putStrLn $ intercalate "\n" errs
exitWith $ ExitFailure 1
Right albumOrList ->
C.writeFile (dest </> "album.json") $ encode albumOrList
findExistingAlbumData :: FilePath -> String -> IO (Maybe (AlbumOrList, UTCTime))
findExistingAlbumData d albumFile = do
exists <- doesFileExist $ d </> albumFile
case exists of
False -> return Nothing
True -> do
bytes <- C.readFile $ d </> albumFile
date <- getModificationTime $ d </> albumFile
return $ swap . (,) date <$> decode bytes
data ThumbnailSpec
= ChooseAutomatically
| RequireExplicit
genAlbumOrList :: FilePath -> FilePath -> FilePath -> Maybe (AlbumOrList, UTCTime) -> ThumbnailSpec -> IO (Either [String] AlbumOrList)
genAlbumOrList srcRoot src dest existingAlbumData thumbspec = do
files <- filter (`notElem` [".", "..", thumbFilename]) <$> getDirectoryContents src
let afiles = map (src </>) (sort files)
epimgs <- procImgsOnly srcRoot src dest existingAlbumData afiles
case epimgs of
Left e ->
return $ Left e
Right pimgs -> do
subdirs <- dirsOnly afiles
let icount = length pimgs
idirs = length subdirs
if (icount > 0) && (idirs > 0)
then return $ Left ["directory " ++ src ++ " contains both images and subdirs, this is not supported"]
else case pimgs of
imgFirst : imgRest -> do
aOrErr <- genAlbum srcRoot src dest imgFirst imgRest
case aOrErr of
Left err ->
return $ Left [err]
Right a ->
return $ Right $ Leaf a
[] ->
case subdirs of
dirFirst : dirRest -> do
en <- genNode srcRoot src dest existingAlbumData thumbspec dirFirst dirRest
case en of
Left err ->
return $ Left err
Right n ->
return $ Right $ List n
[] ->
return $ Left ["no images or subdirs in " ++ src]
genNode :: FilePath -> FilePath -> FilePath -> Maybe (AlbumOrList, UTCTime) -> ThumbnailSpec -> FilePath -> [FilePath] -> IO (Either [String] AlbumList)
genNode srcRoot src dest existingAlbumData thumbspec dirFirst dirRest = do
ecFirst <- genAlbumOrList srcRoot dirFirst dest existingAlbumData RequireExplicit
case ecFirst of
Left err ->
return $ Left err
Right cFirst -> do
ecRest <- mapM (\dir -> genAlbumOrList srcRoot dir dest existingAlbumData RequireExplicit) dirRest
case lefts ecRest of
[] -> do
let cRest = rights ecRest
childImages = getChildImages $ cFirst : cRest
thumbOrErr <- findThumb srcRoot src dest childImages
case thumbOrErr of
Left err ->
case thumbspec of
ChooseAutomatically ->
case childImages of
[] ->
return $ Left ["cannot automatically chose a thumbnail when there are no subalbums " ++ titleForDir src]
firstChild : _ ->
return $
Right $
AlbumList
{ listTitle = titleForDir src,
listThumbnail = firstChild,
childFirst = cFirst,
childRest = cRest
}
RequireExplicit ->
return $ Left [err]
Right thumb ->
return $
Right $
AlbumList
{ listTitle = titleForDir src,
listThumbnail = thumb,
childFirst = cFirst,
childRest = cRest
}
errs ->
return $ Left $ concat errs
getChildImages :: [AlbumOrList] -> [Image]
getChildImages =
concatMap getChildImages1
getChildImages1 :: AlbumOrList -> [Image]
getChildImages1 albumOrList =
case albumOrList of
List albumTreeNode ->
getChildImages $ childFirst albumTreeNode : childRest albumTreeNode
Leaf album ->
imageFirst album : imageRest album
genAlbum :: FilePath -> FilePath -> FilePath -> Image -> [Image] -> IO (Either String Album)
genAlbum srcRoot src dest imgFirst imgRest = do
thumbOrErr <- findThumb srcRoot src dest $ imgFirst : imgRest
case thumbOrErr of
Left err ->
return $ Left err
Right thumb ->
return $
Right $
Album
{ title = titleForDir src,
thumbnail = thumb,
imageFirst = imgFirst,
imageRest = imgRest
}
titleForDir :: String -> String
titleForDir dir =
let dirName = last $ splitDirectories dir
in subRegex (mkRegex "^[0-9]+_") dirName ""
findThumb :: FilePath -> FilePath -> FilePath -> [Image] -> IO (Either String Image)
findThumb srcRoot src dest images = do
let thumbLink = src </> thumbFilename
thumbLinkFileExists <- doesFileExist thumbLink
if thumbLinkFileExists
then do
thumbLinkExists <- pathIsSymbolicLink thumbLink
if thumbLinkExists
then do
thumbPath <- readLink thumbLink
if isAbsolute $ makeRelative srcRoot thumbPath
then return $ Left $ src ++ " thumbnail link must point to a path inside " ++ srcRoot ++ ", but does not: " ++ thumbPath
else do
let thumbDest = fst $ destForRaw srcRoot dest thumbPath
isThumb i = equalFilePath thumbDest (dest </> url (srcSetFirst i))
thumb = find isThumb images
case thumb of
Nothing ->
return $ Left $ src ++ " thumbnail '" ++ thumbLink ++ "' -> '" ++ thumbPath ++ "' (" ++ thumbDest ++ ") does not point to any images in this album: " ++ concatMap (\i -> dest </> url (srcSetFirst i)) images
Just t ->
return $ Right t
else return $ Left $ thumbLink ++ " is not a symbolic link"
else return $ Left $ src ++ " does not contain a 'thumbnail' file"
--
-- Single-image functions
--
data FileClassification
= NotAnImage
| AlreadyProcessed Image
| ToProcess (DynamicImage, Metadatas)
toProc :: FileClassification -> Bool
toProc (ToProcess _) = True
toProc _ = False
procImgsOnly :: FilePath -> FilePath -> FilePath -> Maybe (AlbumOrList, UTCTime) -> [FilePath] -> IO (Either [String] [Image])
procImgsOnly _ _ _ _ [] = return $ Right []
procImgsOnly srcRoot src dest existingAlbumData files = do
let classify f = do
classification <- classifyFile srcRoot dest existingAlbumData f
return (f, classification)
putStrSameLn $ src ++ ": classifying " ++ show (length files) ++ " files ... "
classifications <- mapConcurrently classify files
let tpCt = show $ length $ filter toProc $ map snd classifications
putStr $ tpCt ++ " to process"
productionResults <- mapConcurrently (produceImage srcRoot dest) classifications
case lefts productionResults of
[] ->
return $ Right $ catMaybes $ rights productionResults
errs ->
return $ Left errs
produceImage :: FilePath -> FilePath -> (FilePath, FileClassification) -> IO (Either String (Maybe Image))
produceImage srcRoot dest (f, classification) = do
case classification of
NotAnImage ->
return $ Right Nothing
AlreadyProcessed img ->
return $ Right $ Just img
ToProcess metadata -> do
eImg <- procImage srcRoot dest (f, metadata)
case eImg of
Left err ->
return $ Left err
Right img ->
return $ Right $ Just img
classifyFile :: FilePath -> FilePath -> Maybe (AlbumOrList, UTCTime) -> FilePath -> IO FileClassification
classifyFile srcRoot destDir existingAlbumData file = do
mImg <- alreadyProcessed srcRoot destDir existingAlbumData file
case mImg of
Just img ->
return $ AlreadyProcessed img
Nothing -> do
mMetadata <- imgOnly file
case mMetadata of
Just metadata ->
return $ ToProcess $ snd metadata
Nothing ->
return NotAnImage
alreadyProcessed :: FilePath -> FilePath -> Maybe (AlbumOrList, UTCTime) -> FilePath -> IO (Maybe Image)
alreadyProcessed s d existingAlbumData f = do
let existingImage = do
-- note: this uses the Maybe instance of Monad
albumAndModTime <- existingAlbumData
matchExisting s f $ fst albumAndModTime
existingImageAndModDate = liftA2 (,) existingImage $ fmap snd existingAlbumData
destsExist maxWidth = do
let rawDest = fst $ destForRaw s d f
allDests = rawDest : map snd (shrinkDests s d f maxWidth)
existences <- mapM doesFileExist allDests
return (rawDest, allDests, existences)
srcModTimeOrNewerImage <- imageNewerThanSrc existingImageAndModDate f
case srcModTimeOrNewerImage of
Right newerImage -> do
(_, _, existences) <- destsExist $ x $ srcSetFirst newerImage
return $ case and existences of
True ->
Just $
-- work around possible stale image metadata produced
-- before we were smart enough to avoid creating larger
-- "shrunken" images
Image
{ altText = altText newerImage,
srcSetFirst = srcSetFirst newerImage,
srcSetRest =
filter (\srcSet -> x (srcSetFirst newerImage) > x srcSet) $
srcSetRest newerImage
}
False -> Nothing
Left srcModTime -> do
mi <- imgOnly f
case mi of
Nothing ->
return Nothing
Just i -> do
case Codec.Picture.Metadata.lookup Width $ snd $ snd i of
Nothing -> return Nothing
Just width -> do
(rawDest, allDests, existences) <- destsExist $ fromIntegral width
case and existences of
True -> do
-- the destination images all exist
-- the source image exists, but is newer than the metadata we have from album.json
-- the source image also loads cleanly and we have its intrinsic metadata
-- however, it's only safe to use that instrinsic metadata if the destination images
-- are all *newer* than the source image
destModTimes <- mapM getModificationTime allDests
case maximum destModTimes > srcModTime of
True -> createImageWithMetadataSize s d f rawDest i
False -> return Nothing
False -> return Nothing
shrinkDests :: FilePath -> FilePath -> FilePath -> Int -> [(Int, FilePath)]
shrinkDests s d f maxWidth =
map
( \size ->
( size,
fst $ destForShrink size s d f
)
)
$ sizes maxWidth
matchExisting :: FilePath -> FilePath -> AlbumOrList -> Maybe Image
matchExisting s f albumOrList = do
let relNames = splitDirectories $ makeRelative s f
in findImage relNames albumOrList
findImage :: [String] -> AlbumOrList -> Maybe Image
findImage [] _ = Nothing
findImage (name : names) albumOrList =
let getTitle aol =
case aol of
List list -> listTitle list
Leaf album -> title album
matchesName = (==) (titleForDir name) . getTitle
in case albumOrList of
List list ->
case filter matchesName $ childFirst list : childRest list of
[] -> Nothing
_ : _ : _ -> Nothing
[child] -> findImage names child
Leaf album ->
case filter ((==) (takeBaseName name) . altText) $ imageFirst album : imageRest album of
[] -> Nothing
_ : _ : _ -> Nothing
[child] -> Just child
imageNewerThanSrc :: Maybe (Image, UTCTime) -> FilePath -> IO (Either UTCTime Image)
imageNewerThanSrc mImageModTime src = do
srcModTime <- getModificationTime src
case mImageModTime of
Nothing ->
return $ Left srcModTime
Just (image, modTime) ->
case modTime >= srcModTime of
True ->
-- putStrLn $ "found image in album metadata @ " ++ (show modTime) ++ " newer than src " ++ src ++ " @ " ++ (show srcModTime) ++ ": " ++ (show image)
return $ Right image
False ->
-- putStrLn $ "found image in album metadata @ " ++ (show modTime) ++ " older than src " ++ src ++ " @ " ++ (show srcModTime) ++ ": " ++ (show image)
return $ Left srcModTime
createImageWithMetadataSize :: FilePath -> FilePath -> FilePath -> FilePath -> (FilePath, (DynamicImage, Metadatas)) -> IO (Maybe Image)
createImageWithMetadataSize s d f rawDest i = do
let mw = Codec.Picture.Metadata.lookup Width $ snd $ snd i
mh = Codec.Picture.Metadata.lookup Height $ snd $ snd i
wh = maybeTuple (mw, mh)
case wh of
Just (ww, hw) -> do
let t = takeBaseName f
w = fromIntegral ww
h = fromIntegral hw
srcSetFst =
ImgSrc
{ url = makeRelative d rawDest,
x = w,
y = h
}
sdToImgSrc sd =
let (xsm, ysm) = shrink (fst sd) w h
in ImgSrc
{ url = makeRelative d $ snd sd,
x = xsm,
y = ysm
}
srcSetRst = map sdToImgSrc $ shrinkDests s d f w
return $
Just $
Image
{ altText = t,
srcSetFirst = srcSetFst,
srcSetRest = srcSetRst
}
Nothing ->
return Nothing
imgOnly :: FilePath -> IO (Maybe (FilePath, (DynamicImage, Metadatas)))
imgOnly f = do
loadResult <- readImageWithMetadata f
case loadResult of
Left _ -> return Nothing
Right img ->
do
-- putStrSameLn $ "loaded " ++ show f
return $ Just (f, img)
procImage :: FilePath -> FilePath -> (FilePath, (DynamicImage, Metadatas)) -> IO (Either String Image)
procImage s d (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
(srcSetFst, srcSetRst) <- procSrcSet s d f (fst i) w h
return $
Right $
Image
{ altText = t,
srcSetFirst = srcSetFst,
srcSetRest = srcSetRst
}
else 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"
procSrcSet :: FilePath -> FilePath -> FilePath -> DynamicImage -> Int -> Int -> IO (ImgSrc, [ImgSrc])
procSrcSet s d f i w h = do
let shrunkenSrcs = map (shrinkImgSrc s d f i w h) (sizes w) `using` parList rdeepseq
shrunken = map third shrunkenSrcs
rawImg <- copyRawImgSrc s d f w h
-- putStrSameLn $ "processing " ++ show f ++ " "
mapM_ (writeShrunkenImgSrc . fstSnd) shrunkenSrcs
return (rawImg, shrunken)
writeShrunkenImgSrc :: (Codec.Picture.Types.Image PixelRGBF, FilePath) -> IO ()
writeShrunkenImgSrc (ism, fsmpath) = do
createDirectoryIfMissing True $ takeDirectory fsmpath
hFlush stdout
savePngImage fsmpath $ ImageRGBF ism
shrinkImgSrc :: FilePath -> FilePath -> FilePath -> DynamicImage -> Int -> Int -> Int -> (Codec.Picture.Types.Image PixelRGBF, FilePath, ImgSrc)
shrinkImgSrc s d f i w h maxwidth =
let (xsm, ysm) = shrink maxwidth w h
fsmpath = fst $ destForShrink maxwidth s d f
rgbfImg = M.promoteImage $ convertRGB8 i
rgbfImgSmall = scaleDownBoxAverage xsm ysm rgbfImg
in ( rgbfImgSmall,
fsmpath,
ImgSrc
{ url = makeRelative d fsmpath,
x = xsm,
y = ysm
}
)
copyRawImgSrc :: FilePath -> FilePath -> FilePath -> Int -> Int -> IO ImgSrc
copyRawImgSrc s d fpath w h = do
let (dest, _) = destForRaw s d fpath
createDirectoryIfMissing True $ takeDirectory dest
copyFile fpath dest
setFileMode dest $ foldl unionFileModes ownerReadMode [groupReadMode, otherReadMode]
-- putStrSameLn $ "copied " ++ f
return
ImgSrc
{ url = makeRelative d dest,
x = w,
y = h
}
destForRaw :: FilePath -> FilePath -> FilePath -> (FilePath, FilePath)
destForRaw =
destFor takeFileName
destForShrink :: Int -> FilePath -> FilePath -> FilePath -> (FilePath, FilePath)
destForShrink maxwidth =
destFor (\f -> takeFileName (dropExtension f) ++ "." ++ show maxwidth ++ ".png")
destFor :: (FilePath -> FilePath) -> FilePath -> FilePath -> FilePath -> (FilePath, FilePath)
destFor fNameMaker src dest fileInSrc =
let srel = takeDirectory $ makeRelative src fileInSrc
fName = fNameMaker fileInSrc
in (dest </> srel </> fName, fName)
shrink :: Int -> Int -> Int -> (Int, Int)
shrink maxwidth w h =
let factor = fromIntegral maxwidth / fromIntegral w
scale dim = floor (fromIntegral dim * factor)
in (scale w, scale h)
--
-- file/directory utilities
--
dirsOnly :: [FilePath] -> IO [FilePath]
dirsOnly = filterM doesDirectoryExist
readLink :: FilePath -> IO FilePath
readLink f = do
isLink <- pathIsSymbolicLink f
if isLink
then do
target <- getSymbolicLinkTarget f
if isAbsolute target
then readLink target
else do
let ftgt = takeDirectory f </> target
readLink ftgt
else return f
--
-- 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
fstSnd :: (a, b, c) -> (a, b)
fstSnd (a, b, _) =
(a, b)
third :: (a, b, c) -> c
third (_, _, c) =
c