Audiobook enhancement (#2611)
* Audiobook enhancement * Fix choppy virtual scroller with mixed audiobook/ebook cards by using uniform heights * Fix audiobook cover regeneration for combined books and improve error messages * Fix cover extraction when attaching audiobook files during rescan * Set audiobookCoverUpdatedOn for cache busting when generating cover * Add support for regenerating covers from specific book files * Fix Continue Reading/Listening scrollers to filter by actual progress instead of primary file type * Show ebook cover and open ebook reader in Continue Reading scroller for combined books * Prevent read status from being downgraded when saving audiobook progress * Fix BookCoverServiceTest to match updated regenerateCover method signature --------- Co-authored-by: acx10 <acx10@users.noreply.github.com>
This commit is contained in:
+16
-1
@@ -15,6 +15,9 @@ import org.booklore.service.file.FileFingerprint;
|
||||
import org.booklore.service.fileprocessor.BookFileProcessor;
|
||||
import org.booklore.service.fileprocessor.BookFileProcessorRegistry;
|
||||
import org.booklore.service.kobo.KoboAutoShelfService;
|
||||
import org.booklore.service.metadata.extractor.AudiobookMetadataExtractor;
|
||||
import org.booklore.service.metadata.extractor.MetadataExtractorFactory;
|
||||
import org.booklore.util.FileService;
|
||||
import org.booklore.util.FileUtils;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
@@ -47,6 +50,15 @@ class FileAsBookProcessorTest {
|
||||
@Mock
|
||||
private BookAdditionalFileRepository bookAdditionalFileRepository;
|
||||
|
||||
@Mock
|
||||
private FileService fileService;
|
||||
|
||||
@Mock
|
||||
private MetadataExtractorFactory metadataExtractorFactory;
|
||||
|
||||
@Mock
|
||||
private AudiobookMetadataExtractor audiobookMetadataExtractor;
|
||||
|
||||
@Mock
|
||||
private BookFileProcessor bookFileProcessor;
|
||||
|
||||
@@ -64,7 +76,10 @@ class FileAsBookProcessorTest {
|
||||
processorRegistry,
|
||||
koboAutoShelfService,
|
||||
bookRepository,
|
||||
bookAdditionalFileRepository
|
||||
bookAdditionalFileRepository,
|
||||
fileService,
|
||||
metadataExtractorFactory,
|
||||
audiobookMetadataExtractor
|
||||
);
|
||||
fileFingerprintMock = mockStatic(FileFingerprint.class);
|
||||
fileFingerprintMock.when(() -> FileFingerprint.generateHash(any(Path.class))).thenReturn("testhash");
|
||||
|
||||
@@ -188,24 +188,22 @@ class BookCoverServiceTest {
|
||||
@Test
|
||||
void regenerateCover_success() {
|
||||
BookEntity book = mockBookEntity(7L, false);
|
||||
when(bookRepository.findById(7L)).thenReturn(Optional.of(book));
|
||||
when(bookRepository.findAllWithMetadataByIds(any())).thenReturn(List.of(book));
|
||||
when(bookRepository.findByIdWithBookFiles(7L)).thenReturn(Optional.of(book));
|
||||
BookFileProcessor processor = mock(BookFileProcessor.class);
|
||||
when(processorRegistry.getProcessorOrThrow(any())).thenReturn(processor);
|
||||
when(processor.generateCover(any())).thenReturn(true);
|
||||
when(processor.generateCover(any(BookEntity.class), any(BookFileEntity.class))).thenReturn(true);
|
||||
when(bookRepository.save(any())).thenReturn(book);
|
||||
|
||||
bookCoverService.regenerateCover(7L);
|
||||
|
||||
verify(processor).generateCover(book);
|
||||
verify(processor).generateCover(eq(book), any(BookFileEntity.class));
|
||||
verify(bookRepository).save(book);
|
||||
}
|
||||
|
||||
@Test
|
||||
void regenerateCover_coverLocked_throws() {
|
||||
BookEntity book = mockBookEntity(8L, true);
|
||||
when(bookRepository.findById(8L)).thenReturn(Optional.of(book));
|
||||
when(bookRepository.findAllWithMetadataByIds(any())).thenReturn(List.of(book));
|
||||
when(bookRepository.findByIdWithBookFiles(8L)).thenReturn(Optional.of(book));
|
||||
assertThatThrownBy(() -> bookCoverService.regenerateCover(8L))
|
||||
.isInstanceOf(ApiError.METADATA_LOCKED.createException().getClass());
|
||||
}
|
||||
|
||||
+39
-35
@@ -60,8 +60,6 @@ class AudioMetadataServiceTest {
|
||||
Files.createFile(audioPath);
|
||||
}
|
||||
|
||||
// ==================== getMetadata tests for single file ====================
|
||||
|
||||
@Test
|
||||
void getMetadata_singleFile_extractsBasicInfo() throws Exception {
|
||||
AudioFile audioFile = mock(AudioFile.class);
|
||||
@@ -70,7 +68,7 @@ class AudioMetadataServiceTest {
|
||||
|
||||
when(audioFile.getAudioHeader()).thenReturn(header);
|
||||
when(audioFile.getTag()).thenReturn(tag);
|
||||
when(header.getPreciseTrackLength()).thenReturn(3600.0); // 1 hour
|
||||
when(header.getPreciseTrackLength()).thenReturn(3600.0);
|
||||
when(header.getBitRateAsNumber()).thenReturn(128L);
|
||||
when(header.getEncodingType()).thenReturn("AAC");
|
||||
when(header.getSampleRateAsNumber()).thenReturn(44100);
|
||||
@@ -89,11 +87,11 @@ class AudioMetadataServiceTest {
|
||||
assertEquals(1L, info.getBookId());
|
||||
assertEquals(10L, info.getBookFileId());
|
||||
assertFalse(info.isFolderBased());
|
||||
assertEquals(3600000L, info.getDurationMs()); // 1 hour in ms
|
||||
assertEquals(3600000L, info.getDurationMs());
|
||||
assertEquals(128, info.getBitrate());
|
||||
assertEquals("AAC", info.getCodec());
|
||||
assertEquals(44100, info.getSampleRate());
|
||||
assertEquals(2, info.getChannels()); // Stereo = 2
|
||||
assertEquals(2, info.getChannels());
|
||||
assertEquals("Test Audiobook", info.getTitle());
|
||||
assertEquals("Test Author", info.getAuthor());
|
||||
assertEquals("Test Narrator", info.getNarrator());
|
||||
@@ -120,7 +118,7 @@ class AudioMetadataServiceTest {
|
||||
|
||||
assertNotNull(info);
|
||||
assertEquals(1800000L, info.getDurationMs());
|
||||
assertEquals(1, info.getChannels()); // Mono = 1
|
||||
assertEquals(1, info.getChannels());
|
||||
assertNull(info.getTitle());
|
||||
assertNull(info.getAuthor());
|
||||
assertNull(info.getNarrator());
|
||||
@@ -134,7 +132,7 @@ class AudioMetadataServiceTest {
|
||||
|
||||
when(audioFile.getAudioHeader()).thenReturn(header);
|
||||
when(audioFile.getTag()).thenReturn(null);
|
||||
when(header.getPreciseTrackLength()).thenReturn(7200.0); // 2 hours
|
||||
when(header.getPreciseTrackLength()).thenReturn(7200.0);
|
||||
|
||||
try (MockedStatic<AudioFileIO> audioFileIOMock = mockStatic(AudioFileIO.class)) {
|
||||
audioFileIOMock.when(() -> AudioFileIO.read(audioPath.toFile())).thenReturn(audioFile);
|
||||
@@ -153,30 +151,44 @@ class AudioMetadataServiceTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void getMetadata_cachesResult() throws Exception {
|
||||
AudioFile audioFile = mock(AudioFile.class);
|
||||
AudioHeader header = mock(AudioHeader.class);
|
||||
void getMetadata_usesDbMetadataWhenAvailable() throws Exception {
|
||||
bookFileEntity.setDurationSeconds(3600L);
|
||||
bookFileEntity.setBitrate(192);
|
||||
bookFileEntity.setSampleRate(48000);
|
||||
bookFileEntity.setChannels(2);
|
||||
bookFileEntity.setCodec("MP3");
|
||||
bookFileEntity.setChapterCount(5);
|
||||
bookFileEntity.setChapters(List.of(
|
||||
BookFileEntity.AudioFileChapter.builder()
|
||||
.index(0)
|
||||
.title("Chapter 1")
|
||||
.startTimeMs(0L)
|
||||
.endTimeMs(1000000L)
|
||||
.durationMs(1000000L)
|
||||
.build()
|
||||
));
|
||||
|
||||
when(audioFile.getAudioHeader()).thenReturn(header);
|
||||
when(audioFile.getTag()).thenReturn(null);
|
||||
when(header.getPreciseTrackLength()).thenReturn(100.0);
|
||||
var bookMetadata = new org.booklore.model.entity.BookMetadataEntity();
|
||||
bookMetadata.setTitle("DB Book Title");
|
||||
bookMetadata.setNarrator("DB Narrator");
|
||||
bookEntity.setMetadata(bookMetadata);
|
||||
|
||||
try (MockedStatic<AudioFileIO> audioFileIOMock = mockStatic(AudioFileIO.class)) {
|
||||
audioFileIOMock.when(() -> AudioFileIO.read(audioPath.toFile())).thenReturn(audioFile);
|
||||
AudiobookInfo info = audioMetadataService.getMetadata(bookFileEntity, audioPath);
|
||||
|
||||
// First call
|
||||
AudiobookInfo info1 = audioMetadataService.getMetadata(bookFileEntity, audioPath);
|
||||
// Second call should use cache
|
||||
AudiobookInfo info2 = audioMetadataService.getMetadata(bookFileEntity, audioPath);
|
||||
assertNotNull(info);
|
||||
assertEquals("DB Narrator", info.getNarrator());
|
||||
assertEquals(3600000L, info.getDurationMs());
|
||||
assertEquals(192, info.getBitrate());
|
||||
assertEquals("MP3", info.getCodec());
|
||||
assertEquals("DB Book Title", info.getTitle());
|
||||
assertEquals(1, info.getChapters().size());
|
||||
assertEquals("Chapter 1", info.getChapters().get(0).getTitle());
|
||||
|
||||
assertSame(info1, info2);
|
||||
// AudioFileIO.read should only be called once
|
||||
audioFileIOMock.verify(() -> AudioFileIO.read(audioPath.toFile()), times(1));
|
||||
audioFileIOMock.verify(() -> AudioFileIO.read(any()), never());
|
||||
}
|
||||
}
|
||||
|
||||
// ==================== getMetadata tests for folder-based ====================
|
||||
|
||||
@Test
|
||||
void getMetadata_folderBased_extractsTracksInfo() throws Exception {
|
||||
bookFileEntity.setFolderBased(true);
|
||||
@@ -201,7 +213,7 @@ class AudioMetadataServiceTest {
|
||||
|
||||
when(audioFile1.getAudioHeader()).thenReturn(header1);
|
||||
when(audioFile1.getTag()).thenReturn(tag1);
|
||||
when(header1.getPreciseTrackLength()).thenReturn(1800.0); // 30 min
|
||||
when(header1.getPreciseTrackLength()).thenReturn(1800.0);
|
||||
when(header1.getBitRateAsNumber()).thenReturn(192L);
|
||||
when(header1.getEncodingType()).thenReturn("MP3");
|
||||
when(header1.getSampleRateAsNumber()).thenReturn(44100);
|
||||
@@ -212,7 +224,7 @@ class AudioMetadataServiceTest {
|
||||
|
||||
when(audioFile2.getAudioHeader()).thenReturn(header2);
|
||||
when(audioFile2.getTag()).thenReturn(null);
|
||||
when(header2.getPreciseTrackLength()).thenReturn(2400.0); // 40 min
|
||||
when(header2.getPreciseTrackLength()).thenReturn(2400.0);
|
||||
|
||||
try (MockedStatic<AudioFileIO> audioFileIOMock = mockStatic(AudioFileIO.class)) {
|
||||
audioFileIOMock.when(() -> AudioFileIO.read(track1.toFile())).thenReturn(audioFile1);
|
||||
@@ -222,7 +234,7 @@ class AudioMetadataServiceTest {
|
||||
|
||||
assertNotNull(info);
|
||||
assertTrue(info.isFolderBased());
|
||||
assertEquals(4200000L, info.getDurationMs()); // 30 + 40 min = 70 min
|
||||
assertEquals(4200000L, info.getDurationMs());
|
||||
assertEquals("Test Album", info.getTitle());
|
||||
assertEquals("Test Author", info.getAuthor());
|
||||
|
||||
@@ -274,7 +286,7 @@ class AudioMetadataServiceTest {
|
||||
when(audioFile.getAudioHeader()).thenReturn(header);
|
||||
when(audioFile.getTag()).thenReturn(tag);
|
||||
when(header.getPreciseTrackLength()).thenReturn(600.0);
|
||||
when(tag.getFirst(FieldKey.TITLE)).thenReturn(""); // Empty title
|
||||
when(tag.getFirst(FieldKey.TITLE)).thenReturn("");
|
||||
|
||||
try (MockedStatic<AudioFileIO> audioFileIOMock = mockStatic(AudioFileIO.class)) {
|
||||
audioFileIOMock.when(() -> AudioFileIO.read(track1.toFile())).thenReturn(audioFile);
|
||||
@@ -285,8 +297,6 @@ class AudioMetadataServiceTest {
|
||||
}
|
||||
}
|
||||
|
||||
// ==================== getEmbeddedCoverArt tests ====================
|
||||
|
||||
@Test
|
||||
void getEmbeddedCoverArt_returnsArtworkData() throws Exception {
|
||||
byte[] expectedData = new byte[]{(byte) 0xFF, (byte) 0xD8, 0x00, 0x01};
|
||||
@@ -350,8 +360,6 @@ class AudioMetadataServiceTest {
|
||||
}
|
||||
}
|
||||
|
||||
// ==================== getCoverArtMimeType tests ====================
|
||||
|
||||
@Test
|
||||
void getCoverArtMimeType_returnsMimeTypeFromArtwork() throws Exception {
|
||||
AudioFile audioFile = mock(AudioFile.class);
|
||||
@@ -442,8 +450,6 @@ class AudioMetadataServiceTest {
|
||||
}
|
||||
}
|
||||
|
||||
// ==================== Channel parsing tests ====================
|
||||
|
||||
@Test
|
||||
void getMetadata_parsesStereoChannels() throws Exception {
|
||||
AudioFile audioFile = mock(AudioFile.class);
|
||||
@@ -516,8 +522,6 @@ class AudioMetadataServiceTest {
|
||||
}
|
||||
}
|
||||
|
||||
// ==================== Bitrate handling ====================
|
||||
|
||||
@Test
|
||||
void getMetadata_handlesZeroBitrate() throws Exception {
|
||||
AudioFile audioFile = mock(AudioFile.class);
|
||||
|
||||
@@ -4,7 +4,6 @@ import org.booklore.config.AppProperties;
|
||||
import org.booklore.model.dto.settings.AppSettings;
|
||||
import org.booklore.model.dto.settings.CoverCroppingSettings;
|
||||
import org.booklore.model.entity.BookMetadataEntity;
|
||||
import org.booklore.repository.BookMetadataRepository;
|
||||
import org.booklore.service.appsettings.AppSettingService;
|
||||
import org.junit.jupiter.api.*;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
@@ -62,7 +61,7 @@ class FileServiceTest {
|
||||
.build();
|
||||
lenient().when(appSettingService.getAppSettings()).thenReturn(appSettings);
|
||||
|
||||
fileService = new FileService(appProperties, mock(RestTemplate.class), appSettingService, mock(BookMetadataRepository.class));
|
||||
fileService = new FileService(appProperties, mock(RestTemplate.class), appSettingService);
|
||||
}
|
||||
|
||||
@Nested
|
||||
@@ -1177,7 +1176,7 @@ class FileServiceTest {
|
||||
.build();
|
||||
lenient().when(appSettingServiceForNetwork.getAppSettings()).thenReturn(appSettings);
|
||||
|
||||
fileService = new FileService(appProperties, restTemplate, appSettingServiceForNetwork, mock(BookMetadataRepository.class));
|
||||
fileService = new FileService(appProperties, restTemplate, appSettingServiceForNetwork);
|
||||
}
|
||||
|
||||
@Nested
|
||||
@@ -1194,7 +1193,7 @@ class FileServiceTest {
|
||||
|
||||
RestTemplate mockRestTemplate = mock(RestTemplate.class);
|
||||
AppSettingService mockAppSettingService = mock(AppSettingService.class);
|
||||
FileService testFileService = new FileService(appProperties, mockRestTemplate, mockAppSettingService, mock(BookMetadataRepository.class));
|
||||
FileService testFileService = new FileService(appProperties, mockRestTemplate, mockAppSettingService);
|
||||
|
||||
ResponseEntity<byte[]> responseEntity = ResponseEntity.ok(imageBytes);
|
||||
when(mockRestTemplate.exchange(
|
||||
|
||||
Reference in New Issue
Block a user