Skip to content

Commit 97227b7

Browse files
committed
feat: Add FileValidator class for multiform validation
1 parent 5a586d7 commit 97227b7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+234
-59
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Compiled class file
22
*.class
3+
/target
34

45
# Log file
56
*.log

.idea/sonarlint/issuestore/1/0/1035b95fde851fd8381dabb4da89dd79908aed9b

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/sonarlint/issuestore/3/c/3c8842d6f65bc0f21cf2bfc030702ffe7e82d70d

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/sonarlint/issuestore/6/e/6e481a8cc098ad8ab7f553fbd33042be7454e52a

Whitespace-only changes.

.idea/sonarlint/issuestore/a/a/aa5999cb67ff93d7fa1f135c6ce35751a7f50782

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/sonarlint/issuestore/d/9/d90a638069474de771e722f81c6e4f61b75eb1c4

Whitespace-only changes.

.idea/sonarlint/issuestore/d/f/df99130c3df382b199c34a94824d11e25467ed68

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/sonarlint/issuestore/index.pb

Lines changed: 11 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/sonarlint/securityhotspotstore/3/c/3c8842d6f65bc0f21cf2bfc030702ffe7e82d70d

Whitespace-only changes.

.idea/sonarlint/securityhotspotstore/6/e/6e481a8cc098ad8ab7f553fbd33042be7454e52a

Whitespace-only changes.

.idea/sonarlint/securityhotspotstore/a/a/aa5999cb67ff93d7fa1f135c6ce35751a7f50782

Whitespace-only changes.

.idea/sonarlint/securityhotspotstore/d/9/d90a638069474de771e722f81c6e4f61b75eb1c4

Whitespace-only changes.

.idea/sonarlint/securityhotspotstore/d/f/df99130c3df382b199c34a94824d11e25467ed68

Whitespace-only changes.

.idea/sonarlint/securityhotspotstore/index.pb

Lines changed: 11 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/workspace.xml

Lines changed: 33 additions & 52 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package io.github.multiform_validator;
2+
3+
import java.io.File;
4+
import java.io.IOException;
5+
import java.nio.file.Files;
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
import java.util.logging.Logger;
9+
10+
public class FileValidator {
11+
// Prevent instantiation
12+
private FileValidator() {
13+
throw new IllegalStateException("Utility class");
14+
}
15+
16+
private static final Logger logger = Logger.getLogger(FileValidator.class.getName());
17+
18+
/**
19+
* Validates an image file.
20+
*
21+
* @param file The image file to validate.
22+
* @return true if the file is a valid image, false otherwise.
23+
* @throws IllegalArgumentException if the input value is null.
24+
*/
25+
public static boolean validateImage(File file) {
26+
return validateImage(file, null);
27+
}
28+
29+
/**
30+
* Validates an image file, excluding specific file types.
31+
*
32+
* @param file The image file to validate.
33+
* @param exclude A list of file types to exclude from validation.
34+
* @return true if the file is a valid image, false otherwise.
35+
* @throws IllegalArgumentException if the input value is null.
36+
*/
37+
public static boolean validateImage(File file, List<String> exclude) {
38+
if (file == null) {
39+
throw new IllegalArgumentException("The input value cannot be null.");
40+
}
41+
42+
try {
43+
byte[] fileBytes = Files.readAllBytes(file.toPath());
44+
45+
if (exclude == null) {
46+
return isGif(fileBytes) || isIco(fileBytes) || isJpeg(fileBytes) || isPng(fileBytes);
47+
}
48+
49+
List<String> listToValidate = new ArrayList<>();
50+
listToValidate.add("gif");
51+
listToValidate.add("ico");
52+
listToValidate.add("jpeg");
53+
listToValidate.add("png");
54+
55+
List<String> filteredList = new ArrayList<>();
56+
for (String item : listToValidate) {
57+
if (!exclude.contains(item)) {
58+
filteredList.add(item);
59+
}
60+
}
61+
62+
if (filteredList.isEmpty()) {
63+
return false;
64+
}
65+
66+
return validateFileTypes(fileBytes, filteredList);
67+
68+
} catch (IOException e) {
69+
logger.severe("An error occurred while reading the file: " + e.getMessage());
70+
return false;
71+
}
72+
}
73+
74+
private static boolean isPng(byte[] fileBytes) {
75+
return fileBytes[0] == (byte) 0x89 && fileBytes[1] == 0x50 && fileBytes[2] == 0x4E && fileBytes[3] == 0x47;
76+
}
77+
78+
private static boolean isJpeg(byte[] fileBytes) {
79+
return fileBytes[0] == (byte) 0xFF && fileBytes[1] == (byte) 0xD8 && fileBytes[2] == (byte) 0xFF;
80+
}
81+
82+
private static boolean isGif(byte[] fileBytes) {
83+
return fileBytes[0] == 0x47 && fileBytes[1] == 0x49 && fileBytes[2] == 0x46 && fileBytes[3] == 0x38;
84+
}
85+
86+
private static boolean isIco(byte[] fileBytes) {
87+
return fileBytes[0] == 0x00 && fileBytes[1] == 0x00 && fileBytes[2] == 0x01;
88+
}
89+
90+
private static boolean validateFileTypes(byte[] fileBytes, List<String> filteredList) {
91+
boolean isGifValid = filteredList.contains("gif") && isGif(fileBytes);
92+
boolean isIcoValid = filteredList.contains("ico") && isIco(fileBytes);
93+
boolean isJpegValid = filteredList.contains("jpeg") && isJpeg(fileBytes);
94+
boolean isPngValid = filteredList.contains("png") && isPng(fileBytes);
95+
96+
return isGifValid || isIcoValid || isJpegValid || isPngValid;
97+
}
98+
}

