Skip to content

Commit

Permalink
download file when submission is single source
Browse files Browse the repository at this point in the history
  • Loading branch information
as6325400 committed Sep 22, 2024
1 parent 3324986 commit 7b977fc
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
4 changes: 4 additions & 0 deletions webapp/src/Controller/Team/SubmissionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,10 @@ public function downloadAction(int $submitId): Response
$submitId));
}

if($this->submissionService->getSubmissionFileNums($submission) == 1){
return $this->submissionService->getSubmissionFileResponse($submission);
}

return $this->submissionService->getSubmissionZipResponse($submission);
}
}
29 changes: 29 additions & 0 deletions webapp/src/Service/SubmissionService.php
Original file line number Diff line number Diff line change
Expand Up @@ -768,4 +768,33 @@ public function getSubmissionZipResponse(Submission $submission): StreamedRespon

return Utils::streamZipFile($tmpfname, 's' . $submission->getSubmitid() . '.zip');
}

public function getSubmissionFileResponse(Submission $submission): StreamedResponse
{
/** @var SubmissionFile[] $files */
$files = $submission->getFiles();

if (count($files) !== 1) {
throw new ServiceUnavailableHttpException(null, 'Submission does not contain exactly one file.');
}

$file = $files[0];
$filename = $file->getFilename();
$sourceCode = $file->getSourcecode();

return new StreamedResponse(function () use ($sourceCode) {
echo $sourceCode;
}, 200, [
'Content-Type' => 'application/octet-stream',
'Content-Disposition' => 'attachment; filename="' . $filename . '"',
'Content-Length' => strlen($sourceCode),
]);
}


public function getSubmissionFileNums(Submission $submission): int
{
$files = $submission->getFiles();
return count($files);
}
}

0 comments on commit 7b977fc

Please sign in to comment.