-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #39 from SELab-2/feature/savefiles
Feature/savefiles
- Loading branch information
Showing
10 changed files
with
224 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,3 +35,4 @@ out/ | |
|
||
### VS Code ### | ||
.vscode/ | ||
backend/app/data/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
backend/app/src/main/java/com/ugent/pidgeon/controllers/FilesubmissiontestController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package com.ugent.pidgeon.controllers; | ||
|
||
import com.ugent.pidgeon.postgre.models.FileEntity; | ||
import com.ugent.pidgeon.postgre.models.SubmissionEntity; | ||
import com.ugent.pidgeon.postgre.repository.FileRepository; | ||
import com.ugent.pidgeon.postgre.repository.GroupRepository; | ||
import com.ugent.pidgeon.postgre.repository.ProjectRepository; | ||
import com.ugent.pidgeon.postgre.repository.SubmissionRepository; | ||
import com.ugent.pidgeon.util.Filehandler; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.core.io.InputStreamResource; | ||
import org.springframework.core.io.Resource; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import java.io.FileInputStream; | ||
import java.nio.file.Path; | ||
import java.sql.Timestamp; | ||
import java.util.logging.Logger; | ||
import java.util.zip.ZipFile; | ||
|
||
@RestController | ||
public class FilesubmissiontestController { | ||
|
||
@Autowired | ||
private GroupRepository groupRepository; | ||
@Autowired | ||
private FileRepository fileRepository; | ||
@Autowired | ||
private SubmissionRepository submissionRepository; | ||
@Autowired | ||
private ProjectRepository projectRepository; | ||
|
||
@PostMapping("/project/{projectid}/submit") //Route to submit a file, it accepts a multiform with the file and submissionTime | ||
public ResponseEntity<String> submitFile(@RequestParam("file") MultipartFile file, @RequestParam("submissionTime") Timestamp time, @PathVariable("projectid") long projectid) { | ||
long userId = 1L; //TODO: replace with id of current user | ||
Long groupId = groupRepository.groupIdByProjectAndUser(projectid, userId); | ||
|
||
if (!projectRepository.userPartOfProject(projectid, userId)) { | ||
return ResponseEntity.status(HttpStatus.FORBIDDEN).body("You aren't part of this project"); | ||
} | ||
//TODO: executes the tests onces these are implemented | ||
try { | ||
//Save the file entry in the database to get the id | ||
FileEntity fileEntity = new FileEntity("", "", userId); | ||
long fileid = fileRepository.save(fileEntity).getId(); | ||
|
||
//Save the submission in the database TODO: update the accepted parameter | ||
SubmissionEntity submissionEntity = new SubmissionEntity(projectid, groupId, fileid, time, false); | ||
SubmissionEntity submission = submissionRepository.save(submissionEntity); | ||
|
||
//Save the file on the server | ||
Path path = Filehandler.getSubmissionPath(projectid, groupId, submission.getId()); | ||
String filename = Filehandler.saveSubmission(path, file); | ||
|
||
//Update name and path for the file entry | ||
fileEntity.setName(filename); | ||
fileEntity.setPath(path.toString()); | ||
fileRepository.save(fileEntity); | ||
|
||
return ResponseEntity.ok("File saved"); | ||
} catch (Exception e) { | ||
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Error while saving file: " + e.getMessage()); | ||
} | ||
|
||
} | ||
|
||
@GetMapping("submissions/{submissionid}") | ||
public ResponseEntity<Resource> getSubmission(@PathVariable("submissionid") long submissionid) { | ||
long userId = 1L; //TODO: replace with id of current user | ||
// Get the submission entry from the database | ||
SubmissionEntity submission = submissionRepository.findById(submissionid).orElse(null); | ||
if (submission == null) { | ||
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null); | ||
} | ||
if (!groupRepository.userInGroup(submission.getGroupId(), userId)) { | ||
return ResponseEntity.status(HttpStatus.FORBIDDEN).body(null); | ||
} | ||
// Get the file entry from the database | ||
FileEntity file = fileRepository.findById(submission.getFileId()).orElse(null); | ||
if (file == null) { | ||
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null); | ||
} | ||
|
||
|
||
// Get the file from the server | ||
try { | ||
Resource zipFile = Filehandler.getSubmissionAsResource(Path.of(file.getPath(), file.getName())); | ||
|
||
// Set headers for the response | ||
HttpHeaders headers = new HttpHeaders(); | ||
headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + file.getName()); | ||
headers.add(HttpHeaders.CONTENT_TYPE, "application/zip"); | ||
|
||
return ResponseEntity.ok() | ||
.headers(headers) | ||
.contentType(MediaType.APPLICATION_OCTET_STREAM) | ||
.body(zipFile); | ||
} catch (Exception e) { | ||
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 3 additions & 4 deletions
7
backend/app/src/main/java/com/ugent/pidgeon/postgre/models/SubmissionEntity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
backend/app/src/main/java/com/ugent/pidgeon/util/Filehandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package com.ugent.pidgeon.util; | ||
|
||
import org.apache.tika.Tika; | ||
import org.springframework.core.io.InputStreamResource; | ||
import org.springframework.web.multipart.MultipartFile; | ||
import org.springframework.core.io.Resource; | ||
|
||
import java.io.*; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.StandardCopyOption; | ||
import java.util.logging.Logger; | ||
import java.util.zip.ZipFile; | ||
|
||
public class Filehandler { | ||
|
||
static String BASEPATH = "data"; | ||
static String SUBMISSION_FILENAME = "files.zip"; | ||
|
||
public static String saveSubmission(Path directory, MultipartFile file) throws IOException { | ||
// Check if the file is empty | ||
if (file.isEmpty()) { | ||
throw new IOException("File is empty"); | ||
} | ||
|
||
try { | ||
// Create a temporary file and save the uploaded file to it | ||
File tempFile = File.createTempFile("uploaded-zip-", ".zip"); | ||
file.transferTo(tempFile); | ||
|
||
// Check if the file is a ZIP file | ||
if (!isZipFile(tempFile)) { | ||
throw new IOException("File is not a ZIP file"); | ||
} | ||
// Create directory | ||
File uploadDirectory = new File(directory.toString()); | ||
if (!uploadDirectory.exists()) { | ||
if(!uploadDirectory.mkdirs()) { | ||
throw new IOException("Error while creating directory"); | ||
} | ||
} | ||
|
||
// Save the file to the server | ||
Path filePath = directory.resolve(SUBMISSION_FILENAME); | ||
|
||
try(InputStream stream = new FileInputStream(tempFile)) { | ||
Files.copy(stream, filePath, StandardCopyOption.REPLACE_EXISTING); | ||
} | ||
|
||
return filePath.getFileName().toString(); | ||
} catch (IOException e) { | ||
throw new IOException(e.getMessage()); | ||
} | ||
} | ||
|
||
static public Path getSubmissionPath(long projectid, long groupid, long submissionid) { | ||
return Path.of(BASEPATH,"projects", String.valueOf(projectid), String.valueOf(groupid), String.valueOf(submissionid)); | ||
} | ||
|
||
public static boolean isZipFile(File file) throws IOException { | ||
// Create a Tika instance | ||
Tika tika = new Tika(); | ||
|
||
// Detect the file type | ||
String fileType = tika.detect(file); | ||
|
||
// Check if the detected file type is a ZIP file | ||
Logger.getGlobal().info("File type: " + fileType); | ||
return fileType.equals("application/zip") || fileType.equals("application/x-zip-compressed"); | ||
|
||
} | ||
|
||
public static Resource getSubmissionAsResource(Path path) throws IOException { | ||
return new InputStreamResource(new FileInputStream(path.toFile())); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters