Skip to content

Commit

Permalink
BXC-4737 cleanup temp imported thumbnail (#1817)
Browse files Browse the repository at this point in the history
* BXC-4737

* BXC-4737 make exception more specific
  • Loading branch information
sharonluong authored Oct 15, 2024
1 parent 3d47590 commit a0816f7
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand All @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,60 @@
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 {
private ImportThumbnailRequestProcessor processor;

@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
Expand All @@ -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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
Expand Down

0 comments on commit a0816f7

Please sign in to comment.