fix(epub): improve date parsing to support year-only formats and add tests (#1735)
Signed-off-by: Balázs Szücs <bszucs1209@gmail.com>
This commit is contained in:
+19
-3
@@ -27,11 +27,14 @@ import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
public class EpubMetadataExtractor implements FileMetadataExtractor {
|
||||
|
||||
private static final Pattern YEAR_ONLY_PATTERN = Pattern.compile("^\\d{4}$");
|
||||
|
||||
@Override
|
||||
public byte[] extractCover(File epubFile) {
|
||||
try (FileInputStream fis = new FileInputStream(epubFile)) {
|
||||
@@ -263,6 +266,16 @@ public class EpubMetadataExtractor implements FileMetadataExtractor {
|
||||
private LocalDate parseDate(String value) {
|
||||
if (StringUtils.isBlank(value)) return null;
|
||||
|
||||
value = value.trim();
|
||||
|
||||
// Check for year-only format first (e.g., "2024") - common in EPUB metadata
|
||||
if (YEAR_ONLY_PATTERN.matcher(value).matches()) {
|
||||
int year = Integer.parseInt(value);
|
||||
if (year >= 1 && year <= 9999) {
|
||||
return LocalDate.of(year, 1, 1);
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
return LocalDate.parse(value);
|
||||
} catch (Exception ignored) {
|
||||
@@ -273,9 +286,12 @@ public class EpubMetadataExtractor implements FileMetadataExtractor {
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
|
||||
try {
|
||||
return LocalDate.parse(value.substring(0, 10));
|
||||
} catch (Exception ignored) {
|
||||
// Try parsing first 10 characters for ISO date format with extra content
|
||||
if (value.length() >= 10) {
|
||||
try {
|
||||
return LocalDate.parse(value.substring(0, 10));
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
}
|
||||
|
||||
log.warn("Failed to parse date from string: {}", value);
|
||||
|
||||
Reference in New Issue
Block a user