summaryrefslogtreecommitdiff
path: root/src/Hakyll/Core/ResourceProvider.hs
blob: d5f2ea3ef67522b486b736463f8987e0e921994a (plain)
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
-- | This module provides an API for resource providers. Resource providers
-- allow Hakyll to get content from resources; the type of resource depends on
-- the concrete instance.
--
module Hakyll.Core.ResourceProvider
    ( ResourceProvider (..)
    , resourceExists
    , resourceDigest
    , resourceModified
    ) where

import Control.Monad ((<=<))
import Data.Word (Word8)

import qualified Data.ByteString.Lazy as LB
import OpenSSL.Digest.ByteString.Lazy (digest)
import OpenSSL.Digest (MessageDigest (MD5))

import Hakyll.Core.Identifier
import Hakyll.Core.Store

-- | A value responsible for retrieving and listing resources
--
data ResourceProvider = ResourceProvider
    { -- | A list of all resources this provider is able to provide
      resourceList           :: [Identifier]
    , -- | Retrieve a certain resource as string
      resourceString         :: Identifier -> IO String
    , -- | Retrieve a certain resource as lazy bytestring
      resourceLazyByteString :: Identifier -> IO LB.ByteString
    }

-- | Check if a given resource exists
--
resourceExists :: ResourceProvider -> Identifier -> Bool
resourceExists provider = flip elem $ resourceList provider

-- | Retrieve a digest for a given resource
--
resourceDigest :: ResourceProvider -> Identifier -> IO [Word8]
resourceDigest provider = digest MD5 <=< resourceLazyByteString provider

-- | Check if a resource was modified
--
resourceModified :: ResourceProvider -> Identifier -> Store -> IO Bool
resourceModified provider identifier store = do
    -- Get the latest seen digest from the store
    lastDigest <- storeGet store itemName identifier
    -- Calculate the digest for the resource
    newDigest <- resourceDigest provider identifier
    -- Check digests
    if Just newDigest == lastDigest
        -- All is fine, not modified
        then return False
        -- Resource modified; store new digest
        else do storeSet store itemName identifier newDigest
                return True
  where
    itemName = "Hakyll.Core.ResourceProvider.resourceModified"