fix(pdf-writer): use same-dir temp files and harden cleanup (#719)
This commit is contained in:
+19
-16
@@ -43,13 +43,13 @@ public class PdfMetadataWriter implements MetadataWriter {
|
||||
}
|
||||
|
||||
Path filePath = file.toPath();
|
||||
Path parentDir = filePath.getParent();
|
||||
Path backupPath = null;
|
||||
boolean backupCreated = false;
|
||||
File tempFile = null;
|
||||
Path tempPath = null;
|
||||
|
||||
try {
|
||||
String prefix = "pdfBackup-" + UUID.randomUUID() + "-";
|
||||
backupPath = Files.createTempFile(prefix, ".pdf");
|
||||
backupPath = Files.createTempFile(parentDir, ".pdfBackup-", ".pdf");
|
||||
Files.copy(filePath, backupPath, StandardCopyOption.REPLACE_EXISTING);
|
||||
backupCreated = true;
|
||||
} catch (IOException e) {
|
||||
@@ -58,10 +58,10 @@ public class PdfMetadataWriter implements MetadataWriter {
|
||||
|
||||
try (PdfDocument doc = PdfDocument.open(filePath)) {
|
||||
applyMetadataToDocument(doc, metadataEntity, clear);
|
||||
tempFile = File.createTempFile("pdfmeta-", ".pdf");
|
||||
doc.save(tempFile.toPath(), SaveOptions.SKIP_VALIDATION);
|
||||
Files.move(tempFile.toPath(), filePath, StandardCopyOption.REPLACE_EXISTING);
|
||||
tempFile = null; // Prevent deletion in finally block after successful move
|
||||
tempPath = Files.createTempFile(parentDir, ".pdfmeta-", ".pdf");
|
||||
doc.save(tempPath, SaveOptions.SKIP_VALIDATION);
|
||||
Files.move(tempPath, filePath, StandardCopyOption.REPLACE_EXISTING);
|
||||
tempPath = null;
|
||||
log.info("Successfully embedded metadata into PDF: {}", file.getName());
|
||||
} catch (Exception e) {
|
||||
log.warn("Failed to write metadata to PDF {}: {}", file.getName(), e.getMessage(), e);
|
||||
@@ -74,8 +74,12 @@ public class PdfMetadataWriter implements MetadataWriter {
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
if (tempFile != null && tempFile.exists()) {
|
||||
tempFile.delete();
|
||||
if (tempPath != null) {
|
||||
try {
|
||||
Files.deleteIfExists(tempPath);
|
||||
} catch (IOException e) {
|
||||
log.warn("Could not delete PDF temp file: {}", e.getMessage());
|
||||
}
|
||||
}
|
||||
if (backupCreated) {
|
||||
try {
|
||||
@@ -418,18 +422,17 @@ public class PdfMetadataWriter implements MetadataWriter {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Extract numeric ID from slug format "12345678-book-title"
|
||||
int dashIndex = goodreadsId.indexOf('-');
|
||||
if (dashIndex > 0) {
|
||||
String numericPart = goodreadsId.substring(0, dashIndex);
|
||||
// Validate it's actually numeric
|
||||
// Extract numeric ID from slug format "12345678-book-title" or "12345678.Book_Title"
|
||||
int sep = goodreadsId.indexOf('-');
|
||||
if (sep < 0) sep = goodreadsId.indexOf('.');
|
||||
if (sep > 0) {
|
||||
String numericPart = goodreadsId.substring(0, sep);
|
||||
if (numericPart.matches("\\d+")) {
|
||||
return numericPart;
|
||||
}
|
||||
}
|
||||
|
||||
// Already just the ID, or return as-is if it's all numeric
|
||||
return goodreadsId.matches("\\d+") ? goodreadsId : goodreadsId;
|
||||
return goodreadsId;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+12
-8
@@ -88,7 +88,7 @@ class PdfMetadataWriterTest {
|
||||
meta.setPublishedDate(LocalDate.of(2022, 11, 22));
|
||||
meta.setLanguage("en");
|
||||
meta.setPageCount(754);
|
||||
|
||||
|
||||
List<AuthorEntity> authors = new ArrayList<>();
|
||||
AuthorEntity author = new AuthorEntity();
|
||||
author.setName("Jason C McDonald");
|
||||
@@ -197,7 +197,7 @@ class PdfMetadataWriterTest {
|
||||
writer.saveMetadataToFile(pdf, meta, null, null);
|
||||
|
||||
BookMetadata result = extractor.extractMetadata(pdf);
|
||||
assertEquals("52555538", result.getGoodreadsId(),
|
||||
assertEquals("52555538", result.getGoodreadsId(),
|
||||
"Goodreads ID should be normalized to just the numeric part");
|
||||
}
|
||||
|
||||
@@ -287,17 +287,21 @@ class PdfMetadataWriterTest {
|
||||
File pdf = createEmptyPdf("tags-moods.pdf");
|
||||
|
||||
BookMetadataEntity meta = createBasicMetadata();
|
||||
|
||||
|
||||
Set<TagEntity> tags = new HashSet<>();
|
||||
TagEntity tag1 = new TagEntity(); tag1.setName("Python");
|
||||
TagEntity tag2 = new TagEntity(); tag2.setName("Programming");
|
||||
TagEntity tag1 = new TagEntity();
|
||||
tag1.setName("Python");
|
||||
TagEntity tag2 = new TagEntity();
|
||||
tag2.setName("Programming");
|
||||
tags.add(tag1);
|
||||
tags.add(tag2);
|
||||
meta.setTags(tags);
|
||||
|
||||
|
||||
Set<MoodEntity> moods = new HashSet<>();
|
||||
MoodEntity mood1 = new MoodEntity(); mood1.setName("Educational");
|
||||
MoodEntity mood2 = new MoodEntity(); mood2.setName("Technical");
|
||||
MoodEntity mood1 = new MoodEntity();
|
||||
mood1.setName("Educational");
|
||||
MoodEntity mood2 = new MoodEntity();
|
||||
mood2.setName("Technical");
|
||||
moods.add(mood1);
|
||||
moods.add(mood2);
|
||||
meta.setMoods(moods);
|
||||
|
||||
Reference in New Issue
Block a user