|
8 | 8 | use App\Entity\Contest;
|
9 | 9 | use App\Entity\ContestProblem;
|
10 | 10 | use App\Entity\Problem;
|
| 11 | +use App\Entity\ProblemAttachment; |
11 | 12 | use App\Service\ConfigurationService;
|
12 | 13 | use App\Service\DOMJudgeService;
|
13 | 14 | use App\Service\EventLogService;
|
@@ -436,6 +437,58 @@ public function statementAction(Request $request, string $id): Response
|
436 | 437 | return $contestProblem->getProblem()->getProblemStatementStreamedResponse();
|
437 | 438 | }
|
438 | 439 |
|
| 440 | + /** |
| 441 | + * Get an attachment for given problem for this contest. |
| 442 | + * @throws NonUniqueResultException |
| 443 | + */ |
| 444 | + #[Rest\Get('/{id}/attachment/{filename}')] |
| 445 | + #[OA\Response( |
| 446 | + response: 200, |
| 447 | + description: 'Returns the given problem attachment for this contest' |
| 448 | + )] |
| 449 | + #[OA\Parameter(ref: '#/components/parameters/id')] |
| 450 | + #[OA\Parameter( |
| 451 | + name: 'filename', |
| 452 | + description: 'The filename of the attachment to get', |
| 453 | + in: 'query', |
| 454 | + required: true, |
| 455 | + schema: new OA\Schema(type: 'string') |
| 456 | + )] |
| 457 | + public function attachmentAction(Request $request, string $id, string $filename): Response |
| 458 | + { |
| 459 | + $contestProblemData = $this |
| 460 | + ->getQueryBuilder($request) |
| 461 | + ->setParameter('id', $id) |
| 462 | + ->andWhere(sprintf('%s = :id', $this->getIdField())) |
| 463 | + ->getQuery() |
| 464 | + ->getOneOrNullResult(); |
| 465 | + |
| 466 | + if ($contestProblemData === null) { |
| 467 | + throw new NotFoundHttpException(sprintf('Object with ID \'%s\' not found', $id)); |
| 468 | + } |
| 469 | + |
| 470 | + /** @var ContestProblem $contestProblem */ |
| 471 | + $contestProblem = $contestProblemData[0]; |
| 472 | + |
| 473 | + /** @var ProblemAttachment|null $attachment */ |
| 474 | + $attachment = $this->em->createQueryBuilder() |
| 475 | + ->from(ProblemAttachment::class, 'a') |
| 476 | + ->join('a.content', 'c') |
| 477 | + ->select('a, c') |
| 478 | + ->andWhere('a.problem = :problem') |
| 479 | + ->andWhere('a.name = :filename') |
| 480 | + ->setParameter('problem', $contestProblem->getProblem()) |
| 481 | + ->setParameter('filename', $filename) |
| 482 | + ->getQuery() |
| 483 | + ->getOneOrNullResult(); |
| 484 | + |
| 485 | + if ($attachment === null) { |
| 486 | + throw new NotFoundHttpException(sprintf('Attachment with filename \'%s\' not found for problem with ID \'%s\'', $filename, $id)); |
| 487 | + } |
| 488 | + |
| 489 | + return $attachment->getStreamedResponse(); |
| 490 | + } |
| 491 | + |
439 | 492 | protected function getQueryBuilder(Request $request): QueryBuilder
|
440 | 493 | {
|
441 | 494 | $contestId = $this->getContestId($request);
|
|
0 commit comments