module Rakka.Utils
- ( parseYesOrNo
+ ( yesOrNo
+ , parseYesOrNo
, maybeA
- , defaultTo
, deleteIfEmpty
+ , chomp
+ , guessMIMEType
)
where
import Control.Arrow
import Control.Arrow.ArrowList
+import qualified Data.ByteString.Lazy as Lazy (ByteString)
+import qualified Data.ByteString.Lazy.Char8 as L8 hiding (ByteString)
+import Magic
+import Network.HTTP.Lucu
+import System.IO.Unsafe
+
+
+yesOrNo :: Bool -> String
+yesOrNo True = "yes"
+yesOrNo False = "no"
parseYesOrNo :: ArrowChoice a => a String Bool
(x:_) -> returnA -< Just x
-defaultTo :: ArrowChoice a => b -> a (Maybe b) b
-defaultTo def
- = proc m -> case m of
- Nothing -> returnA -< def
- Just x -> returnA -< x
-
-
deleteIfEmpty :: (ArrowList a, ArrowChoice a) => a String String
deleteIfEmpty
= proc str -> do case str of
"" -> none -< ()
- _ -> returnA -< str
\ No newline at end of file
+ _ -> returnA -< str
+
+
+chomp :: String -> String
+chomp = reverse . snd . break (/= '\n') . reverse
+
+
+guessMIMEType :: Lazy.ByteString -> MIMEType
+guessMIMEType = read . unsafePerformIO . magicString magic . L8.unpack
+ where
+ magic :: Magic
+ magic = unsafePerformIO
+ $ do m <- magicOpen [MagicMime]
+ magicLoadDefault m
+ return m