-- Fetch an image into the disk cache (if not already there) and return it as a lazy ByteString
fetchDiskCache :: ImageCache -> Manager -> B.ByteString -> FilePath -> IO BL.ByteString
fetchDiskCache ic manager uri cacheFn = do
    bs <- tryIOError $ BL.readFile cacheFn -- Already in the disk cache?
    case bs of
        Left ex -> do
            unless (isDoesNotExistError ex) $ -- Only handle the missing file ones silently
                throwIO ex
            -- No, fetch image
            if   uriIsHTTP uri
            then runResourceT $ do
                     req <- parseUrl $ B8.unpack uri
                     res <- httpLbs req manager
                     -- Store in disk cache
                     liftIO $ do
                         incCacheMisses ic
                         -- TODO: Misses HTTP protocol overhead
                         incCacheBytesTransf ic . fromIntegral . BL.length . responseBody $ res
                         -- TODO: Disk cache writes disabled for now
                         -- BL.writeFile cacheFn $ responseBody res
                     return $ responseBody res
            else BL.readFile $ B8.unpack uri -- TODO: We should restrict fetches of disk files
                                             --       to certain directories, might be a security
                                             --       issue otherwise
        Right x -> incCacheDiskHits ic >> return x
fetchDiskCache :: ImageCache -> Manager -> B.ByteString -> FilePath -> IO BL.ByteString
fetchDiskCache ic manager uri cacheFn = do
    bs <- tryIOError $ BL.readFile cacheFn -- Already in the disk cache?
    case bs of
        Left ex -> do
            unless (isDoesNotExistError ex) $ -- Only handle the missing file ones silently
                throwIO ex
            -- No, fetch image
            if   uriIsHTTP uri
            then runResourceT $ do
                     req <- parseUrl $ B8.unpack uri
                     res <- httpLbs req manager
                     -- Store in disk cache
                     liftIO $ do
                         incCacheMisses ic
                         -- TODO: Misses HTTP protocol overhead
                         incCacheBytesTransf ic . fromIntegral . BL.length . responseBody $ res
                         -- TODO: Disk cache writes disabled for now
                         -- BL.writeFile cacheFn $ responseBody res
                     return $ responseBody res
            else BL.readFile $ B8.unpack uri -- TODO: We should restrict fetches of disk files
                                             --       to certain directories, might be a security
                                             --       issue otherwise
        Right x -> incCacheDiskHits ic >> return x
fetchDiskCache ic manager uri cacheFn = do
    bs <- tryIOError $ BL.readFile cacheFn -- Already in the disk cache?
    case bs of
        Left ex -> do
            unless (isDoesNotExistError ex) $ -- Only handle the missing file ones silently
                throwIO ex
            -- No, fetch image
            if   uriIsHTTP uri
            then runResourceT $ do
                     req <- parseUrl $ B8.unpack uri
                     res <- httpLbs req manager
                     -- Store in disk cache
                     liftIO $ do
                         incCacheMisses ic
                         -- TODO: Misses HTTP protocol overhead
                         incCacheBytesTransf ic . fromIntegral . BL.length . responseBody $ res
                         -- TODO: Disk cache writes disabled for now
                         -- BL.writeFile cacheFn $ responseBody res
                     return $ responseBody res
            else BL.readFile $ B8.unpack uri -- TODO: We should restrict fetches of disk files
                                             --       to certain directories, might be a security
                                             --       issue otherwise
        Right x -> incCacheDiskHits ic >> return x