-- |

-- Module      : Hakyll.Images.Metadata

-- Description : Handling image metadata

-- Copyright   : (c) Laurent P René de Cotret, 2019 - present

-- License     : BSD3

-- Maintainer  : laurent.decotret@outlook.com

-- Stability   : unstable

-- Portability : portable

module Hakyll.Images.Metadata
  ( module Codec.Picture.Metadata,
    imageMetadata,
    metadata,
  )
where

import Codec.Picture (decodeImageWithMetadata)
import Codec.Picture.Metadata
import Hakyll.Core.Compiler (Compiler)
import Hakyll.Core.Item (Item, itemBody)
import Hakyll.Images.Common (Image (..))

-- | Extract metadata from an image. This function will throw an

-- error in case of a problem.

--

-- This function is for testing purposes.

metadata :: Image -> Metadatas
metadata :: Image -> Metadatas
metadata Image
im = ([Char] -> Metadatas)
-> ((DynamicImage, Metadatas) -> Metadatas)
-> Either [Char] (DynamicImage, Metadatas)
-> Metadatas
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either [Char] -> Metadatas
forall a. HasCallStack => [Char] -> a
error (DynamicImage, Metadatas) -> Metadatas
forall a b. (a, b) -> b
snd (ByteString -> Either [Char] (DynamicImage, Metadatas)
decodeImageWithMetadata (Image -> ByteString
image Image
im))

-- | Extract metadata from an image.

--

-- @

-- match "*.jpg" $ do

--     route idRoute

--     compile $ do

--         im <- loadImage

--         meta <- imageMetadata

--         doSomethingWithMetadata meta

--         ...

-- @

imageMetadata :: Item Image -> Compiler Metadatas
imageMetadata :: Item Image -> Compiler Metadatas
imageMetadata = Metadatas -> Compiler Metadatas
forall (m :: * -> *) a. Monad m => a -> m a
return (Metadatas -> Compiler Metadatas)
-> (Item Image -> Metadatas) -> Item Image -> Compiler Metadatas
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Image -> Metadatas
metadata (Image -> Metadatas)
-> (Item Image -> Image) -> Item Image -> Metadatas
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Item Image -> Image
forall a. Item a -> a
itemBody