Skip to content

Fix image validation #262 #263

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.