Skip to content

#1 PDF Application on GitHub that lets you edit PDFs on any device anywhere

refactor(get-info): remove redundant PDF validation logic (#7213)

brios7cccee4c346c
diff --git a/app/core/src/main/java/stirling/software/SPDF/controller/api/security/GetInfoOnPDF.java b/app/core/src/main/java/stirling/software/SPDF/controller/api/security/GetInfoOnPDF.java
index d29480fd3c..d2775e910b 100644
--- a/app/core/src/main/java/stirling/software/SPDF/controller/api/security/GetInfoOnPDF.java
+++ b/app/core/src/main/java/stirling/software/SPDF/controller/api/security/GetInfoOnPDF.java
@@ -61,7 +61,6 @@ import stirling.software.common.model.api.PDFFile;
import stirling.software.common.model.tool.ToolFormat;
import stirling.software.common.model.tool.ToolIO;
import stirling.software.common.service.CustomPDFDocumentFactory;
-import stirling.software.common.util.ExceptionUtils;
import stirling.software.common.util.RegexPatternUtils;
import stirling.software.common.util.WebResponseUtils;
@@ -270,25 +269,6 @@ public class GetInfoOnPDF {
}
}
- private static void validatePdfFile(MultipartFile file) {
- if (file == null || file.isEmpty()) {
- throw new IllegalArgumentException("PDF file is required");
- }
-
- if (file.getSize() > MAX_FILE_SIZE) {
- throw ExceptionUtils.createIllegalArgumentException(
- "error.fileSizeLimit",
- "File size ({0} bytes) exceeds maximum allowed size ({1} bytes)",
- file.getSize(),
- MAX_FILE_SIZE);
- }
-
- String contentType = file.getContentType();
- if (contentType != null && !"application/pdf".equals(contentType)) {
- log.warn("File content type is {}, expected application/pdf", contentType);
- }
- }
-
private static ResponseEntity<byte[]> createErrorResponse(String errorMessage) {
try {
ObjectNode errorNode = objectMapper.createObjectNode();
@@ -1104,14 +1084,6 @@ public class GetInfoOnPDF {
public ResponseEntity<byte[]> getPdfInfo(@ModelAttribute PDFFile request) throws IOException {
MultipartFile inputFile = request.getFileInput();
- // Validate input
- try {
- validatePdfFile(inputFile);
- } catch (IllegalArgumentException e) {
- log.error("Invalid PDF file: {}", e.getMessage());
- return createErrorResponse("Invalid PDF file: " + e.getMessage());
- }
-
List<PDFVerificationResult> verificationResults = null;
try {
verificationResults = veraPDFService.validatePDF(inputFile.getInputStream());
diff --git a/app/core/src/test/java/stirling/software/SPDF/controller/api/security/GetInfoOnPDFMoreTest.java b/app/core/src/test/java/stirling/software/SPDF/controller/api/security/GetInfoOnPDFMoreTest.java
index 7e2aa388fa..1df66f739e 100644
--- a/app/core/src/test/java/stirling/software/SPDF/controller/api/security/GetInfoOnPDFMoreTest.java
+++ b/app/core/src/test/java/stirling/software/SPDF/controller/api/security/GetInfoOnPDFMoreTest.java
@@ -264,21 +264,6 @@ class GetInfoOnPDFMoreTest {
@DisplayName("error handling")
class Errors {
- @Test
- @DisplayName("empty file input yields an error response")
- void emptyFile() throws Exception {
- MockMultipartFile mf =
- new MockMultipartFile("fileInput", "x.pdf", "application/pdf", new byte[0]);
- PDFFile request = new PDFFile();
- request.setFileInput(mf);
- ResponseEntity<byte[]> resp = getInfoOnPDF.getPdfInfo(request);
- // createErrorResponse returns HTTP 200 with a JSON body carrying an "error" field.
- assertThat(resp.getBody()).isNotNull();
- JsonNode body = om.readTree(resp.getBody());
- assertThat(body.has("error")).isTrue();
- assertThat(body.get("error").asText("")).contains("Invalid");
- }
-
@Test
@DisplayName("veraPDF failure is swallowed and a report is still produced")
void veraPdfFailureSwallowed() throws Exception {
diff --git a/app/core/src/test/java/stirling/software/SPDF/controller/api/security/GetInfoOnPDFTest.java b/app/core/src/test/java/stirling/software/SPDF/controller/api/security/GetInfoOnPDFTest.java
index efc09cf190..8d17d729ea 100644
--- a/app/core/src/test/java/stirling/software/SPDF/controller/api/security/GetInfoOnPDFTest.java
+++ b/app/core/src/test/java/stirling/software/SPDF/controller/api/security/GetInfoOnPDFTest.java
@@ -556,24 +556,6 @@ class GetInfoOnPDFTest {
@DisplayName("Validation and Error Handling Tests")
class ValidationErrorTests {
- @Test
- @DisplayName("Should reject null file")
- void testValidation_NullFile() throws IOException {
- PDFFile request = new PDFFile();
- request.setFileInput(null);
-
- ResponseEntity<byte[]> response = getInfoOnPDF.getPdfInfo(request);
-
- Assertions.assertEquals(
- HttpStatus.OK, response.getStatusCode()); // Returns error JSON with 200
- String jsonResponse = new String(response.getBody(), StandardCharsets.UTF_8);
- JsonNode jsonNode = objectMapper.readTree(jsonResponse);
-
- Assertions.assertTrue(jsonNode.has("error"));
- Assertions.assertTrue(
- jsonNode.get("error").asText("").contains("PDF file is required"));
- }
-
@Test
@DisplayName("Should reject empty file")
void testValidation_EmptyFile() throws IOException {
@@ -591,64 +573,6 @@ class GetInfoOnPDFTest {
Assertions.assertTrue(jsonNode.has("error"));
}
-
- @Test
- @DisplayName("Should reject file that exceeds max size")
- void testValidation_TooLargeFile() throws IOException {
- MultipartFile largeFile =
- new MultipartFile() {
- @Override
- public String getName() {
- return "file";
- }
-
- @Override
- public String getOriginalFilename() {
- return "large.pdf";
- }
-
- @Override
- public String getContentType() {
- return MediaType.APPLICATION_PDF_VALUE;
- }
-
- @Override
- public boolean isEmpty() {
- return false;
- }
-
- @Override
- public long getSize() {
- // Report 101 MB without allocating memory
- return 101L * 1024L * 1024L;
- }
-
- @Override
- public byte[] getBytes() {
- return new byte[0];
- }
-
- @Override
- public java.io.InputStream getInputStream() {
- return java.io.InputStream.nullInputStream();
- }
-
- @Override
- public void transferTo(java.io.File dest) throws IllegalStateException {}
- };
-
- PDFFile request = new PDFFile();
- request.setFileInput(largeFile);
-
- ResponseEntity<byte[]> response = getInfoOnPDF.getPdfInfo(request);
-
- String jsonResponse = new String(response.getBody(), StandardCharsets.UTF_8);
- JsonNode jsonNode = objectMapper.readTree(jsonResponse);
-
- Assertions.assertTrue(jsonNode.has("error"));
- Assertions.assertTrue(
- jsonNode.get("error").asText("").contains("exceeds maximum allowed size"));
- }
}
@Nested