Skip to content

Commit

Permalink
Added route to download artifacts
Browse files Browse the repository at this point in the history
  • Loading branch information
Aqua-sc committed May 9, 2024
1 parent d669531 commit fd00a86
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,9 @@ private DockerOutput runDockerTest(ZipFile file, TestEntity testEntity, Path out
List<File> 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();
Expand Down Expand Up @@ -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.");
}
Expand Down Expand Up @@ -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();
Expand All @@ -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<SubmissionEntity> 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
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
Expand All @@ -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
Expand Down

0 comments on commit fd00a86

Please sign in to comment.