--------------------------------------------------------------------------------
-- | This module provides an wrapper API around the file system which does some
-- caching.
module Hakyll.Core.Provider
    ( -- * Constructing resource providers
      Internal.Provider
    , newProvider

      -- * Querying resource properties
    , Internal.resourceList
    , Internal.resourceExists
    , Internal.resourceFilePath
    , Internal.resourceModified
    , Internal.resourceModificationTime

      -- * Access to raw resource content
    , Internal.resourceString
    , Internal.resourceLBS

      -- * Access to metadata and body content
    , Internal.resourceMetadata
    , Internal.resourceBody
    ) where


--------------------------------------------------------------------------------
import qualified Hakyll.Core.Provider.Internal      as Internal
import qualified Hakyll.Core.Provider.MetadataCache as Internal
import           Hakyll.Core.Store                  (Store)


--------------------------------------------------------------------------------
-- | Create a resource provider
newProvider :: Store                   -- ^ Store to use
            -> (FilePath -> IO Bool)   -- ^ Should we ignore this file?
            -> FilePath                -- ^ Search directory
            -> IO Internal.Provider    -- ^ Resulting provider
newProvider :: Store -> (FilePath -> IO Bool) -> FilePath -> IO Provider
newProvider Store
store FilePath -> IO Bool
ignore FilePath
directory = do
    -- Delete metadata cache where necessary
    Provider
p <- Store -> (FilePath -> IO Bool) -> FilePath -> IO Provider
Internal.newProvider Store
store FilePath -> IO Bool
ignore FilePath
directory
    (Identifier -> IO ()) -> [Identifier] -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (Provider -> Identifier -> IO ()
Internal.resourceInvalidateMetadataCache Provider
p) ([Identifier] -> IO ()) -> [Identifier] -> IO ()
forall a b. (a -> b) -> a -> b
$
        (Identifier -> Bool) -> [Identifier] -> [Identifier]
forall a. (a -> Bool) -> [a] -> [a]
filter (Provider -> Identifier -> Bool
Internal.resourceModified Provider
p) ([Identifier] -> [Identifier]) -> [Identifier] -> [Identifier]
forall a b. (a -> b) -> a -> b
$ Provider -> [Identifier]
Internal.resourceList Provider
p
    Provider -> IO Provider
forall (m :: * -> *) a. Monad m => a -> m a
return Provider
p