-- | Fetch an image or other item from the local filesystem or the net.
-- Returns raw content and maybe mime type.
fetchItem :: Maybe String -> String
          -> IO (Either E.SomeException (BS.ByteString, Maybe String))
fetchItem sourceURL s
  | isURI s         = openURL s
  | otherwise       =
      case sourceURL >>= parseURIReference of
           Just u  -> case parseURIReference s of
                           Just s' -> openURL $ show $
                                        s' `nonStrictRelativeTo` u
                           Nothing -> openURL $ show u ++ "/" ++ s
           Nothing -> E.try readLocalFile
  where readLocalFile = do
          let mime = case takeExtension s of
                          ".gz" -> getMimeType $ dropExtension s
                          x     -> getMimeType x
          cont <- BS.readFile s
          return (cont, mime)

-- | Read from a URL and return raw data and maybe mime type.
fetchItem :: Maybe String -> String
          -> IO (Either E.SomeException (BS.ByteString, Maybe String))
fetchItem sourceURL s
  | isURI s         = openURL s
  | otherwise       =
      case sourceURL >>= parseURIReference of
           Just u  -> case parseURIReference s of
                           Just s' -> openURL $ show $
                                        s' `nonStrictRelativeTo` u
                           Nothing -> openURL $ show u ++ "/" ++ s
           Nothing -> E.try readLocalFile
  where readLocalFile = do
          let mime = case takeExtension s of
                          ".gz" -> getMimeType $ dropExtension s
                          x     -> getMimeType x
          cont <- BS.readFile s
          return (cont, mime)

-- | Read from a URL and return raw data and maybe mime type.
fetchItem sourceURL s
  | isURI s         = openURL s
  | otherwise       =
      case sourceURL >>= parseURIReference of
           Just u  -> case parseURIReference s of
                           Just s' -> openURL $ show $
                                        s' `nonStrictRelativeTo` u
                           Nothing -> openURL $ show u ++ "/" ++ s
           Nothing -> E.try readLocalFile
  where readLocalFile = do
          let mime = case takeExtension s of
                          ".gz" -> getMimeType $ dropExtension s
                          x     -> getMimeType x
          cont <- BS.readFile s
          return (cont, mime)

-- | Read from a URL and return raw data and maybe mime type.