Skip to content

Commit

Permalink
PrerenderPdf: If not enforced, only create pdf if no pdf exists for the
Browse files Browse the repository at this point in the history
respective file or if it is older than the file
  • Loading branch information
Florian Alpers committed Jan 22, 2025
1 parent 5218d52 commit 4634cb4
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.FileTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -748,4 +750,26 @@ public static boolean isWithin(Path path, Path parent) {
Path normalized = path.toAbsolutePath().normalize();
return normalized.startsWith(parent.toAbsolutePath().normalize());
}

public static boolean isYoungerThan(Path path, Path reference) {
FileTime pathDate;
try {
pathDate = getDateModified(path);
FileTime referenceDate = getDateModified(reference);
return pathDate.toInstant().isAfter(referenceDate.toInstant());
} catch (IOException e) {
logger.error("Cannot compare ages of {} and {}: {}", path, reference, e.toString());
return false;
}
}

public static FileTime getDateCreated(Path path) throws IOException {
BasicFileAttributes attr = Files.readAttributes(path, BasicFileAttributes.class);
return attr.creationTime();
}

public static FileTime getDateModified(Path path) throws IOException {
BasicFileAttributes attr = Files.readAttributes(path, BasicFileAttributes.class);
return attr.lastModifiedTime() != null ? attr.lastModifiedTime() : attr.lastAccessTime();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

import java.io.IOException;
import java.io.OutputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
Expand Down Expand Up @@ -99,19 +98,16 @@ private boolean createPdfFiles(String pi, String configVariant, boolean force) t
Path altoFolder = dataFolders.get(ALTO);
if (imageFolder != null && pdfFolder != null && Files.exists(imageFolder)) {
List<Path> imageFiles = FileTools.listFiles(imageFolder, FileTools.IMAGE_NAME_FILTER);
List<Path> pdfFiles = FileTools.listFiles(pdfFolder, FileTools.PDF_NAME_FILTER);
if (imageFiles.isEmpty()) {
logger.trace("No images in {}. Abandoning task", imageFolder);
} else if (imageFiles.size() == pdfFiles.size() && !force) {
logger.trace("PDF files already exist. Abandoning task");
} else {
return createPdfFiles(configVariant, pdfFolder, altoFolder, imageFiles);
return createPdfFiles(configVariant, pdfFolder, altoFolder, imageFiles, force);
}
}
return true;
}

private boolean createPdfFiles(String configVariant, Path pdfFolder, Path altoFolder, List<Path> imageFiles) {
private boolean createPdfFiles(String configVariant, Path pdfFolder, Path altoFolder, List<Path> imageFiles, boolean force) {
if (!Files.exists(pdfFolder)) {
try {
Files.createDirectories(pdfFolder);
Expand All @@ -121,28 +117,35 @@ private boolean createPdfFiles(String configVariant, Path pdfFolder, Path altoFo
}
}
for (Path imagePath : imageFiles) {
if (!createPdfFile(imagePath, pdfFolder, altoFolder, configVariant)) {
try {
createPdfFile(imagePath, pdfFolder, altoFolder, configVariant, force);
} catch (PresentationException e) {
logger.error("Error creating pdf for {}. Abandoning task", imagePath);
return false;
}
}
return true;
}

private boolean createPdfFile(Path imagePath, Path pdfFolder, Path altoFolder, String configVariant) {
private boolean createPdfFile(Path imagePath, Path pdfFolder, Path altoFolder, String configVariant, boolean force) throws PresentationException {
Map<String, String> params = Map.of(
"config", configVariant,
"ignoreCache", "true",
"altoSource", Optional.ofNullable(altoFolder).map(f -> PathConverter.toURI(f.toAbsolutePath()).toString()).orElse(""),
"imageSource", PathConverter.toURI(imagePath.getParent().toAbsolutePath()).toString());
Path pdfPath = pdfFolder.resolve(FileTools.replaceExtension(imagePath.getFileName(), "pdf"));
try (OutputStream out = Files.newOutputStream(pdfPath, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING)) {
SinglePdfRequest request = new SinglePdfRequest(imagePath.getFileName().toString(), params);
new GetPdfAction().writePdf(request, this.contentServerConfiguration, out);
} catch (ContentLibException | IOException | URISyntaxException e) {
logger.error("Failed to create pdf file {} from {}. Reason: {}", pdfPath, imagePath, e.toString());
if (force || !Files.exists(pdfPath) || FileTools.isYoungerThan(imagePath, pdfPath)) {
try (OutputStream out = Files.newOutputStream(pdfPath, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING)) {
SinglePdfRequest request = new SinglePdfRequest(imagePath.getFileName().toString(), params);
new GetPdfAction().writePdf(request, this.contentServerConfiguration, out);
} catch (ContentLibException | IOException | URISyntaxException e) {
throw new PresentationException("Failed to create pdf file {} from {}. Reason: {}", pdfPath, imagePath, e.toString());
}
return true;
} else {
logger.trace("No pdf created at {}, it already exists", pdfPath);
return false;
}
return true;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Path;

import org.apache.commons.lang3.StringUtils;
import org.ehcache.shadow.org.terracotta.utilities.io.Files;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
Expand Down Expand Up @@ -166,4 +169,18 @@ void getBottomFolderFromPathString_shouldReturnEmptyStringIfNoFolderInPath() {
void getFilenameFromPathString_shouldReturnFileNameCorrectly() throws Exception {
Assertions.assertEquals("00000001.xml", FileTools.getFilenameFromPathString("data/1/alto/PPN123/00000001.xml"));
}

@Test
void testIsYounger(@TempDir Path tempDir) throws IOException, InterruptedException {
Path file1 = tempDir.resolve("file1.txt");
Path file2 = tempDir.resolve("file2.txt");

Files.createFile(file1);
Thread.sleep(100);
Files.createFile(file2);

Assertions.assertTrue(FileTools.isYoungerThan(file2, file1));
Assertions.assertFalse(FileTools.isYoungerThan(file1, file2));
Assertions.assertFalse(FileTools.isYoungerThan(file1, file1));
}
}

0 comments on commit 4634cb4

Please sign in to comment.