fix(metadata): include custom namespace fields in file write checks (#257)
* fix(metadata): include custom namespace fields in file write checks * fix(metadata): simplify booklore metadata change checks and improve value validation
This commit is contained in:
+96
-8
@@ -41,9 +41,8 @@ import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.StandardCopyOption;
|
||||
import java.util.Comparator;
|
||||
import java.util.Objects;
|
||||
import java.util.UUID;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
@@ -211,6 +210,10 @@ public class EpubMetadataWriter implements MetadataWriter {
|
||||
}
|
||||
}
|
||||
|
||||
if (!hasChanges[0] && hasBookloreMetadataChanges(metadataElement, metadata)) {
|
||||
hasChanges[0] = true;
|
||||
}
|
||||
|
||||
if (hasChanges[0]) {
|
||||
addBookloreMetadata(metadataElement, opfDoc, metadata);
|
||||
cleanupCalibreArtifacts(metadataElement, opfDoc);
|
||||
@@ -759,6 +762,89 @@ public class EpubMetadataWriter implements MetadataWriter {
|
||||
return version != null && version.trim().startsWith("3");
|
||||
}
|
||||
|
||||
private boolean hasBookloreMetadataChanges(Element metadataElement, BookMetadataEntity metadata) {
|
||||
Map<String, String> existing = new TreeMap<>();
|
||||
NodeList metas = metadataElement.getElementsByTagNameNS("*", "meta");
|
||||
for (int i = 0; i < metas.getLength(); i++) {
|
||||
Element meta = (Element) metas.item(i);
|
||||
String property = meta.getAttribute("property");
|
||||
String name = meta.getAttribute("name");
|
||||
String key = property.startsWith("booklore:") ? property : (name.startsWith("booklore:") ? name : null);
|
||||
if (key != null) {
|
||||
String value = meta.getAttribute("content").isEmpty() ? meta.getTextContent() : meta.getAttribute("content");
|
||||
if (!isEffectivelyZeroOrBlank(value)) {
|
||||
existing.put(key, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Map<String, String> expected = new TreeMap<>();
|
||||
if (StringUtils.isNotBlank(metadata.getSubtitle())) {
|
||||
expected.put("booklore:subtitle", metadata.getSubtitle());
|
||||
}
|
||||
if (metadata.getPageCount() != null && metadata.getPageCount() > 0) {
|
||||
expected.put("booklore:page_count", String.valueOf(metadata.getPageCount()));
|
||||
}
|
||||
if (metadata.getSeriesTotal() != null && metadata.getSeriesTotal() > 0) {
|
||||
expected.put("booklore:series_total", String.valueOf(metadata.getSeriesTotal()));
|
||||
}
|
||||
if (metadata.getAmazonRating() != null && metadata.getAmazonRating() > 0) {
|
||||
expected.put("booklore:amazon_rating", String.valueOf(metadata.getAmazonRating()));
|
||||
}
|
||||
if (metadata.getAmazonReviewCount() != null && metadata.getAmazonReviewCount() > 0) {
|
||||
expected.put("booklore:amazon_review_count", String.valueOf(metadata.getAmazonReviewCount()));
|
||||
}
|
||||
if (metadata.getGoodreadsRating() != null && metadata.getGoodreadsRating() > 0) {
|
||||
expected.put("booklore:goodreads_rating", String.valueOf(metadata.getGoodreadsRating()));
|
||||
}
|
||||
if (metadata.getGoodreadsReviewCount() != null && metadata.getGoodreadsReviewCount() > 0) {
|
||||
expected.put("booklore:goodreads_review_count", String.valueOf(metadata.getGoodreadsReviewCount()));
|
||||
}
|
||||
if (metadata.getHardcoverRating() != null && metadata.getHardcoverRating() > 0) {
|
||||
expected.put("booklore:hardcover_rating", String.valueOf(metadata.getHardcoverRating()));
|
||||
}
|
||||
if (metadata.getHardcoverReviewCount() != null && metadata.getHardcoverReviewCount() > 0) {
|
||||
expected.put("booklore:hardcover_review_count", String.valueOf(metadata.getHardcoverReviewCount()));
|
||||
}
|
||||
if (metadata.getLubimyczytacRating() != null && metadata.getLubimyczytacRating() > 0) {
|
||||
expected.put("booklore:lubimyczytac_rating", String.valueOf(metadata.getLubimyczytacRating()));
|
||||
}
|
||||
if (metadata.getRanobedbRating() != null && metadata.getRanobedbRating() > 0) {
|
||||
expected.put("booklore:ranobedb_rating", String.valueOf(metadata.getRanobedbRating()));
|
||||
}
|
||||
if (metadata.getMoods() != null && !metadata.getMoods().isEmpty()) {
|
||||
String moodsJson = "[" + metadata.getMoods().stream()
|
||||
.map(mood -> "\"" + mood.getName().replace("\"", "\\\"") + "\"")
|
||||
.sorted()
|
||||
.collect(Collectors.joining(", ")) + "]";
|
||||
expected.put("booklore:moods", moodsJson);
|
||||
}
|
||||
if (metadata.getTags() != null && !metadata.getTags().isEmpty()) {
|
||||
String tagsJson = "[" + metadata.getTags().stream()
|
||||
.map(tag -> "\"" + tag.getName().replace("\"", "\\\"") + "\"")
|
||||
.sorted()
|
||||
.collect(Collectors.joining(", ")) + "]";
|
||||
expected.put("booklore:tags", tagsJson);
|
||||
}
|
||||
if (metadata.getAgeRating() != null) {
|
||||
expected.put("booklore:age_rating", String.valueOf(metadata.getAgeRating()));
|
||||
}
|
||||
if (StringUtils.isNotBlank(metadata.getContentRating())) {
|
||||
expected.put("booklore:content_rating", metadata.getContentRating());
|
||||
}
|
||||
|
||||
return !existing.equals(expected);
|
||||
}
|
||||
|
||||
private static boolean isEffectivelyZeroOrBlank(String value) {
|
||||
if (value == null || value.isBlank()) return true;
|
||||
try {
|
||||
return Double.parseDouble(value) <= 0;
|
||||
} catch (NumberFormatException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private void removeAllBookloreMetadata(Element metadataElement) {
|
||||
NodeList metas = metadataElement.getElementsByTagNameNS("*", "meta");
|
||||
for (int i = metas.getLength() - 1; i >= 0; i--) {
|
||||
@@ -956,16 +1042,18 @@ public class EpubMetadataWriter implements MetadataWriter {
|
||||
}
|
||||
|
||||
if (metadata.getMoods() != null && !metadata.getMoods().isEmpty()) {
|
||||
String moodsJson = "[" + String.join(", ", metadata.getMoods().stream()
|
||||
String moodsJson = "[" + metadata.getMoods().stream()
|
||||
.map(mood -> "\"" + mood.getName().replace("\"", "\\\"") + "\"")
|
||||
.toList()) + "]";
|
||||
.sorted()
|
||||
.collect(Collectors.joining(", ")) + "]";
|
||||
metadataElement.appendChild(createBookloreMetaElement(doc, "moods", moodsJson, epub3));
|
||||
}
|
||||
|
||||
|
||||
if (metadata.getTags() != null && !metadata.getTags().isEmpty()) {
|
||||
String tagsJson = "[" + String.join(", ", metadata.getTags().stream()
|
||||
String tagsJson = "[" + metadata.getTags().stream()
|
||||
.map(tag -> "\"" + tag.getName().replace("\"", "\\\"") + "\"")
|
||||
.toList()) + "]";
|
||||
.sorted()
|
||||
.collect(Collectors.joining(", ")) + "]";
|
||||
metadataElement.appendChild(createBookloreMetaElement(doc, "tags", tagsJson, epub3));
|
||||
}
|
||||
|
||||
|
||||
@@ -182,39 +182,39 @@ public class MetadataChangeDetector {
|
||||
new FieldDescriptor<>("pageCount",
|
||||
BookMetadata::getPageCount, BookMetadataEntity::getPageCount,
|
||||
BookMetadata::getPageCountLocked, BookMetadataEntity::getPageCountLocked,
|
||||
MetadataClearFlags::isPageCount, false),
|
||||
MetadataClearFlags::isPageCount, true),
|
||||
new FieldDescriptor<>("amazonRating",
|
||||
BookMetadata::getAmazonRating, BookMetadataEntity::getAmazonRating,
|
||||
BookMetadata::getAmazonRatingLocked, BookMetadataEntity::getAmazonRatingLocked,
|
||||
MetadataClearFlags::isAmazonRating, false),
|
||||
MetadataClearFlags::isAmazonRating, true),
|
||||
new FieldDescriptor<>("amazonReviewCount",
|
||||
BookMetadata::getAmazonReviewCount, BookMetadataEntity::getAmazonReviewCount,
|
||||
BookMetadata::getAmazonReviewCountLocked, BookMetadataEntity::getAmazonReviewCountLocked,
|
||||
MetadataClearFlags::isAmazonReviewCount, false),
|
||||
MetadataClearFlags::isAmazonReviewCount, true),
|
||||
new FieldDescriptor<>("goodreadsRating",
|
||||
BookMetadata::getGoodreadsRating, BookMetadataEntity::getGoodreadsRating,
|
||||
BookMetadata::getGoodreadsRatingLocked, BookMetadataEntity::getGoodreadsRatingLocked,
|
||||
MetadataClearFlags::isGoodreadsRating, false),
|
||||
MetadataClearFlags::isGoodreadsRating, true),
|
||||
new FieldDescriptor<>("goodreadsReviewCount",
|
||||
BookMetadata::getGoodreadsReviewCount, BookMetadataEntity::getGoodreadsReviewCount,
|
||||
BookMetadata::getGoodreadsReviewCountLocked, BookMetadataEntity::getGoodreadsReviewCountLocked,
|
||||
MetadataClearFlags::isGoodreadsReviewCount, false),
|
||||
MetadataClearFlags::isGoodreadsReviewCount, true),
|
||||
new FieldDescriptor<>("hardcoverRating",
|
||||
BookMetadata::getHardcoverRating, BookMetadataEntity::getHardcoverRating,
|
||||
BookMetadata::getHardcoverRatingLocked, BookMetadataEntity::getHardcoverRatingLocked,
|
||||
MetadataClearFlags::isHardcoverRating, false),
|
||||
MetadataClearFlags::isHardcoverRating, true),
|
||||
new FieldDescriptor<>("hardcoverReviewCount",
|
||||
BookMetadata::getHardcoverReviewCount, BookMetadataEntity::getHardcoverReviewCount,
|
||||
BookMetadata::getHardcoverReviewCountLocked, BookMetadataEntity::getHardcoverReviewCountLocked,
|
||||
MetadataClearFlags::isHardcoverReviewCount, false),
|
||||
MetadataClearFlags::isHardcoverReviewCount, true),
|
||||
new FieldDescriptor<>("lubimyczytacRating",
|
||||
BookMetadata::getLubimyczytacRating, BookMetadataEntity::getLubimyczytacRating,
|
||||
BookMetadata::getLubimyczytacRatingLocked, BookMetadataEntity::getLubimyczytacRatingLocked,
|
||||
MetadataClearFlags::isLubimyczytacRating, false),
|
||||
MetadataClearFlags::isLubimyczytacRating, true),
|
||||
new FieldDescriptor<>("ranobedbRating",
|
||||
BookMetadata::getRanobedbRating, BookMetadataEntity::getRanobedbRating,
|
||||
BookMetadata::getRanobedbRatingLocked, BookMetadataEntity::getRanobedbRatingLocked,
|
||||
MetadataClearFlags::isRanobedbRating, false),
|
||||
MetadataClearFlags::isRanobedbRating, true),
|
||||
new FieldDescriptor<>("audibleId",
|
||||
BookMetadata::getAudibleId, BookMetadataEntity::getAudibleId,
|
||||
BookMetadata::getAudibleIdLocked, BookMetadataEntity::getAudibleIdLocked,
|
||||
@@ -257,11 +257,11 @@ public class MetadataChangeDetector {
|
||||
new CollectionFieldDescriptor("moods",
|
||||
BookMetadata::getMoods, BookMetadataEntity::getMoods,
|
||||
BookMetadata::getMoodsLocked, BookMetadataEntity::getMoodsLocked,
|
||||
MetadataClearFlags::isMoods, false),
|
||||
MetadataClearFlags::isMoods, true),
|
||||
new CollectionFieldDescriptor("tags",
|
||||
BookMetadata::getTags, BookMetadataEntity::getTags,
|
||||
BookMetadata::getTagsLocked, BookMetadataEntity::getTagsLocked,
|
||||
MetadataClearFlags::isTags, false)
|
||||
MetadataClearFlags::isTags, true)
|
||||
);
|
||||
|
||||
public static boolean isDifferent(BookMetadata newMeta, BookMetadataEntity existingMeta, MetadataClearFlags clear) {
|
||||
|
||||
@@ -291,13 +291,12 @@ public class MetadataChangeDetectorTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void testHasValueChangesForFileWrite_excludesRatingsForFileWrite() {
|
||||
// Change rating fields that should not be considered for file write
|
||||
void testHasValueChangesForFileWrite_includesRatingsForFileWrite() {
|
||||
newMeta.setAmazonRating(4.8);
|
||||
newMeta.setGoodreadsRating(4.4);
|
||||
newMeta.setHardcoverRating(4.3);
|
||||
boolean result = MetadataChangeDetector.hasValueChangesForFileWrite(newMeta, existingMeta, clearFlags);
|
||||
assertFalse(result, "Should return false when only rating fields change for file write");
|
||||
assertTrue(result, "Should return true when rating fields change for file write");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -524,39 +523,45 @@ public class MetadataChangeDetectorTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("hasValueChangesForFileWrite() excludes certain fields from triggering file writes")
|
||||
void testHasValueChangesForFileWrite_excludesPageCount() {
|
||||
@DisplayName("hasValueChangesForFileWrite() includes booklore-namespace fields that are written to files")
|
||||
void testHasValueChangesForFileWrite_includesPageCount() {
|
||||
newMeta.setPageCount(999);
|
||||
boolean result = MetadataChangeDetector.hasValueChangesForFileWrite(newMeta, existingMeta, clearFlags);
|
||||
assertFalse(result, "PageCount should not trigger file write");
|
||||
assertTrue(result, "PageCount should trigger file write");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testHasValueChangesForFileWrite_excludesMoods() {
|
||||
void testHasValueChangesForFileWrite_includesMoods() {
|
||||
newMeta.setMoods(Set.of("New Mood"));
|
||||
boolean result = MetadataChangeDetector.hasValueChangesForFileWrite(newMeta, existingMeta, clearFlags);
|
||||
assertFalse(result, "Moods should not trigger file write");
|
||||
assertTrue(result, "Moods should trigger file write");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testHasValueChangesForFileWrite_excludesTags() {
|
||||
void testHasValueChangesForFileWrite_includesTags() {
|
||||
newMeta.setTags(Set.of("New Tag"));
|
||||
boolean result = MetadataChangeDetector.hasValueChangesForFileWrite(newMeta, existingMeta, clearFlags);
|
||||
assertFalse(result, "Tags should not trigger file write");
|
||||
assertTrue(result, "Tags should trigger file write");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testHasValueChangesForFileWrite_excludesRatingFields() {
|
||||
void testHasValueChangesForFileWrite_includesRatingFields() {
|
||||
newMeta.setAmazonRating(4.9);
|
||||
newMeta.setGoodreadsRating(4.8);
|
||||
newMeta.setHardcoverRating(4.7);
|
||||
newMeta.setAmazonReviewCount(2000);
|
||||
newMeta.setGoodreadsReviewCount(35000);
|
||||
newMeta.setHardcoverReviewCount(800);
|
||||
boolean result = MetadataChangeDetector.hasValueChangesForFileWrite(newMeta, existingMeta, clearFlags);
|
||||
assertTrue(result, "Rating and review count fields should trigger file write");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testHasValueChangesForFileWrite_excludesAudiobookFields() {
|
||||
newMeta.setAudibleRating(4.6);
|
||||
newMeta.setAudibleReviewCount(3000);
|
||||
boolean result = MetadataChangeDetector.hasValueChangesForFileWrite(newMeta, existingMeta, clearFlags);
|
||||
assertFalse(result, "Rating and review count fields should not trigger file write");
|
||||
assertFalse(result, "Audiobook-only fields should not trigger file write");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user