From 85c0ed528e621a351b7bd18cec5483625803eb02 Mon Sep 17 00:00:00 2001 From: Lucy Cifferello <12953208+elvece@users.noreply.github.com> Date: Thu, 10 Nov 2022 16:01:45 -0700 Subject: [PATCH] only send icon if it exists --- src/Handler/Package/V0/Info.hs | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/Handler/Package/V0/Info.hs b/src/Handler/Package/V0/Info.hs index 5595909..15d6365 100644 --- a/src/Handler/Package/V0/Info.hs +++ b/src/Handler/Package/V0/Info.hs @@ -6,17 +6,19 @@ import Foundation (Handler, RegistryCtx (..)) import Handler.Util (tickleMAU) import Model (Category (..), EntityField (..)) import Settings (AppSettings (..)) -import Startlude (Generic, Show, Text, pure, ($), (.), (<$>), MonadIO (liftIO), decodeUtf8) +import Startlude (Generic, Show, Text, pure, ($), (.), (<$>), MonadIO (liftIO), decodeUtf8, Maybe) import Yesod (ToContent (..), ToTypedContent (..), YesodPersist (runDB), getsYesod) import Yesod.Core.Types (JSONResponse (..)) import System.FilePath (()) import Data.ByteString (readFile) +import System.Directory (doesFileExist) +import Data.Maybe (Maybe(..)) data InfoRes = InfoRes { name :: !Text , categories :: ![Text] - , icon :: !Text -- base64 + , icon :: Maybe Text -- base64 } deriving (Show, Generic) instance ToJSON InfoRes @@ -30,9 +32,12 @@ getInfoR :: Handler (JSONResponse InfoRes) getInfoR = do name <- getsYesod $ marketplaceName . appSettings iconFile <- getsYesod $ ( "icon") . iconPath . appSettings - icon' <- liftIO $ readFile iconFile - let icon = decodeUtf8 icon' - + existingIcon <- liftIO $ doesFileExist iconFile + icon <- if existingIcon + then do + icon' <- liftIO $ readFile iconFile + pure $ Just $ decodeUtf8 icon' + else pure $ Nothing allCategories <- runDB $ select $ do cats <- from $ table @Category