2019-01-23 16:39:02 +00:00
|
|
|
{- Interface to libmagic
|
|
|
|
-
|
|
|
|
- Copyright 2019 Joey Hess <id@joeyh.name>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
{-# LANGUAGE CPP #-}
|
|
|
|
|
|
|
|
module Annex.Magic (
|
|
|
|
Magic,
|
|
|
|
MimeType,
|
|
|
|
initMagicMimeType,
|
|
|
|
getMagicMimeType,
|
|
|
|
) where
|
|
|
|
|
|
|
|
#ifdef WITH_MAGICMIME
|
|
|
|
import Magic
|
|
|
|
import Utility.Env
|
|
|
|
import Common
|
|
|
|
#else
|
|
|
|
type Magic = ()
|
|
|
|
#endif
|
|
|
|
|
|
|
|
initMagicMimeType :: IO (Maybe Magic)
|
|
|
|
#ifdef WITH_MAGICMIME
|
|
|
|
initMagicMimeType = catchMaybeIO $ do
|
|
|
|
m <- magicOpen [MagicMimeType]
|
|
|
|
liftIO $ getEnv "GIT_ANNEX_DIR" >>= \case
|
|
|
|
Nothing -> magicLoadDefault m
|
|
|
|
Just d -> magicLoad m
|
|
|
|
(d </> "magic" </> "magic.mgc")
|
|
|
|
return m
|
|
|
|
#else
|
2019-01-24 04:10:16 +00:00
|
|
|
initMagicMimeType = return Nothing
|
2019-01-23 16:39:02 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
type MimeType = String
|
|
|
|
|
|
|
|
getMagicMimeType :: Magic -> FilePath -> IO (Maybe MimeType)
|
|
|
|
#ifdef WITH_MAGICMIME
|
|
|
|
getMagicMimeType m f = Just <$> magicFile m f
|
|
|
|
#else
|
2019-02-09 17:49:46 +00:00
|
|
|
getMagicMimeType _ _ = return Nothing
|
2019-01-23 16:39:02 +00:00
|
|
|
#endif
|