Skip to content

Commit

Permalink
SourceResolverHelper review
Browse files Browse the repository at this point in the history
  • Loading branch information
fugerit79 committed Oct 3, 2023
1 parent 8c8049a commit f16e757
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.fugerit.java.doc.base.helper;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

import org.fugerit.java.core.io.StreamIO;
Expand All @@ -15,18 +16,18 @@ private SourceResolverHelper() {} // java:S1118

public static final String MODE_CLASSLOADER = StreamHelper.PATH_CLASSLOADER;

private static byte[] byteResolverHelper( String path ) throws IOException {
try ( InputStream is = path.startsWith( StreamHelper.PATH_CLASSLOADER ) ? StreamHelper.resolveStream( path ) : new URL( path ).openConnection().getInputStream() ) {
return StreamIO.readBytes( is );
}
}

public static String resolveImageToBase64( DocImage img ) throws IOException {
return HelperIOException.get( () -> {
String path = img.getUrl();
String base64 = img.getBase64();
if ( StringUtils.isEmpty( base64 ) && path != null ) {
byte[] data = null;
if ( path.startsWith( StreamHelper.PATH_CLASSLOADER ) ) {
data = StreamIO.readBytes( StreamHelper.resolveStream( path ) );
} else {
URL url = new URL( path );
data = StreamIO.readBytes( url.openConnection().getInputStream() );
}
byte[] data = byteResolverHelper( path );
base64 = Base64Helper.encodeBase64String( data );
} else {
throw new IOException( "Null path and base64 provided!" );
Expand All @@ -43,12 +44,7 @@ public static byte[] resolveImage( DocImage img ) throws IOException {
if ( StringUtils.isNotEmpty( base64 ) ) {
data = Base64Helper.decodeBase64String( base64 );
} else if ( path != null ) {
if ( path.startsWith( StreamHelper.PATH_CLASSLOADER ) ) {
data = StreamIO.readBytes( StreamHelper.resolveStream( path ) );
} else {
URL url = new URL( path );
data = StreamIO.readBytes( url.openConnection().getInputStream() );
}
data = byteResolverHelper( path );
} else {
throw new IOException( "Null path provided!" );
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package test.org.fugerit.java.doc.base.helper;

import java.io.IOException;
import java.util.Base64;

import org.fugerit.java.core.io.StreamIO;
import org.fugerit.java.core.lang.helpers.ClassHelper;
import org.fugerit.java.doc.base.helper.SourceResolverHelper;
import org.fugerit.java.doc.base.model.DocImage;
import org.junit.Assert;
import org.junit.Test;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class TestSourceResolverHelper {

@Test
public void test1() throws IOException {
DocImage docImage = new DocImage();
Assert.assertThrows( IOException.class , () -> SourceResolverHelper.resolveImageToBase64( docImage ) );
Assert.assertThrows( IOException.class , () -> SourceResolverHelper.resolveImage( docImage ) );
String pathImg = "test/img_test_blue.png";
String base64 = Base64.getEncoder().encodeToString( StreamIO.readBytes( ClassHelper.loadFromDefaultClassLoader( pathImg ) ) );
docImage.setBase64(base64);
Assert.assertNotNull( SourceResolverHelper.resolveImage(docImage) );
log.info( "docImage : {}", docImage );
}

}

0 comments on commit f16e757

Please sign in to comment.