fix(bookdrop): clean bookdrop metadata before persisting (#462)
* fix(api): clean bookdrop metadata before persisting * test: add tests for behaviors * fix(api): add missing ratings + IDs * fix(api): use `toBuilder()` to reduce risk of dropping fields
This commit is contained in:
+40
-1
@@ -31,6 +31,7 @@ import java.util.concurrent.ThreadLocalRandom;
|
|||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
import static org.booklore.model.entity.BookdropFileEntity.Status.PENDING_REVIEW;
|
import static org.booklore.model.entity.BookdropFileEntity.Status.PENDING_REVIEW;
|
||||||
|
import static org.booklore.util.FileService.truncate;
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
@@ -127,11 +128,49 @@ public class BookdropMetadataService {
|
|||||||
return bookdropFileRepository.findById(id).orElseThrow(() -> new IllegalArgumentException("Bookdrop file not found: " + id));
|
return bookdropFileRepository.findById(id).orElseThrow(() -> new IllegalArgumentException("Bookdrop file not found: " + id));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private BookMetadata cleanInitialMetadata(BookMetadata extracted) {
|
||||||
|
if (extracted == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Created a builder from `extracted` to perform a shallow clone
|
||||||
|
// where we overwrite only truncated fields.
|
||||||
|
|
||||||
|
return extracted.toBuilder()
|
||||||
|
|
||||||
|
// Basic Fields
|
||||||
|
.title(truncate(extracted.getTitle(), 1000))
|
||||||
|
.subtitle(truncate(extracted.getSubtitle(), 1000))
|
||||||
|
.description(truncate(extracted.getDescription(), 5000))
|
||||||
|
.publisher(truncate(extracted.getPublisher(), 1000))
|
||||||
|
.publishedDate(extracted.getPublishedDate())
|
||||||
|
.seriesName(truncate(extracted.getSeriesName(), 1000))
|
||||||
|
.language(truncate(extracted.getLanguage(), 10))
|
||||||
|
|
||||||
|
// ISBN
|
||||||
|
.isbn10(truncate(extracted.getIsbn10(), 10))
|
||||||
|
.isbn13(truncate(extracted.getIsbn13(), 13))
|
||||||
|
|
||||||
|
// External IDs
|
||||||
|
.asin(truncate(extracted.getAsin(), 10))
|
||||||
|
.audibleId(truncate(extracted.getAudibleId(), 10))
|
||||||
|
.goodreadsId(truncate(extracted.getGoodreadsId(), 100))
|
||||||
|
.hardcoverId(truncate(extracted.getHardcoverId(), 100))
|
||||||
|
.hardcoverBookId(truncate(extracted.getHardcoverBookId(), 100))
|
||||||
|
.googleId(truncate(extracted.getGoogleId(), 100))
|
||||||
|
.comicvineId(truncate(extracted.getComicvineId(), 100))
|
||||||
|
.lubimyczytacId(truncate(extracted.getLubimyczytacId(), 100))
|
||||||
|
.ranobedbId(truncate(extracted.getRanobedbId(), 100))
|
||||||
|
.doubanId(truncate(extracted.getDoubanId(), 100))
|
||||||
|
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
private BookMetadata extractInitialMetadata(BookdropFileEntity entity) {
|
private BookMetadata extractInitialMetadata(BookdropFileEntity entity) {
|
||||||
File file = new File(entity.getFilePath());
|
File file = new File(entity.getFilePath());
|
||||||
BookFileExtension fileExt = BookFileExtension.fromFileName(file.getName())
|
BookFileExtension fileExt = BookFileExtension.fromFileName(file.getName())
|
||||||
.orElseThrow(() -> ApiError.INVALID_FILE_FORMAT.createException("Unsupported file extension"));
|
.orElseThrow(() -> ApiError.INVALID_FILE_FORMAT.createException("Unsupported file extension"));
|
||||||
return metadataExtractorFactory.extractMetadata(fileExt, file);
|
return cleanInitialMetadata(metadataExtractorFactory.extractMetadata(fileExt, file));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void extractAndSaveCover(BookdropFileEntity entity) {
|
private void extractAndSaveCover(BookdropFileEntity entity) {
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ import org.booklore.util.FileService;
|
|||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.junit.jupiter.api.extension.ExtendWith;
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
|
import org.mockito.ArgumentCaptor;
|
||||||
import org.mockito.InjectMocks;
|
import org.mockito.InjectMocks;
|
||||||
import org.mockito.Mock;
|
import org.mockito.Mock;
|
||||||
import org.mockito.junit.jupiter.MockitoExtension;
|
import org.mockito.junit.jupiter.MockitoExtension;
|
||||||
@@ -123,7 +124,7 @@ class BookdropMetadataServiceTest {
|
|||||||
|
|
||||||
when(bookdropFileRepository.findById(1L)).thenReturn(Optional.of(sampleFile));
|
when(bookdropFileRepository.findById(1L)).thenReturn(Optional.of(sampleFile));
|
||||||
when(metadataExtractorFactory.extractMetadata(eq(BookFileExtension.EPUB), any(File.class))).thenReturn(metadata);
|
when(metadataExtractorFactory.extractMetadata(eq(BookFileExtension.EPUB), any(File.class))).thenReturn(metadata);
|
||||||
when(objectMapper.writeValueAsString(metadata)).thenReturn("{\"title\":\"No Cover Book\"}");
|
when(objectMapper.writeValueAsString(any())).thenReturn("{\"title\":\"No Cover Book\"}");
|
||||||
when(bookdropFileRepository.save(any())).thenAnswer(invocation -> invocation.getArgument(0));
|
when(bookdropFileRepository.save(any())).thenAnswer(invocation -> invocation.getArgument(0));
|
||||||
|
|
||||||
BookdropFileEntity result = bookdropMetadataService.attachInitialMetadata(1L);
|
BookdropFileEntity result = bookdropMetadataService.attachInitialMetadata(1L);
|
||||||
@@ -132,6 +133,33 @@ class BookdropMetadataServiceTest {
|
|||||||
verify(bookdropFileRepository).save(result);
|
verify(bookdropFileRepository).save(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test()
|
||||||
|
void attachInitialMetadata_shouldTruncateFields() throws Exception {
|
||||||
|
BookMetadata metadata = BookMetadata.builder()
|
||||||
|
.asin("SAMPLEASINTOOLONG")
|
||||||
|
.isbn10("00000000000000000000000000000")
|
||||||
|
.isbn13("00000000000000000000000000000")
|
||||||
|
.language("US EnglishTOOLONG")
|
||||||
|
.build();
|
||||||
|
|
||||||
|
when(bookdropFileRepository.findById(1L)).thenReturn(Optional.of(sampleFile));
|
||||||
|
when(metadataExtractorFactory.extractMetadata(eq(BookFileExtension.EPUB), any(File.class))).thenReturn(metadata);
|
||||||
|
when(objectMapper.writeValueAsString(any())).thenReturn("{\"title\":\"No Cover Book\"}");
|
||||||
|
when(bookdropFileRepository.save(any())).thenAnswer(invocation -> invocation.getArgument(0));
|
||||||
|
|
||||||
|
bookdropMetadataService.attachInitialMetadata(1L);
|
||||||
|
|
||||||
|
ArgumentCaptor<BookMetadata> argument = ArgumentCaptor.forClass(BookMetadata.class);
|
||||||
|
verify(objectMapper).writeValueAsString(argument.capture());
|
||||||
|
|
||||||
|
BookMetadata actual = argument.getValue();
|
||||||
|
|
||||||
|
assertThat(actual.getAsin()).isEqualTo("SAMPLEASIN");
|
||||||
|
assertThat(actual.getIsbn10()).isEqualTo("0000000000");
|
||||||
|
assertThat(actual.getIsbn13()).isEqualTo("0000000000000");
|
||||||
|
assertThat(actual.getLanguage()).isEqualTo("US English");
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void extractInitialMetadata_shouldThrowForUnsupportedFileExtension() {
|
void extractInitialMetadata_shouldThrowForUnsupportedFileExtension() {
|
||||||
sampleFile.setFileName("book.txt");
|
sampleFile.setFileName("book.txt");
|
||||||
|
|||||||
Reference in New Issue
Block a user