fix: library update errors on invalid categories (#385)
- Truncate category names to fit within a VARCHAR(255) limit - Skip subjects that are null, empty, too long, or contain newlines/whitespace Signed-off-by: justmiles <1012226+justmiles@users.noreply.github.com> Co-authored-by: justmiles <1012226+justmiles@users.noreply.github.com>
This commit is contained in:
+15
-2
@@ -45,13 +45,15 @@ public class BookCreatorService {
|
|||||||
|
|
||||||
public void addCategoriesToBook(List<String> categories, BookEntity bookEntity) {
|
public void addCategoriesToBook(List<String> categories, BookEntity bookEntity) {
|
||||||
for (String category : categories) {
|
for (String category : categories) {
|
||||||
Optional<CategoryEntity> catOpt = categoryRepository.findByName(category);
|
// Truncate category name to fit in VARCHAR(255)
|
||||||
|
String truncatedCategory = truncate(category, 255);
|
||||||
|
Optional<CategoryEntity> catOpt = categoryRepository.findByName(truncatedCategory);
|
||||||
CategoryEntity categoryEntity;
|
CategoryEntity categoryEntity;
|
||||||
if (catOpt.isPresent()) {
|
if (catOpt.isPresent()) {
|
||||||
categoryEntity = catOpt.get();
|
categoryEntity = catOpt.get();
|
||||||
} else {
|
} else {
|
||||||
categoryEntity = CategoryEntity.builder()
|
categoryEntity = CategoryEntity.builder()
|
||||||
.name(category)
|
.name(truncatedCategory)
|
||||||
.build();
|
.build();
|
||||||
categoryEntity = categoryRepository.save(categoryEntity);
|
categoryEntity = categoryRepository.save(categoryEntity);
|
||||||
}
|
}
|
||||||
@@ -80,6 +82,17 @@ public class BookCreatorService {
|
|||||||
bookEntity.getMetadata().getAuthors().add(authorEntity);
|
bookEntity.getMetadata().getAuthors().add(authorEntity);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Truncates a string to the specified maximum length
|
||||||
|
* @param input The input string
|
||||||
|
* @param maxLength The maximum length
|
||||||
|
* @return The truncated string, or null if input is null
|
||||||
|
*/
|
||||||
|
private String truncate(String input, int maxLength) {
|
||||||
|
if (input == null) return null;
|
||||||
|
return input.length() <= maxLength ? input : input.substring(0, maxLength);
|
||||||
|
}
|
||||||
|
|
||||||
public void saveConnections(BookEntity bookEntity) {
|
public void saveConnections(BookEntity bookEntity) {
|
||||||
if (bookEntity.getMetadata().getAuthors() != null && !bookEntity.getMetadata().getAuthors().isEmpty()) {
|
if (bookEntity.getMetadata().getAuthors() != null && !bookEntity.getMetadata().getAuthors().isEmpty()) {
|
||||||
|
|||||||
+35
-1
@@ -10,6 +10,7 @@ import java.time.Instant;
|
|||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
import java.time.OffsetDateTime;
|
import java.time.OffsetDateTime;
|
||||||
import java.time.format.DateTimeParseException;
|
import java.time.format.DateTimeParseException;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
@@ -227,7 +228,40 @@ public class EpubProcessor implements FileProcessor {
|
|||||||
extractFromOpf(FileUtils.getBookFullPath(bookEntity), bookMetadata);
|
extractFromOpf(FileUtils.getBookFullPath(bookEntity), bookMetadata);
|
||||||
|
|
||||||
bookCreatorService.addAuthorsToBook(getAuthors(book), bookEntity);
|
bookCreatorService.addAuthorsToBook(getAuthors(book), bookEntity);
|
||||||
bookCreatorService.addCategoriesToBook(epubMetadata.getSubjects(), bookEntity);
|
|
||||||
|
// Get subjects and filter out invalid ones
|
||||||
|
List<String> subjects = epubMetadata.getSubjects();
|
||||||
|
List<String> validSubjects = new ArrayList<>();
|
||||||
|
|
||||||
|
if (subjects != null && !subjects.isEmpty()) {
|
||||||
|
for (String subject : subjects) {
|
||||||
|
// Skip null or empty subjects
|
||||||
|
if (subject == null || subject.trim().isEmpty()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Skip subjects that are too long (likely not real categories)
|
||||||
|
// Real categories are typically short, descriptive terms
|
||||||
|
if (subject.length() > 100) {
|
||||||
|
log.warn("Skipping suspiciously long subject in {}: length={}, value='{}'",
|
||||||
|
bookEntity.getFileName(), subject.length(),
|
||||||
|
subject.substring(0, Math.min(50, subject.length())) + "...");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Skip subjects that contain newlines or excessive whitespace (likely descriptions)
|
||||||
|
if (subject.contains("\n") || subject.contains("\r") || subject.contains(" ")) {
|
||||||
|
log.warn("Skipping subject with newlines/whitespace in {}: '{}'",
|
||||||
|
bookEntity.getFileName(),
|
||||||
|
subject.substring(0, Math.min(50, subject.length())) + "...");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
validSubjects.add(subject);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bookCreatorService.addCategoriesToBook(validSubjects, bookEntity);
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("Error loading epub file {}, error: {}", bookEntity.getFileName(), e.getMessage());
|
log.error("Error loading epub file {}, error: {}", bookEntity.getFileName(), e.getMessage());
|
||||||
|
|||||||
Reference in New Issue
Block a user