mirror of
https://github.com/Alfresco/alfresco-transform-core.git
synced 2025-08-14 17:58:27 +00:00
ACS-9926 added logic to avoid TiffImageReader provided by JAI library (#1130)
* ACS-9926 added logic to skip TiffReader provided by JAI library * skip the jai implementation Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * minor fix pmd * Update ImageToPdfTransformer.java * uses existing error message * updated hardcoded class name * updated headers and added unit test * precommit fix * PMD fix * added suggested change * removed wildcard import * precommit fix --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
* #%L
|
||||
* Alfresco Transform Core
|
||||
* %%
|
||||
* Copyright (C) 2005 - 2024 Alfresco Software Limited
|
||||
* Copyright (C) 2005 - 2025 Alfresco Software Limited
|
||||
* %%
|
||||
* This file is part of the Alfresco software.
|
||||
* -
|
||||
@@ -39,6 +39,7 @@ import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Comparator;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Stream;
|
||||
@@ -81,6 +82,7 @@ public class ImageToPdfTransformer implements CustomTransformerFileAdaptor
|
||||
private static final String DEFAULT_PDF_FORMAT_STRING = "DEFAULT"; // pdf format to use when no pdf format specified
|
||||
private static final String DEFAULT_PDF_ORIENTATION_STRING = "DEFAULT";
|
||||
private static final float PDFBOX_POINTS_PER_INCH = 72.0F;
|
||||
private static final List<String> DENY_LIST = List.of("com.github.jaiimageio.impl.plugins.tiff.TIFFImageReader");
|
||||
|
||||
@Override
|
||||
public String getTransformerName()
|
||||
@@ -130,10 +132,17 @@ public class ImageToPdfTransformer implements CustomTransformerFileAdaptor
|
||||
{
|
||||
throw new IOException(String.format(INVALID_IMAGE_ERROR_MESSAGE, imageName, mimetype));
|
||||
}
|
||||
final ImageReader imageReader = imageReaders.next();
|
||||
imageReader.setInput(imageInputStream);
|
||||
|
||||
return imageReader;
|
||||
while (imageReaders.hasNext())
|
||||
{
|
||||
ImageReader reader = imageReaders.next();
|
||||
// Only process if the reader class is not in the deny list
|
||||
if (!DENY_LIST.contains(reader.getClass().getName()))
|
||||
{
|
||||
reader.setInput(imageInputStream);
|
||||
return reader;
|
||||
}
|
||||
}
|
||||
throw new IOException(String.format(INVALID_IMAGE_ERROR_MESSAGE, imageName, mimetype));
|
||||
}
|
||||
|
||||
private void scaleAndDrawImage(final PDDocument pdfDocument, final BufferedImage bufferedImage, final String pdfFormat, final String pdfOrientation, final Map<String, Integer> resolution)
|
||||
|
@@ -2,7 +2,7 @@
|
||||
* #%L
|
||||
* Alfresco Transform Core
|
||||
* %%
|
||||
* Copyright (C) 2005 - 2024 Alfresco Software Limited
|
||||
* Copyright (C) 2005 - 2025 Alfresco Software Limited
|
||||
* %%
|
||||
* This file is part of the Alfresco software.
|
||||
* -
|
||||
@@ -44,6 +44,7 @@ import static org.alfresco.transform.common.RequestParamMap.START_PAGE;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
@@ -53,9 +54,12 @@ import java.util.function.BiPredicate;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Stream;
|
||||
import javax.imageio.ImageIO;
|
||||
import javax.imageio.ImageReader;
|
||||
import javax.imageio.stream.ImageInputStream;
|
||||
|
||||
import org.apache.pdfbox.pdmodel.PDDocument;
|
||||
import org.apache.pdfbox.pdmodel.common.PDRectangle;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
@@ -68,9 +72,11 @@ import org.mockito.MockitoAnnotations;
|
||||
import org.alfresco.transform.base.TransformManager;
|
||||
import org.alfresco.transform.misc.util.ArgumentsCartesianProduct;
|
||||
|
||||
@SuppressWarnings("PMD.AvoidAccessibilityAlteration")
|
||||
class ImageToPdfTransformerTest
|
||||
{
|
||||
private static final File sourceFile = loadFile("sample.gif");
|
||||
private static final File SOURCE_TIFF_FILE = loadFile("sample.tiff");
|
||||
private static final int sourceFileWidth;
|
||||
private static final int sourceFileHeight;
|
||||
|
||||
@@ -316,6 +322,28 @@ class ImageToPdfTransformerTest
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testFindImageReaderForTiffFiles()
|
||||
{
|
||||
try (ImageInputStream imageInputStream = ImageIO.createImageInputStream(SOURCE_TIFF_FILE))
|
||||
{
|
||||
Method method = ImageToPdfTransformer.class.getDeclaredMethod(
|
||||
"findImageReader", ImageInputStream.class, String.class, String.class);
|
||||
method.setAccessible(true);
|
||||
// when
|
||||
ImageReader imageReader = (ImageReader) method.invoke(
|
||||
transformer, imageInputStream, "sample.tiff", MIMETYPE_IMAGE_TIFF);
|
||||
|
||||
// then
|
||||
assertNotNull(imageReader, "Image reader should not be null for TIFF file");
|
||||
assertEquals("com.sun.imageio.plugins.tiff.TIFFImageReader", imageReader.getClass().getName(),
|
||||
"ImageReader should be com.sun.imageio.plugins.tiff.TIFFImageReader");
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Assertions.fail("Exception occurred: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
// ----------------------------------------------- Helper methods and classes -----------------------------------------------
|
||||
|
||||
private static BiFunction<Float, Float, PDRectangle> unchangedRectangle()
|
||||
|
Reference in New Issue
Block a user