Skip to content

Commit

Permalink
Fix image validation #262 (#263)
Browse files Browse the repository at this point in the history
  • Loading branch information
fugerit79 authored Nov 19, 2024
1 parent 2653cfb commit b2d4dc1
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.fugerit.java.doc.val.core.basic;

import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;
import java.util.Iterator;
Expand All @@ -11,7 +10,6 @@
import javax.imageio.stream.ImageInputStream;

import org.fugerit.java.core.lang.helpers.JavaVersionHelper;
import org.fugerit.java.core.util.result.Result;
import org.fugerit.java.doc.val.core.DocTypeValidationResult;
import org.fugerit.java.doc.val.core.DocTypeValidator;

Expand Down Expand Up @@ -54,23 +52,19 @@ public static boolean javaVersionSupportHelper( int javaMajorVersionFound, int j
private int javaMajorVersionRequired;

@Override
public DocTypeValidationResult validate(InputStream is) throws IOException {
DocTypeValidationResult result = DocTypeValidationResult.newFail();
public DocTypeValidationResult validate(InputStream is) {
try ( ImageInputStream iis = ImageIO.createImageInputStream( is ) ) {
Iterator<ImageReader> readers = ImageIO.getImageReadersByFormatName( this.format );
while (readers.hasNext()) {
try {
ImageReader reader = readers.next();
reader.setInput(iis);
reader.read(0);
result.setResultCode( Result.RESULT_CODE_OK );
break;
} catch (IOException exp) {
log.debug( "checkImage {}", exp.getMessage() );
}
}
if (readers.hasNext()) {
ImageReader reader = readers.next();
reader.setInput(iis);
reader.read(0);
return DocTypeValidationResult.newOk();
}
} catch (Exception exp) {
log.debug( "checkImage (v2) {}", exp.getMessage() );
}
return result;
return DocTypeValidationResult.newFail();
}

protected ImageValidator(String mimeType, Set<String> supportedExtensions, String format, int javaMajorVersionRequired) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import java.io.ByteArrayOutputStream;
import java.io.InputStream;

import lombok.extern.slf4j.Slf4j;
import org.bouncycastle.cms.CMSException;
import org.fugerit.java.core.cfg.ConfigRuntimeException;
import org.fugerit.java.core.function.SafeFunction;
import org.fugerit.java.doc.val.core.DocTypeValidationResult;
Expand All @@ -13,6 +15,7 @@
import lombok.Getter;
import lombok.Setter;

@Slf4j
public class P7MContentValidator extends AbstractDocTypeValidator {

public static final boolean DEFAULT_PROCEED_ON_INNTER_CHECK = Boolean.FALSE;
Expand Down Expand Up @@ -51,10 +54,11 @@ public String checkInnerType(InputStream is ) {
} else {
throw new ConfigRuntimeException( "Content not valid for this validator facade!" );
}
} else {
return null;
}
} catch (CMSException e) {
log.warn( String.format( "Error on inner check : %s", e ) );
}
return null;
} );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ public void testP7MAsP7MKo() {
Assert.assertTrue( ok );
}

@Test
public void testKO() {
Assert.assertNull( this.worker( CONTENT_JPG_PROCEED, "docx_as_docx.docx" ) );
}

@Test
public void testProccedKo() {
Assert.assertNull( this.worker( CONTENT_JPG_PROCEED, FILENAME_PDF_AS_P7M ) );
Expand Down
Binary file not shown.

0 comments on commit b2d4dc1

Please sign in to comment.