From fd00a868b8397975fca928504d932bb5be08cc78 Mon Sep 17 00:00:00 2001 From: Aqua-sc <108478185+Aqua-sc@users.noreply.github.com> Date: Thu, 9 May 2024 19:02:15 +0200 Subject: [PATCH] Added route to download artifacts --- .../controllers/SubmissionController.java | 44 ++++++++++++++++--- .../com/ugent/pidgeon/util/Filehandler.java | 16 +++---- 2 files changed, 46 insertions(+), 14 deletions(-) diff --git a/backend/app/src/main/java/com/ugent/pidgeon/controllers/SubmissionController.java b/backend/app/src/main/java/com/ugent/pidgeon/controllers/SubmissionController.java index fb717792..1028825c 100644 --- a/backend/app/src/main/java/com/ugent/pidgeon/controllers/SubmissionController.java +++ b/backend/app/src/main/java/com/ugent/pidgeon/controllers/SubmissionController.java @@ -105,7 +105,9 @@ private DockerOutput runDockerTest(ZipFile file, TestEntity testEntity, Path out List artifacts = model.getArtifacts(); // Copy all files as zip into the output directory - Filehandler.copyFilesAsZip(artifacts, outputPath); + if (artifacts != null && !artifacts.isEmpty()) { + Filehandler.copyFilesAsZip(artifacts, outputPath); + } // Cleanup garbage files and container model.cleanUp(); @@ -281,8 +283,7 @@ public ResponseEntity submitFile(@RequestParam("file") MultipartFile file, @P try { // Check if docker tests succeed DockerOutput dockerOutput = runDockerTest(new ZipFile(finalSavedFile), testEntity, - Path.of(Filehandler.getSubmissionPath(projectid, groupId, submission.getId()) - + "/artifacts.zip")); + Filehandler.getSubmissionAritfactPath(projectid, groupId, submission.getId())); if (dockerOutput == null) { throw new RuntimeException("Error while running docker tests."); } @@ -343,7 +344,10 @@ public ResponseEntity getSubmissionFile(@PathVariable("submissionid") long su // Get the file from the server try { - Resource zipFile = Filehandler.getSubmissionAsResource(Path.of(file.getPath())); + Resource zipFile = Filehandler.getFileAsResource(Path.of(file.getPath())); + if (zipFile == null) { + return ResponseEntity.status(HttpStatus.NOT_FOUND).body("File not found."); + } // Set headers for the response HttpHeaders headers = new HttpHeaders(); @@ -358,7 +362,37 @@ public ResponseEntity getSubmissionFile(@PathVariable("submissionid") long su } } - + @GetMapping(ApiRoutes.SUBMISSION_BASE_PATH + "/{submissionid}/artifacts") //Route to get a submission + @Roles({UserRole.teacher, UserRole.student}) + public ResponseEntity getSubmissionArtifacts(@PathVariable("submissionid") long submissionid, Auth auth) { + CheckResult checkResult = submissionUtil.canGetSubmission(submissionid, auth.getUserEntity()); + if (!checkResult.getStatus().equals(HttpStatus.OK)) { + return ResponseEntity.status(checkResult.getStatus()).body(checkResult.getMessage()); + } + SubmissionEntity submission = checkResult.getData(); + + // Get the file from the server + try { + Resource zipFile = Filehandler.getFileAsResource(Filehandler.getSubmissionAritfactPath(submission.getProjectId(), submission.getGroupId(), submission.getId())); + if (zipFile == null) { + return ResponseEntity.status(HttpStatus.NOT_FOUND).body("No artifacts found for this submission."); + } + // Set headers for the response + HttpHeaders headers = new HttpHeaders(); + headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + zipFile.getFilename()); + headers.add(HttpHeaders.CONTENT_TYPE, "application/zip"); + + return ResponseEntity.ok() + .headers(headers) + .body(zipFile); + } catch (Exception e) { + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage()); + } + } + + + + /** * Function to delete a submission * diff --git a/backend/app/src/main/java/com/ugent/pidgeon/util/Filehandler.java b/backend/app/src/main/java/com/ugent/pidgeon/util/Filehandler.java index cf27b536..3475b43d 100644 --- a/backend/app/src/main/java/com/ugent/pidgeon/util/Filehandler.java +++ b/backend/app/src/main/java/com/ugent/pidgeon/util/Filehandler.java @@ -126,6 +126,10 @@ static public Path getSubmissionPath(long projectid, long groupid, long submissi return Path.of(BASEPATH,"projects", String.valueOf(projectid), String.valueOf(groupid), String.valueOf(submissionid)); } + static public Path getSubmissionAritfactPath(long projectid, long groupid, long submissionid) { + return getSubmissionPath(projectid, groupid, submissionid).resolve("artifacts.zip"); + } + /** * Get the path were a test is stored * @param projectid id of the project @@ -141,6 +145,9 @@ static public Path getTestPath(long projectid) { * @return the file as a resource */ public static Resource getFileAsResource(Path path) { + if (!Files.exists(path)) { + return null; + } File file = path.toFile(); return new FileSystemResource(file); } @@ -164,15 +171,6 @@ public static boolean isZipFile(File file) throws IOException { } - /** - * Get a submission as a resource - * @param path path of the submission - * @return the submission as a resource - * @throws IOException if an error occurs while getting the submission - */ - public static Resource getSubmissionAsResource(Path path) throws IOException { - return new InputStreamResource(new FileInputStream(path.toFile())); - } /** * Save a file to the server