Add proper end-point for challenge images

Also, fix bug in Swagger documentation
This commit is contained in:
Filip Gralinski 2021-05-28 07:17:45 +02:00
parent 3a276bbc9e
commit 0c9a21c40b
5 changed files with 36 additions and 11 deletions

View File

@ -217,7 +217,7 @@ instance Yesod App where
isAuthorized (ResetPasswordR _) _ = return Authorized
isAuthorized (ToggleSubmissionTagR _) _ = regularAuthorization
isAuthorized (ChallengeImageR _) _ = regularAuthorization
isAuthorized (ChallengeImgR _) _ = regularAuthorization
isAuthorized (ApiTxtScoreR _) _ = return Authorized

View File

@ -59,9 +59,9 @@ getListArchivedChallengesR :: Handler Html
getListArchivedChallengesR = generalListChallenges [ChallengeArchived ==. Just True]
imageUrl :: Entity Challenge -> Maybe (Route App)
imageUrl (Entity challengeId challenge) =
imageUrl (Entity _ challenge) =
case challengeImage challenge of
Just _ -> Just $ ChallengeImageR challengeId
Just _ -> Just $ ChallengeImgR $ challengeName challenge
Nothing -> Nothing
instance ToJSON (Entity Challenge) where
@ -134,8 +134,7 @@ instance ToJSON (Entity Version) where
instance ToSchema (Entity Version) where
declareNamedSchema _ = do
stringSchema <- declareSchemaRef (Proxy :: Proxy String)
booleanSchema <- declareSchemaRef (Proxy :: Proxy Bool)
return $ NamedSchema (Just "Challenge") $ mempty
return $ NamedSchema (Just "Version") $ mempty
& type_ .~ SwaggerObject
& properties .~
fromList [ ("deadline", stringSchema)
@ -176,11 +175,36 @@ getVersionInfoJsonR versionHash = do
return $ toJSON theVersion
getChallengeImageR :: ChallengeId -> Handler Html
getChallengeImageR challengeId = do
challenge <- runDB $ get404 challengeId
getChallengeImgR :: Text -> Handler Html
getChallengeImgR chName = do
(Entity _ challenge) <- runDB $ getBy404 $ UniqueName chName
case challengeImage challenge of
Just image -> do
addHeader "Content-Disposition" "attachment; filename=\"image.png\""
sendResponse (typePng, toContent image)
Nothing -> sendResponseStatus status202 ()
declareChallengeImgSwagger :: Declare (Definitions Schema) Swagger
declareChallengeImgSwagger = do
-- param schemas
let challengeNameSchema = toParamSchema (Proxy :: Proxy String)
return $ mempty
& paths .~
[ ("/api/challenge-img/{challengeName}",
mempty & get ?~ (mempty
& parameters .~ [ Inline $ mempty
& name .~ "challengeName"
& required ?~ True
& schema .~ ParamOther (mempty
& in_ .~ ParamPath
& paramSchema .~ challengeNameSchema) ]
& produces ?~ MimeList ["image/png"]
& description ?~ "Return the main image for a challenge"))
]
challengeImgApi :: Swagger
challengeImgApi = spec & definitions .~ defs
where
(defs, spec) = runDeclare declareChallengeImgSwagger mempty

View File

@ -29,6 +29,7 @@ apiDescription = generalApi
<> versionInfoApi
<> listTagsApi
<> myTeamsApi
<> challengeImgApi
generalApi :: Swagger
generalApi = (mempty :: Swagger)

View File

@ -21,7 +21,7 @@
/api/add-user AddUserR GET
/api/challenge-submission/#Text ChallengeSubmissionJsonR POST
/api/challenge-readme/#Text/markdown ChallengeReadmeInMarkdownR GET
/api/challenge-image/#ChallengeId ChallengeImageR GET
/api/challenge-img/#Text ChallengeImgR GET
/api/query/#Text QueryJsonR GET
/api/challenge-info/#Text ChallengeInfoJsonR GET
/api/version-info/#Text VersionInfoJsonR GET

View File

@ -1,9 +1,9 @@
$forall (Entity challengeId challenge) <- challenges
$forall (Entity _ challenge) <- challenges
<div class="panel panel-success">
<div class="panel-heading"><a .challenge-link href="@{ShowChallengeR (challengeName challenge)}">#{challengeTitle challenge}</a>
$if challengeStarred challenge
\ <span class="glyphicon glyphicon-star-empty"></span>
$if isJust (challengeImage challenge)
<div class="panel-body" style="background-image:url(@{ChallengeImageR challengeId}); background-color:lightgrey; background-blend-mode:screen;background-size: 100%;">#{challengeDescription challenge}
<div class="panel-body" style="background-image:url(@{ChallengeImgR $ challengeName challenge}); background-color:lightgrey; background-blend-mode:screen;background-size: 100%;">#{challengeDescription challenge}
$else
<div class="panel-body">#{challengeDescription challenge}