Skip to content
Snippets Groups Projects

Resolve "Files as assignment descriptions"

Files

@@ -17,6 +17,7 @@
*/
package nl.tudelft.submit.controller;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
@@ -30,12 +31,18 @@ import javax.transaction.Transactional;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import com.opencsv.exceptions.CsvValidationException;
@@ -70,6 +77,12 @@ import nl.tudelft.submit.service.*;
@Controller
public class AssignmentController {
@Value("${submit.filesys.storage-dir}")
private String storageDir;
@Value("${submit.filesys.temp-storage-dir}")
private String tempStorageDir;
@Autowired
private StudentGroupCacheManager groupCache;
@@ -103,6 +116,9 @@ public class AssignmentController {
@Autowired
private PersonCacheManager personCache;
@Autowired
private FileService fileService;
/**
* Gets all details of an assignment.
*
@@ -234,6 +250,26 @@ public class AssignmentController {
return "assignment/statistics";
}
/**
* Downloads the assignment file from the local file system.
*
* @param assignmentId the identifier of the assignment associated with the file
* @return the ResponseEntity called
*/
@GetMapping("/{assignmentId}/download-assignment-files")
@PreAuthorize("@authorizationService.canViewAssignment(#assignmentId)")
public ResponseEntity<Resource> downloadAssignmentFiles(@PathVariable Long assignmentId)
throws Exception {
String assignmentsDirectory = storageDir + "/assignments";
String downloadsDirectory = tempStorageDir + "/downloads";
Resource resource = fileService.downloadFiles(assignmentId, assignmentsDirectory, downloadsDirectory);
return ResponseEntity.ok()
.contentType(MediaType.parseMediaType(MediaType.APPLICATION_OCTET_STREAM_VALUE))
.header(HttpHeaders.CONTENT_DISPOSITION,
"attachment; filename=\"" + resource.getFilename() + "\"")
.body(resource);
}
/**
* Adds an assignment by posting an add request to the Labracore API.
*
@@ -241,11 +277,13 @@ public class AssignmentController {
* @return The link to the site where this request redirects to.
*/
@Validated
@PostMapping
@PostMapping(consumes = { "multipart/form-data" })
@PreAuthorize("@authorizationService.canCreateAssignment(#dto.module.id)")
public String addAssignment(@Valid @ModelAttribute("assignment") SubmitAssignmentCreateDTO dto) {
public String addAssignment(@Valid @ModelAttribute("assignment") SubmitAssignmentCreateDTO dto,
MultipartHttpServletRequest request) throws IOException {
Long id = assignmentService.addAssignment(dto);
String assignmentsDirectory = storageDir + "/assignments";
fileService.uploadFiles(request.getFileMap(), id, assignmentsDirectory);
return "redirect:/assignment/" + id;
}
@@ -257,15 +295,33 @@ public class AssignmentController {
* @return The link to the site where this request redirects to.
*/
@Validated
@PatchMapping("/{id}")
@PostMapping(value = "/{id}", consumes = { "multipart/form-data" })
@PreAuthorize("@authorizationService.canEditAssignment(#id)")
public String patchAssignment(@PathVariable Long id,
@Valid @ModelAttribute("patch") SubmitAssignmentPatchDTO patch) {
@Valid @ModelAttribute("patch") SubmitAssignmentPatchDTO patch,
MultipartHttpServletRequest request) throws IOException {
Long patchedId = assignmentService.patchAssignment(id, patch);
String assignmentsDirectory = storageDir + "/assignments";
fileService.overwriteDirectory(request.getFileMap(), patchedId, assignmentsDirectory);
return "redirect:/assignment/" + patchedId;
}
/**
* Deletes an assignment directory (which includes all assignment files).
*
* @param id id of the assignment.
* @return The link to the site where this request redirects to.
*/
@Validated
@PostMapping(value = "/{id}/delete-assignment-directory")
@PreAuthorize("@authorizationService.canEditAssignment(#id)")
public String deleteAssignmentDirectory(@PathVariable Long id) {
String assignmentsDirectory = storageDir + "/assignments";
File assignmentDir = fileService.getFilePathWithId(assignmentsDirectory, id);
fileService.deleteDirectory(assignmentDir);
return "redirect:/assignment/" + id;
}
/**
* Adds or updates the instructor note on an instance of this entity.
*
Loading