Skip to content
Snippets Groups Projects

Fixes the direct upload endpoint to work wih trampoline again.

4 files
+ 136
74
Compare changes
  • Side-by-side
  • Inline

Files

@@ -235,18 +235,27 @@ public class SubmissionController extends ControllerBase {
/**
* Deletes data associated with an existing submission.
*
* @param sid the submission id
* @param submissionName the name of the submission to delete.
* @param aid the assignment id
*/
public void deleteSubmissionData(final String sid, final String aid) {
logger.debug("Deleting submission data for assignment id {}, submission id {}", aid, sid);
this.repositories.getSubmissionRepository().deleteById(sid);
public void deleteSubmissionData(final String submissionName, final String aid) {
logger.debug("Deleting submission data for assignment id {}, name {}", aid,
submissionName);
final var submissionRespository = this.repositories.getSubmissionRepository();
final var entityRepository = this.repositories.getEntityRepository();
final var entityOptional = entityRepository.findByParentIds(sid, aid);
logger.debug("Container exists in resultsOptional associated with submission data: {}",
entityOptional.isPresent());
entityOptional.ifPresent(ec -> entityRepository.deleteById(ec.getId()));
final var submissionList = submissionRespository.findByNameAndAssignment(submissionName,
aid);
logger.debug("Found {} submissions with matching name and assignment id",
submissionList.size());
submissionList.forEach(s -> {
logger.debug("Deleting submission with name {}, id {}, aid {}", s.getId(),
s.getName(), s.getAssignmentId());
submissionRespository.deleteById(s.getId());
final var entityOptional = entityRepository.findByParentIds(s.getId(), aid);
logger.debug("Entity exists associated with submission data: {}",
entityOptional.isPresent());
entityOptional.ifPresent(ec -> entityRepository.deleteById(ec.getId()));
});
}
@@ -270,47 +279,63 @@ public class SubmissionController extends ControllerBase {
* a file, where the Content-Disposition name="file" and the filename=[FILENAME].zip
*
* @param aid the assignment id to add the submission to
* @param sid the id of the submission which is to be created. Trampoline takes commit SHA-1
* hash by default.
* @param file the multipart zipped file
* @return a response
* @param directUploadId the id string of the new submission. This method will use this id to
* create a file where the uploaded file will be copied to
* (name=directUploadId.zip) and will also be used to name the
* submission (direct-upload:directUploadId)
* @return a response which includes the "id": id of the new submission
* @throws IOException if an error occurs while writing to a file
*/
@RequestMapping(path = "/api/v1/assignment/{aid}/submission/{sid}/direct-upload",
@RequestMapping(path = "/api/v1/assignment/{aid}/submission/direct-upload",
method = RequestMethod.POST)
public ResponseEntity<Response> directUploadSubmissionAction(
final @PathVariable String aid,
final @PathVariable String sid,
final @RequestParam(value = "file") MultipartFile file,
final @RequestParam String directUploadId,
final @Nullable @RequestParam(value = "identifier", required = false) String identifier
) throws IOException {
logger.debug("Received trampoline message, assignment id {}, submission id {}", aid, sid);
final var assignmentRepository = this.repositories.getAssignmentRepository();
final var submissionRepository = this.repositories.getSubmissionRepository();
var assignment = assignmentRepository.findExisting(aid);
) throws IOException, URISyntaxException {
final var res = new Response();
//final var commit = this.getString(req, "commit", res);
logger.debug("Received trampoline message, assignment id {}, directUploadId {}, "
+ "identifier {}", aid, directUploadId, identifier);
// Check if the original filename has an extension
final var originalFilename = file.getOriginalFilename();
if (originalFilename == null || this.getExtension(originalFilename).isEmpty()) {
throw new InvalidFileTypeException("File has no extension: " + originalFilename);
}
final var submissionRepository = this.repositories.getSubmissionRepository();
final var assignmentRepository = this.repositories.getAssignmentRepository();
this.deleteSubmissionData(sid, aid);
// Delete data associated with submission that has name direct-upload:COMMIT_HASH
final var newSubmissionName = "direct-upload:" + directUploadId;
var assignment = assignmentRepository.findExisting(aid);
this.deleteSubmissionData(newSubmissionName, aid);
// Move file in request to the temp folders directory.
final String extension = this.getExtension(originalFilename);
final String filename = sid + extension;
final String filename = directUploadId + extension;
final var temp = Files.createTempFile("auta-upload-", extension);
file.transferTo(temp.toFile());
this.files.add(filename, temp);
Submission submission = new Submission("devops:" + sid, "/" + filename, aid);
submission.setId(sid);
// Create a new submission with the new name, add to assignment and identityContainer and
// add to queue.
var submission = new Submission();
submission.setName(newSubmissionName);
submission.setAssignmentId(aid);
submission.setContents("/" + filename);
submission.getPipelineLog().setSubmitted(Instant.now());
submission = submissionRepository.save(submission);
final var sid = submission.getId();
res.put("id", sid);
assignment.addSubmission(submission);
assignment = assignmentRepository.save(assignment);
logger.debug("Created new submission for trampoline submission, submission id {}", sid);
logger.debug("Created new submission for direct-upload submission, submission id {}", sid);
if (identifier == null) {
logger.warn("Submission id {} does not belong to an identity", sid);
@@ -321,6 +346,6 @@ public class SubmissionController extends ControllerBase {
}
this.queue.add(new Job(assignment, submission));
return ResponseEntity.ok(new Response());
return ResponseEntity.created(this.generateSubmissionUri(aid, sid)).body(res);
}
}
Loading