src/test/java/FileValidatorTest.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import io.github.multiform_validator.FileValidator;
2+
import org.junit.jupiter.api.Test;
3+
4+
import java.io.File;
5+
import java.util.Arrays;
6+
import java.util.List;
7+
8+
import static org.junit.jupiter.api.Assertions.*;
9+
10+
class FileValidatorTest {
11+
final String basePath = "src/test/java/assets/isValidImage/";
12+
13+
@Test
14+
void testValidateImage() {
15+
File file = new File(basePath + "/valid/valid.jpg");
16+
assertTrue(FileValidator.validateImage(file));
17+
}
18+
19+
@Test
20+
void testValidateImageWithExclusion() {
21+
File file = new File(basePath + "/valid/valid.jpg");
22+
List<String> exclude = Arrays.asList("gif", "ico", "png");
23+
assertTrue(FileValidator.validateImage(file, exclude));
24+
}
25+
26+
@Test
27+
void testValidateImageWithInvalidImage() {
28+
File file = new File(basePath + "/invalid/invalid.png");
29+
assertFalse(FileValidator.validateImage(file));
30+
}
31+
32+
@Test
33+
void testValidateImageWithValidImageAndValidExclusion() {
34+
File file = new File(basePath + "/valid/valid.png");
35+
List<String> exclude = Arrays.asList("gif", "ico", "png");
36+
assertFalse(FileValidator.validateImage(file, exclude));
37+
}
38+
39+
@Test
40+
void testValidateImageWithValidImageAndInvalidExclusion() {
41+
File file = new File(basePath + "/valid/valid.png");
42+
List<String> exclude = Arrays.asList("gif", "ico");
43+
assertTrue(FileValidator.validateImage(file, exclude));
44+
}
45+
46+
@Test
47+
void testValidateImageWithNullFile() {
48+
assertThrows(IllegalArgumentException.class, () -> FileValidator.validateImage(null));
49+
}
50+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
asdasdasd
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
asdasdasd
Binary file not shown.
Binary file not shown.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
asdasdasdsdasdaSADASDASDASDASDASDASDASD
Lines changed: 1 addition & 0 deletions
Loading
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
asdasdasdsdasdaSADASDASDASDASDASDASDASD
Lines changed: 1 addition & 0 deletions
Loading
Lines changed: 1 addition & 0 deletions
Loading
Lines changed: 1 addition & 0 deletions
Loading
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
asdasdasdsdasdaSADASDASDASDASDASDASDASD
Binary file not shown.
Loading
Binary file not shown.
Loading
Loading

src/test/java/assets/isValidImage/valid/valid.svg

Lines changed: 1 addition & 0 deletions
Loading
Binary file not shown.
Loading
619 Bytes
Binary file not shown.
53.5 KB
Binary file not shown.
53.5 KB
Binary file not shown.

0 commit comments

Comments
 (0)