From 321f40d3a897aa4470f3a9565d2ecf6c5e6f608c Mon Sep 17 00:00:00 2001 From: Thomas Scheffler Date: Mon, 24 Jul 2023 15:48:16 +0200 Subject: [PATCH] Add MCRImage.getInstance() without derivateID required #35 --- .../java/org/mycore/imagetiler/MCRImage.java | 45 ++++++++++++++++--- .../org/mycore/imagetiler/MCRImageTest.java | 14 +++++- 2 files changed, 52 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/mycore/imagetiler/MCRImage.java b/src/main/java/org/mycore/imagetiler/MCRImage.java index 65ffb2c..f3a4ba9 100644 --- a/src/main/java/org/mycore/imagetiler/MCRImage.java +++ b/src/main/java/org/mycore/imagetiler/MCRImage.java @@ -35,6 +35,7 @@ import java.nio.file.StandardOpenOption; import java.util.Iterator; import java.util.Locale; +import java.util.Objects; import java.util.concurrent.atomic.AtomicInteger; import java.util.stream.IntStream; import java.util.zip.ZipEntry; @@ -205,6 +206,33 @@ public static MCRImage getInstance(final Path file, final String derivateID, fin return new MCRMemSaveImage(file, derivateID, imagePath); } + /** + * returns an instance of MCRImage (or subclass) based on the provided parameters. + * + * @param baseDir the base directory where the image is located + * @param file the image file + * @param targetDir the target directory where the tiled image should be saved + * @return a new instance of MCRImage representing file + */ + public static MCRImage getInstance(final Path baseDir, final Path file, final Path targetDir) { + Path imageFile = safeResolve(baseDir, file); + String relImagePath = baseDir.toAbsolutePath().relativize(imageFile).toString(); + MCRMemSaveImage image = new MCRMemSaveImage(imageFile, null, relImagePath); + image.setTileDir(targetDir); + return image; + } + + public static Path safeResolve(Path basePath, Path resolve) { + Path absoluteBasePath = Objects.requireNonNull(basePath).toAbsolutePath(); + final Path resolved = absoluteBasePath + .resolve(Objects.requireNonNull(resolve)) + .normalize(); + if (resolved.startsWith(absoluteBasePath.normalize())) { + return resolved; + } + throw new IllegalArgumentException("Bad path: " + resolve); + } + /** * calculates the amount of tiles produces by this image dimensions. * @@ -233,9 +261,7 @@ public static int getTileCount(final int imageWidth, final int imageHeight) { * @return tile directory of derivate if imagePath is null or the tile file (.iview2) */ public static Path getTiledFile(final Path tileDir, final String derivateID, final String imagePath) { - if (LOGGER.isDebugEnabled()) { - LOGGER.debug("tileDir: {}, derivate: {}, imagePath: {}", tileDir, derivateID, imagePath); - } + LOGGER.debug("tileDir: {}, derivate: {}, imagePath: {}", tileDir, derivateID, imagePath); Path tileFile = getTiledFileBaseDir(tileDir, derivateID); if (imagePath == null) { return tileFile; @@ -513,6 +539,13 @@ public MCRTiledPictureProps tile() throws IOException { return tile(null); } + private String getName() { + if (derivate != null) { + return derivate + ":" + imagePath; + } + return imageFile.toString(); + } + /** * starts the tile process. * @@ -522,7 +555,7 @@ public MCRTiledPictureProps tile() throws IOException { */ public MCRTiledPictureProps tile(MCRTileEventHandler eventHandler) throws IOException { long start = System.nanoTime(); - LOGGER.info(String.format(Locale.ENGLISH, "Start tiling of %s:%s", derivate, imagePath)); + LOGGER.info(String.format(Locale.ENGLISH, "Start tiling of %s", getName())); setOrientation(); //waterMarkFile = ImageIO.read(new File(MCRIview2Props.getProperty("Watermark"))); //initialize some basic variables @@ -556,8 +589,8 @@ public MCRTiledPictureProps tile(MCRTileEventHandler eventHandler) throws IOExce final MCRTiledPictureProps imageProperties = getImageProperties(); long pixel = (long) imageProperties.getWidth() * imageProperties.getHeight(); LOGGER.info(() -> String.format(Locale.ENGLISH, - "Finished tiling of %s:%s in %.0f ms (%d MPixel/s). ", - derivate, imagePath, (end - start) / 1e6, 1000 * pixel / (end - start))); + "Finished tiling of %s in %.0f ms (%d MPixel/s). ", + getName(), (end - start) / 1e6, 1000 * pixel / (end - start))); return imageProperties; } diff --git a/src/test/java/org/mycore/imagetiler/MCRImageTest.java b/src/test/java/org/mycore/imagetiler/MCRImageTest.java index 425fc9d..600ef1b 100644 --- a/src/test/java/org/mycore/imagetiler/MCRImageTest.java +++ b/src/test/java/org/mycore/imagetiler/MCRImageTest.java @@ -87,7 +87,7 @@ private static boolean deleteDirectory(final Path path) { */ @Before public void setUp() throws IOException { - Path testFilesDir = Path.of("src").resolve("test").resolve("resources"); + Path testFilesDir = Path.of("src", "test", "resources"); final Path landscapeZip = testFilesDir.resolve("landscape.zip"); landscapeZipFS = FileSystems.newFileSystem(landscapeZip, this.getClass().getClassLoader()); pics.put("small", testFilesDir.resolve("Bay_of_Noboto.jpg")); @@ -248,4 +248,16 @@ public void testgetTiledFile() { assertEquals("Path to file is not es axpected.", pExpected, tiledFile); } + @Test + public void testGetTiledFileWithoutMCR() throws IOException { + Path firstImage = pics.values().stream().findFirst().get().toAbsolutePath(); + Path baseDir = firstImage.getParent().getParent().toAbsolutePath(); + Path targetDir = Path.of("target"); + Path imageFile = baseDir.relativize(firstImage); + MCRImage image = MCRImage.getInstance(baseDir, imageFile, targetDir); + Path tiledFile = MCRImage.getTiledFile(targetDir, null, imageFile.toString()); + image.tile(); + assertTrue("Tiled file is not present " + tiledFile, Files.exists(tiledFile)); + } + }