module Handler.Utils.Minio
  ( runAppMinio
  , minioIsDoesNotExist
  ) where

import Import.NoFoundation
import Foundation.Type

import Network.Minio (Minio)
import qualified Network.Minio as Minio


runAppMinio :: ( MonadHandler m, HandlerSite m ~ UniWorX
               , MonadThrow m
               , MonadPlus m
               )
            => Minio a -> m a
runAppMinio act = do
  conn <- hoistMaybe =<< getsYesod appUploadCache
  either throwM return <=< liftIO $ Minio.runMinioWith conn act

minioIsDoesNotExist :: HttpException -> Bool
minioIsDoesNotExist (HttpExceptionRequest _ (StatusCodeException resp _))
  = responseStatus resp == notFound404
minioIsDoesNotExist _ = False
