2010-12-24 07:42:05 +00:00
|
|
|
-- | 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.
|
|
|
|
--
|
2011-02-11 22:26:54 +00:00
|
|
|
-- A resource is represented by the 'Resource' type. This is basically just a
|
|
|
|
-- newtype wrapper around 'Identifier' -- but it has an important effect: it
|
|
|
|
-- guarantees that a resource with this identifier can be provided by one or
|
|
|
|
-- more resource providers.
|
|
|
|
--
|
|
|
|
-- Therefore, it is not recommended to read files directly -- you should use the
|
|
|
|
-- provided 'Resource' methods.
|
|
|
|
--
|
2011-04-05 20:14:49 +00:00
|
|
|
module Hakyll.Core.Resource.Provider
|
|
|
|
( ResourceProvider (..)
|
2010-12-31 12:28:31 +00:00
|
|
|
, resourceExists
|
2010-12-26 15:22:05 +00:00
|
|
|
, resourceDigest
|
2010-12-31 11:38:12 +00:00
|
|
|
, resourceModified
|
2010-12-24 07:42:05 +00:00
|
|
|
) where
|
|
|
|
|
2011-04-06 07:30:24 +00:00
|
|
|
import Control.Concurrent (MVar, readMVar, modifyMVar_)
|
2010-12-26 15:22:05 +00:00
|
|
|
import Control.Monad ((<=<))
|
|
|
|
import Data.Word (Word8)
|
2011-04-06 07:30:24 +00:00
|
|
|
import Data.Map (Map)
|
|
|
|
import qualified Data.Map as M
|
2010-12-24 07:42:05 +00:00
|
|
|
|
2010-12-26 15:12:24 +00:00
|
|
|
import qualified Data.ByteString.Lazy as LB
|
2010-12-26 15:22:05 +00:00
|
|
|
import OpenSSL.Digest.ByteString.Lazy (digest)
|
|
|
|
import OpenSSL.Digest (MessageDigest (MD5))
|
|
|
|
|
2010-12-31 11:38:12 +00:00
|
|
|
import Hakyll.Core.Store
|
2011-04-05 20:14:49 +00:00
|
|
|
import Hakyll.Core.Resource
|
2011-02-11 16:52:19 +00:00
|
|
|
|
2010-12-24 07:42:05 +00:00
|
|
|
-- | A value responsible for retrieving and listing resources
|
|
|
|
--
|
|
|
|
data ResourceProvider = ResourceProvider
|
|
|
|
{ -- | A list of all resources this provider is able to provide
|
2011-02-11 22:26:54 +00:00
|
|
|
resourceList :: [Resource]
|
2010-12-24 07:42:05 +00:00
|
|
|
, -- | Retrieve a certain resource as string
|
2011-02-11 22:26:54 +00:00
|
|
|
resourceString :: Resource -> IO String
|
2010-12-26 15:12:24 +00:00
|
|
|
, -- | Retrieve a certain resource as lazy bytestring
|
2011-02-11 22:26:54 +00:00
|
|
|
resourceLazyByteString :: Resource -> IO LB.ByteString
|
2011-04-06 07:30:24 +00:00
|
|
|
, -- | Cache keeping track of modified items
|
|
|
|
resourceModifiedCache :: MVar (Map Resource Bool)
|
2010-12-24 07:42:05 +00:00
|
|
|
}
|
2010-12-26 15:22:05 +00:00
|
|
|
|
2011-02-11 22:26:54 +00:00
|
|
|
-- | Check if a given identifier has a resource
|
2010-12-31 12:28:31 +00:00
|
|
|
--
|
2011-04-12 08:09:03 +00:00
|
|
|
resourceExists :: ResourceProvider -> Resource -> Bool
|
|
|
|
resourceExists provider = flip elem $ resourceList provider
|
2010-12-31 12:28:31 +00:00
|
|
|
|
2010-12-26 15:22:05 +00:00
|
|
|
-- | Retrieve a digest for a given resource
|
|
|
|
--
|
2011-02-11 22:26:54 +00:00
|
|
|
resourceDigest :: ResourceProvider -> Resource -> IO [Word8]
|
2010-12-26 15:22:05 +00:00
|
|
|
resourceDigest provider = digest MD5 <=< resourceLazyByteString provider
|
2010-12-31 11:38:12 +00:00
|
|
|
|
|
|
|
-- | Check if a resource was modified
|
|
|
|
--
|
2011-04-06 12:40:36 +00:00
|
|
|
resourceModified :: ResourceProvider -> Store -> Resource -> IO Bool
|
|
|
|
resourceModified provider store resource = do
|
2011-04-06 07:30:24 +00:00
|
|
|
cache <- readMVar mvar
|
|
|
|
case M.lookup resource cache of
|
|
|
|
-- Already in the cache
|
|
|
|
Just m -> return m
|
|
|
|
-- Not yet in the cache, check digests (if it exists)
|
|
|
|
Nothing -> do
|
2011-04-12 08:09:03 +00:00
|
|
|
m <- if resourceExists provider resource
|
2011-04-06 12:40:36 +00:00
|
|
|
then digestModified provider store resource
|
2011-04-06 07:30:24 +00:00
|
|
|
else return False
|
|
|
|
modifyMVar_ mvar (return . M.insert resource m)
|
|
|
|
return m
|
|
|
|
where
|
|
|
|
mvar = resourceModifiedCache provider
|
|
|
|
|
|
|
|
-- | Check if a resource digest was modified
|
|
|
|
--
|
2011-04-06 12:40:36 +00:00
|
|
|
digestModified :: ResourceProvider -> Store -> Resource -> IO Bool
|
|
|
|
digestModified provider store resource = do
|
2010-12-31 11:38:12 +00:00
|
|
|
-- Get the latest seen digest from the store
|
2011-04-12 08:09:03 +00:00
|
|
|
lastDigest <- storeGet store itemName identifier
|
2010-12-31 11:38:12 +00:00
|
|
|
-- Calculate the digest for the resource
|
2011-02-11 22:26:54 +00:00
|
|
|
newDigest <- resourceDigest provider resource
|
2010-12-31 11:38:12 +00:00
|
|
|
-- Check digests
|
|
|
|
if Just newDigest == lastDigest
|
|
|
|
-- All is fine, not modified
|
|
|
|
then return False
|
|
|
|
-- Resource modified; store new digest
|
2011-04-12 08:09:03 +00:00
|
|
|
else do storeSet store itemName identifier newDigest
|
2010-12-31 11:38:12 +00:00
|
|
|
return True
|
|
|
|
where
|
2011-04-12 08:09:03 +00:00
|
|
|
identifier = toIdentifier resource
|
2011-04-06 07:30:24 +00:00
|
|
|
itemName = "Hakyll.Core.ResourceProvider.digestModified"
|