diff --git a/backend/app/build.gradle b/backend/app/build.gradle index a7302389..2710f535 100644 --- a/backend/app/build.gradle +++ b/backend/app/build.gradle @@ -33,8 +33,10 @@ dependencies { implementation 'com.auth0:jwks-rsa:0.18.0' implementation 'javax.servlet:javax.servlet-api:4.0.1' implementation 'org.springframework.boot:spring-boot-starter-data-jpa' + implementation 'org.apache.tika:tika-core:1.27' runtimeOnly 'org.postgresql:postgresql' + implementation "org.springframework.boot:spring-boot-devtools" testImplementation 'org.springframework.boot:spring-boot-starter-test' testImplementation 'org.springframework.security:spring-security-test' 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 8017b691..a2f52045 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 @@ -1,5 +1,6 @@ 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; @@ -48,7 +49,7 @@ public static String saveSubmission(Path directory, MultipartFile file) throws I return filePath.getFileName().toString(); } catch (IOException e) { - throw new IOException("Error while saving file" + e.getMessage()); + throw new IOException(e.getMessage()); } } @@ -56,21 +57,17 @@ 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 boolean isZipFile(File file) throws IOException { - try (InputStream inputStream = new FileInputStream(file); - BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream)) { + public static boolean isZipFile(File file) throws IOException { + // Create a Tika instance + Tika tika = new Tika(); - byte[] signature = new byte[4]; - bufferedInputStream.mark(4); - int bytesRead = bufferedInputStream.read(signature); + // Detect the file type + String fileType = tika.detect(file); - if (bytesRead != 4) { - throw new IOException("Error while reading 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"); - // Check if the file signature matches the ZIP format - return (signature[0] == 0x50 && signature[1] == 0x4b && signature[2] == 0x03 && signature[3] == 0x04); - } } public static Resource getSubmissionAsResource(Path path) throws IOException {