Skip to content

Commit c6097fe

Browse files
committed
Check if there would be any attachments/samples in the contest samples.zip
We always try to exit early and skip the actual adding. As checking and creation are more or less the same code we wrap the check inside the actual creation to prevent duplication.
1 parent 2a10433 commit c6097fe

File tree

1 file changed

+43
-10
lines changed

1 file changed

+43
-10
lines changed

webapp/src/Service/DOMJudgeService.php

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -830,7 +830,7 @@ public function getSamplesZipContent(ContestProblem $contestProblem): string
830830
return $zipFileContents;
831831
}
832832

833-
protected function addSamplesToZip(ZipArchive $zip, ContestProblem $problem, ?string $directory = null): void
833+
protected function addSamplesToZip(?ZipArchive $zip, ContestProblem $problem, ?string $directory = null, bool $fullZip = true): bool
834834
{
835835
/** @var Testcase[] $testcases */
836836
$testcases = $this->em->createQueryBuilder()
@@ -849,6 +849,9 @@ protected function addSamplesToZip(ZipArchive $zip, ContestProblem $problem, ?st
849849
->getResult();
850850

851851
foreach ($testcases as $index => $testcase) {
852+
if (!$fullZip) {
853+
return true;
854+
}
852855
foreach (['input', 'output'] as $type) {
853856
$extension = Testcase::EXTENSION_MAPPING[$type];
854857

@@ -870,6 +873,7 @@ protected function addSamplesToZip(ZipArchive $zip, ContestProblem $problem, ?st
870873
$zip->addFromString($filename, $content);
871874
}
872875
}
876+
return false;
873877
}
874878

875879
public function getSamplesZipStreamedResponse(ContestProblem $contestProblem): StreamedResponse
@@ -879,7 +883,7 @@ public function getSamplesZipStreamedResponse(ContestProblem $contestProblem): S
879883
return Utils::streamAsBinaryFile($zipFileContent, $outputFilename, 'zip');
880884
}
881885

882-
public function getSamplesZipForContest(Contest $contest): StreamedResponse
886+
public function helperSamplesZipForContest(Contest $contest, bool $fullZip): StreamedResponse|bool
883887
{
884888
// Note, we reload the contest with the problems and attachments, to reduce the number of queries
885889
// We do not load the testcases here since addSamplesToZip loads them
@@ -896,30 +900,42 @@ public function getSamplesZipForContest(Contest $contest): StreamedResponse
896900
->getQuery()
897901
->getSingleResult();
898902

899-
$zip = new ZipArchive();
900-
if (!($tempFilename = tempnam($this->getDomjudgeTmpDir(), "export-"))) {
901-
throw new ServiceUnavailableHttpException(null, 'Could not create temporary file.');
902-
}
903+
$zip = null;
904+
if ($fullZip) {
905+
$zip = new ZipArchive();
906+
if (!($tempFilename = tempnam($this->getDomjudgeTmpDir(), "export-"))) {
907+
throw new ServiceUnavailableHttpException(null, 'Could not create temporary file.');
908+
}
903909

904-
$res = $zip->open($tempFilename, ZipArchive::OVERWRITE);
905-
if ($res !== true) {
906-
throw new ServiceUnavailableHttpException(null, 'Could not create temporary zip file.');
910+
$res = $zip->open($tempFilename, ZipArchive::OVERWRITE);
911+
if ($res !== true) {
912+
throw new ServiceUnavailableHttpException(null, 'Could not create temporary zip file.');
913+
}
907914
}
908915

909916
/** @var ContestProblem $problem */
910917
foreach ($contest->getProblems() as $problem) {
911918
// We don't include the samples for interactive problems.
912919
if (!$problem->getProblem()->isInteractiveProblem()) {
913-
$this->addSamplesToZip($zip, $problem, $problem->getShortname());
920+
$samplesFound = $this->addSamplesToZip($zip, $problem, $problem->getShortname(), fullZip: $fullZip);;
921+
if (!$fullZip && $samplesFound) {
922+
return true;
923+
}
914924
}
915925

916926
if ($problem->getProblem()->getProblemstatementType()) {
927+
if (!$fullZip) {
928+
return true;
929+
}
917930
$filename = sprintf('%s/statement.%s', $problem->getShortname(), $problem->getProblem()->getProblemstatementType());
918931
$zip->addFromString($filename, $problem->getProblem()->getProblemstatement());
919932
}
920933

921934
/** @var ProblemAttachment $attachment */
922935
foreach ($problem->getProblem()->getAttachments() as $attachment) {
936+
if (!$fullZip) {
937+
return true;
938+
}
923939
$filename = sprintf('%s/attachments/%s', $problem->getShortname(), $attachment->getName());
924940
$zip->addFromString($filename, $attachment->getContent()->getContent());
925941
if ($attachment->getContent()->isExecutable()) {
@@ -934,17 +950,34 @@ public function getSamplesZipForContest(Contest $contest): StreamedResponse
934950
}
935951

936952
if ($contest->getContestProblemsetType()) {
953+
if (!$fullZip) {
954+
return true;
955+
}
937956
$filename = sprintf('contest.%s', $contest->getContestProblemsetType());
938957
$zip->addFromString($filename, $contest->getContestProblemset());
939958
}
940959

960+
if (!$fullZip) {
961+
return false;
962+
}
963+
941964
$zip->close();
942965
$zipFileContents = file_get_contents($tempFilename);
943966
unlink($tempFilename);
944967

945968
return Utils::streamAsBinaryFile($zipFileContents, 'samples.zip', 'zip');
946969
}
947970

971+
public function checkIfSamplesZipForContest(Contest $contest): bool
972+
{
973+
return self::helperSamplesZipForContest($contest, fullZip: false);
974+
}
975+
976+
public function getSamplesZipForContest(Contest $contest): StreamedResponse
977+
{
978+
return self::helperSamplesZipForContest($contest, fullZip: true);
979+
}
980+
948981
/**
949982
* @throws NonUniqueResultException
950983
*/

0 commit comments

Comments
 (0)