From 7232c482924ebd29a8b64b9497c0a4454e1a5cf5 Mon Sep 17 00:00:00 2001 From: Lucy Cifferello Date: Tue, 7 Jul 2020 17:38:17 -0600 Subject: [PATCH] refactor for boolean blindness --- src/Handler/Apps.hs | 28 ++++++++++++++-------------- src/Lib/Error.hs | 5 +++++ src/Lib/Registry.hs | 6 ++++++ src/Lib/Types/FileSystem.hs | 6 ++++++ src/Orphans/Yesod.hs | 1 + 5 files changed, 32 insertions(+), 14 deletions(-) create mode 100644 src/Lib/Types/FileSystem.hs diff --git a/src/Handler/Apps.hs b/src/Handler/Apps.hs index e799c8b..2f3160d 100644 --- a/src/Handler/Apps.hs +++ b/src/Handler/Apps.hs @@ -16,7 +16,6 @@ import Data.Char import Data.Conduit import qualified Data.Conduit.Binary as CB import qualified Data.Text as T -import Data.List import qualified GHC.Show (Show (..)) import Network.HTTP.Types import System.Directory @@ -27,6 +26,8 @@ import Foundation import Lib.Registry import Lib.Semver import Lib.Types.Semver +import Lib.Types.FileSystem +import Lib.Error import System.FilePath ((<.>), ()) import System.Posix.Files (fileSize, getFileStatus) import Settings @@ -71,27 +72,26 @@ getApp rootDir ext@(Extension appId) = do case getSpecifiedAppVersion spec appVersions of Nothing -> notFound Just (RegisteredAppVersion (appVersion, filePath)) -> do - let isApp = isInfixOf "apps" rootDir - exists <- liftIO $ doesFileExist filePath - determineEvent exists isApp filePath appVersion + exists <- liftIO $ doesFileExist filePath >>= \case + True -> pure Existent + False -> pure NonExistent + determineEvent exists (extension ext) filePath appVersion where - determineEvent True False fp _ = do + determineEvent :: FileExistence -> String -> FilePath -> AppVersion -> HandlerFor AgentCtx TypedContent + -- for system files + determineEvent Existent "" fp _ = do sz <- liftIO $ fileSize <$> getFileStatus fp addHeader "Content-Length" (show sz) respondSource typePlain $ CB.sourceFile fp .| awaitForever sendChunkBS - determineEvent True True fp av = do + -- for app files + determineEvent Existent "s9pk" fp av = do _ <- recordMetrics appId rootDir av sz <- liftIO $ fileSize <$> getFileStatus fp addHeader "Content-Length" (show sz) respondSource typePlain $ CB.sourceFile fp .| awaitForever sendChunkBS - determineEvent False _ _ _ = notFound - - - -errOnNothing :: MonadHandler m => Status -> Text -> Maybe a -> m a -errOnNothing status res entity = case entity of - Nothing -> sendResponseStatus status res - Just a -> pure a + -- for png files + determineEvent Existent _ _ _ = notFound + determineEvent NonExistent _ _ _ = notFound recordMetrics :: String -> FilePath -> AppVersion -> HandlerFor AgentCtx () recordMetrics appId rootDir appVersion = do diff --git a/src/Lib/Error.hs b/src/Lib/Error.hs index d3e9e54..a1af45f 100644 --- a/src/Lib/Error.hs +++ b/src/Lib/Error.hs @@ -55,3 +55,8 @@ handleS9ErrNuclear :: MonadIO m => S9ErrT m a -> m a handleS9ErrNuclear action = runExceptT action >>= \case Left e -> throwIO e Right a -> pure a + +errOnNothing :: MonadHandler m => Status -> Text -> Maybe a -> m a +errOnNothing status res entity = case entity of + Nothing -> sendResponseStatus status res + Just a -> pure a \ No newline at end of file diff --git a/src/Lib/Registry.hs b/src/Lib/Registry.hs index 0db0957..fb7901e 100644 --- a/src/Lib/Registry.hs +++ b/src/Lib/Registry.hs @@ -16,6 +16,7 @@ import Yesod.Core import Lib.Semver import Lib.Types.Semver +import Data.Char type Registry = HashMap String (HashMap AppVersion FilePath) @@ -50,6 +51,11 @@ type S9PK = Extension "s9pk" type SYS_EXTENSIONLESS = Extension "" type PNG = Extension "png" +data Extensions = S9PK | PNG | EMPTY + deriving (Read) +instance Show Extensions where + show a = fmap toLower $ show a + instance IsString (Extension a) where fromString = Extension diff --git a/src/Lib/Types/FileSystem.hs b/src/Lib/Types/FileSystem.hs new file mode 100644 index 0000000..229cc39 --- /dev/null +++ b/src/Lib/Types/FileSystem.hs @@ -0,0 +1,6 @@ +module Lib.Types.FileSystem where + + import Startlude + + data FileExistence = Existent | NonExistent + deriving (Eq, Show) \ No newline at end of file diff --git a/src/Orphans/Yesod.hs b/src/Orphans/Yesod.hs index 778c7b2..c07fc37 100644 --- a/src/Orphans/Yesod.hs +++ b/src/Orphans/Yesod.hs @@ -10,3 +10,4 @@ instance ToJSON a => ToContent [a] where toContent = toContent . toJSON . fmap toJSON instance ToJSON a => ToTypedContent [a] where toTypedContent = toTypedContent . toJSON . fmap toJSON + \ No newline at end of file