From a0816f73ece1c4ca6b3bfeb23b96f1594611255c Mon Sep 17 00:00:00 2001 From: sharonl <6546457+sharonluong@users.noreply.github.com> Date: Tue, 15 Oct 2024 14:45:56 -0400 Subject: [PATCH] BXC-4737 cleanup temp imported thumbnail (#1817) * BXC-4737 * BXC-4737 make exception more specific --- .../ImportThumbnailRequestProcessor.java | 23 +++++++++ .../camel/thumbnails/ThumbnailRouter.java | 15 ++++-- .../ImportThumbnailProcessorTest.java | 49 +++++++++++++------ .../camel/thumbnails/ThumbnailRouterTest.java | 2 + 4 files changed, 70 insertions(+), 19 deletions(-) diff --git a/services-camel-app/src/main/java/edu/unc/lib/boxc/services/camel/thumbnails/ImportThumbnailRequestProcessor.java b/services-camel-app/src/main/java/edu/unc/lib/boxc/services/camel/thumbnails/ImportThumbnailRequestProcessor.java index 589da47484..40e0b6b9ed 100644 --- a/services-camel-app/src/main/java/edu/unc/lib/boxc/services/camel/thumbnails/ImportThumbnailRequestProcessor.java +++ b/services-camel-app/src/main/java/edu/unc/lib/boxc/services/camel/thumbnails/ImportThumbnailRequestProcessor.java @@ -3,9 +3,15 @@ import edu.unc.lib.boxc.model.fcrepo.ids.PIDs; import edu.unc.lib.boxc.operations.jms.thumbnails.ImportThumbnailRequestSerializationHelper; import org.apache.camel.Exchange; +import org.apache.camel.Message; import org.apache.camel.Processor; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; import static edu.unc.lib.boxc.services.camel.util.CdrFcrepoHeaders.CdrBinaryMimeType; import static edu.unc.lib.boxc.services.camel.util.CdrFcrepoHeaders.CdrBinaryPath; @@ -17,6 +23,7 @@ * @author snluong */ public class ImportThumbnailRequestProcessor implements Processor { + private static final Logger log = LoggerFactory.getLogger(ImportThumbnailRequestProcessor.class); @Override public void process(Exchange exchange) throws IOException { var in = exchange.getIn(); @@ -30,4 +37,20 @@ public void process(Exchange exchange) throws IOException { in.setHeader(CdrBinaryMimeType, mimetype); in.setHeader(FCREPO_URI, repoPath); } + + /** + * Deletes the temporarily stored uploaded thumbnail file + * @param exchange + * @throws IOException + */ + public void cleanupTempThumbnailFile(Exchange exchange) throws IOException { + final Message in = exchange.getIn(); + String tempValue = (String) in.getHeader(CdrBinaryPath); + Path tempPath = Paths.get(tempValue); + + boolean deleted = Files.deleteIfExists(tempPath); + if (deleted) { + log.debug("Cleaned up leftover temp file {}", tempPath); + } + } } diff --git a/services-camel-app/src/main/java/edu/unc/lib/boxc/services/camel/thumbnails/ThumbnailRouter.java b/services-camel-app/src/main/java/edu/unc/lib/boxc/services/camel/thumbnails/ThumbnailRouter.java index 6c68957820..723f6e4555 100644 --- a/services-camel-app/src/main/java/edu/unc/lib/boxc/services/camel/thumbnails/ThumbnailRouter.java +++ b/services-camel-app/src/main/java/edu/unc/lib/boxc/services/camel/thumbnails/ThumbnailRouter.java @@ -30,11 +30,16 @@ public void configure() throws Exception { .bean(thumbnailRequestProcessor); from("{{cdr.import.thumbnails.stream.camel}}") - .routeId("DcrImportThumbnails") - .process(importThumbnailRequestProcessor) - .log(DEBUG, log, - "Received thumbnail request: importing thumbnail for ${headers[CamelFcrepoUri]}") + .routeId("DcrImportThumbnails") + .process(importThumbnailRequestProcessor) + .log(DEBUG, log, + "Received thumbnail request: importing thumbnail for ${headers[CamelFcrepoUri]}") + .doTry() // trigger JP2 generation sequentially followed by indexing - .to("direct:process.enhancement.imageAccessCopy", "direct:solrIndexing"); + .to("direct:process.enhancement.imageAccessCopy", "direct:solrIndexing") + .endDoTry() + .doFinally() + .bean(importThumbnailRequestProcessor, "cleanupTempThumbnailFile") + .end(); } } diff --git a/services-camel-app/src/test/java/edu/unc/lib/boxc/services/camel/thumbnails/ImportThumbnailProcessorTest.java b/services-camel-app/src/test/java/edu/unc/lib/boxc/services/camel/thumbnails/ImportThumbnailProcessorTest.java index c04c27a2eb..4ccb3e3c30 100644 --- a/services-camel-app/src/test/java/edu/unc/lib/boxc/services/camel/thumbnails/ImportThumbnailProcessorTest.java +++ b/services-camel-app/src/test/java/edu/unc/lib/boxc/services/camel/thumbnails/ImportThumbnailProcessorTest.java @@ -3,24 +3,30 @@ import edu.unc.lib.boxc.auth.api.models.AgentPrincipals; import edu.unc.lib.boxc.auth.fcrepo.models.AccessGroupSetImpl; import edu.unc.lib.boxc.auth.fcrepo.models.AgentPrincipalsImpl; +import edu.unc.lib.boxc.model.api.ids.PID; import edu.unc.lib.boxc.operations.jms.thumbnails.ImportThumbnailRequest; import edu.unc.lib.boxc.operations.jms.thumbnails.ImportThumbnailRequestSerializationHelper; import edu.unc.lib.boxc.services.camel.TestHelper; +import org.apache.camel.Exchange; +import org.apache.camel.Message; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; -import org.mockito.Mock; import java.io.IOException; +import java.nio.file.Files; import java.nio.file.Path; +import java.util.List; import static edu.unc.lib.boxc.services.camel.util.CdrFcrepoHeaders.CdrBinaryMimeType; import static edu.unc.lib.boxc.services.camel.util.CdrFcrepoHeaders.CdrBinaryPath; import static org.fcrepo.camel.FcrepoHeaders.FCREPO_URI; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; import static org.mockito.MockitoAnnotations.openMocks; public class ImportThumbnailProcessorTest { @@ -28,13 +34,29 @@ public class ImportThumbnailProcessorTest { @TempDir private Path path; + private Path tempThumbnailPath; private final AgentPrincipals agent = new AgentPrincipalsImpl("user", new AccessGroupSetImpl("agroup")); + private Exchange exchange; + private Message message; + private String mimetype = "image/jpeg"; + private PID pid; private AutoCloseable closeable; @BeforeEach public void init() throws IOException { closeable = openMocks(this); processor = new ImportThumbnailRequestProcessor(); + pid = TestHelper.makePid(); + tempThumbnailPath = path.resolve(pid.getId() + ".png"); + + var request = new ImportThumbnailRequest(); + request.setMimetype(mimetype); + request.setAgent(agent); + request.setStoragePath(path); + request.setPidString(pid.getId()); + + exchange = TestHelper.mockExchange(ImportThumbnailRequestSerializationHelper.toJson(request)); + message = exchange.getIn(); } @AfterEach @@ -44,21 +66,20 @@ void closeService() throws Exception { @Test public void testImportThumbnail() throws IOException { - var pid = TestHelper.makePid(); - var mimetype = "image/jpeg"; - var request = new ImportThumbnailRequest(); - request.setMimetype(mimetype); - request.setAgent(agent); - request.setStoragePath(path); - request.setPidString(pid.getId()); - - var exchange = TestHelper.mockExchange(ImportThumbnailRequestSerializationHelper.toJson(request)); - var message = exchange.getIn(); - processor.process(exchange); verify(message).setHeader(FCREPO_URI, pid.getRepositoryPath()); verify(message).setHeader(CdrBinaryMimeType, mimetype); verify(message).setHeader(CdrBinaryPath, path.toString()); } + + @Test + public void cleanupTempThumbnailFileTest() throws Exception { + Files.write(tempThumbnailPath, List.of("fake image")); + assertTrue(Files.exists(tempThumbnailPath)); + when(message.getHeader(eq(CdrBinaryPath))) + .thenReturn(tempThumbnailPath.toString()); + processor.cleanupTempThumbnailFile(exchange); + assertFalse(Files.exists(tempThumbnailPath)); + } } diff --git a/services-camel-app/src/test/java/edu/unc/lib/boxc/services/camel/thumbnails/ThumbnailRouterTest.java b/services-camel-app/src/test/java/edu/unc/lib/boxc/services/camel/thumbnails/ThumbnailRouterTest.java index bb849fe838..219116cf5d 100644 --- a/services-camel-app/src/test/java/edu/unc/lib/boxc/services/camel/thumbnails/ThumbnailRouterTest.java +++ b/services-camel-app/src/test/java/edu/unc/lib/boxc/services/camel/thumbnails/ThumbnailRouterTest.java @@ -9,6 +9,7 @@ import edu.unc.lib.boxc.operations.jms.thumbnails.ThumbnailRequestSerializationHelper; import edu.unc.lib.boxc.services.camel.TestHelper; import org.apache.camel.BeanInject; +import org.apache.camel.Exchange; import org.apache.camel.Produce; import org.apache.camel.ProducerTemplate; import org.apache.camel.builder.AdviceWith; @@ -68,6 +69,7 @@ public void importRequestSentTest() throws Exception { template.sendBody(body); verify(importProcessor).process(any()); + verify(importProcessor).cleanupTempThumbnailFile(any(Exchange.class)); solrEndpoint.assertIsSatisfied(); imageAccessCopyEndpoint.assertIsSatisfied(); }