Skip to content

Commit

Permalink
feat: Add FileValidator class for multiform validation
Browse files Browse the repository at this point in the history
  • Loading branch information
gabriel-logan committed Jul 11, 2024
1 parent 5a586d7 commit 97227b7
Show file tree
Hide file tree
Showing 46 changed files with 234 additions and 59 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Compiled class file
*.class
/target

# Log file
*.log
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Empty file.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Empty file.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 11 additions & 1 deletion .idea/sonarlint/issuestore/index.pb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
12 changes: 11 additions & 1 deletion .idea/sonarlint/securityhotspotstore/index.pb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

85 changes: 33 additions & 52 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

98 changes: 98 additions & 0 deletions src/main/java/io/github/multiform_validator/FileValidator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package io.github.multiform_validator;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

public class FileValidator {
// Prevent instantiation
private FileValidator() {
throw new IllegalStateException("Utility class");
}

private static final Logger logger = Logger.getLogger(FileValidator.class.getName());

/**
* Validates an image file.
*
* @param file The image file to validate.
* @return true if the file is a valid image, false otherwise.
* @throws IllegalArgumentException if the input value is null.
*/
public static boolean validateImage(File file) {
return validateImage(file, null);
}

/**
* Validates an image file, excluding specific file types.
*
* @param file The image file to validate.
* @param exclude A list of file types to exclude from validation.
* @return true if the file is a valid image, false otherwise.
* @throws IllegalArgumentException if the input value is null.
*/
public static boolean validateImage(File file, List<String> exclude) {
if (file == null) {
throw new IllegalArgumentException("The input value cannot be null.");
}

try {
byte[] fileBytes = Files.readAllBytes(file.toPath());

if (exclude == null) {
return isGif(fileBytes) || isIco(fileBytes) || isJpeg(fileBytes) || isPng(fileBytes);
}

List<String> listToValidate = new ArrayList<>();
listToValidate.add("gif");
listToValidate.add("ico");
listToValidate.add("jpeg");
listToValidate.add("png");

List<String> filteredList = new ArrayList<>();
for (String item : listToValidate) {
if (!exclude.contains(item)) {
filteredList.add(item);
}
}

if (filteredList.isEmpty()) {
return false;
}

return validateFileTypes(fileBytes, filteredList);

} catch (IOException e) {
logger.severe("An error occurred while reading the file: " + e.getMessage());
return false;
}
}

private static boolean isPng(byte[] fileBytes) {
return fileBytes[0] == (byte) 0x89 && fileBytes[1] == 0x50 && fileBytes[2] == 0x4E && fileBytes[3] == 0x47;
}

private static boolean isJpeg(byte[] fileBytes) {
return fileBytes[0] == (byte) 0xFF && fileBytes[1] == (byte) 0xD8 && fileBytes[2] == (byte) 0xFF;
}

private static boolean isGif(byte[] fileBytes) {
return fileBytes[0] == 0x47 && fileBytes[1] == 0x49 && fileBytes[2] == 0x46 && fileBytes[3] == 0x38;
}

private static boolean isIco(byte[] fileBytes) {
return fileBytes[0] == 0x00 && fileBytes[1] == 0x00 && fileBytes[2] == 0x01;
}

private static boolean validateFileTypes(byte[] fileBytes, List<String> filteredList) {
boolean isGifValid = filteredList.contains("gif") && isGif(fileBytes);
boolean isIcoValid = filteredList.contains("ico") && isIco(fileBytes);
boolean isJpegValid = filteredList.contains("jpeg") && isJpeg(fileBytes);
boolean isPngValid = filteredList.contains("png") && isPng(fileBytes);

return isGifValid || isIcoValid || isJpegValid || isPngValid;
}
}
50 changes: 50 additions & 0 deletions src/test/java/FileValidatorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import io.github.multiform_validator.FileValidator;
import org.junit.jupiter.api.Test;

import java.io.File;
import java.util.Arrays;
import java.util.List;

import static org.junit.jupiter.api.Assertions.*;

class FileValidatorTest {
final String basePath = "src/test/java/assets/isValidImage/";

@Test
void testValidateImage() {
File file = new File(basePath + "/valid/valid.jpg");
assertTrue(FileValidator.validateImage(file));
}

@Test
void testValidateImageWithExclusion() {
File file = new File(basePath + "/valid/valid.jpg");
List<String> exclude = Arrays.asList("gif", "ico", "png");
assertTrue(FileValidator.validateImage(file, exclude));
}

@Test
void testValidateImageWithInvalidImage() {
File file = new File(basePath + "/invalid/invalid.png");
assertFalse(FileValidator.validateImage(file));
}

@Test
void testValidateImageWithValidImageAndValidExclusion() {
File file = new File(basePath + "/valid/valid.png");
List<String> exclude = Arrays.asList("gif", "ico", "png");
assertFalse(FileValidator.validateImage(file, exclude));
}

@Test
void testValidateImageWithValidImageAndInvalidExclusion() {
File file = new File(basePath + "/valid/valid.png");
List<String> exclude = Arrays.asList("gif", "ico");
assertTrue(FileValidator.validateImage(file, exclude));
}

@Test
void testValidateImageWithNullFile() {
assertThrows(IllegalArgumentException.class, () -> FileValidator.validateImage(null));
}
}
1 change: 1 addition & 0 deletions src/test/java/assets/isValidAudio/invalid/invalid.mp3
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
asdasdasd
1 change: 1 addition & 0 deletions src/test/java/assets/isValidAudio/invalid/invalid.wav
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
asdasdasd
Binary file added src/test/java/assets/isValidAudio/valid/valid.mp3
Binary file not shown.
Binary file not shown.
1 change: 1 addition & 0 deletions src/test/java/assets/isValidImage/invalid/invalid.avif
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
asdasdasdsdasdaSADASDASDASDASDASDASDASD
1 change: 1 addition & 0 deletions src/test/java/assets/isValidImage/invalid/invalid.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/test/java/assets/isValidImage/invalid/invalid.ico
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
asdasdasdsdasdaSADASDASDASDASDASDASDASD
1 change: 1 addition & 0 deletions src/test/java/assets/isValidImage/invalid/invalid.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/test/java/assets/isValidImage/invalid/invalid.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/test/java/assets/isValidImage/invalid/invalid.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/test/java/assets/isValidImage/invalid/invalid.webp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
asdasdasdsdasdaSADASDASDASDASDASDASDASD
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/test/java/assets/isValidImage/valid/valid.ico
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/test/java/assets/isValidImage/valid/valid.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/test/java/assets/isValidPdf/invalid.pdf
Binary file not shown.
Binary file added src/test/java/assets/isValidPdf/valid.pdf
Binary file not shown.
Binary file added src/test/java/assets/isValidTxt/invalid.txt
Binary file not shown.
Loading

0 comments on commit 97227b7

Please sign in to comment.