WIP: replacement content for existing PR (#3011)

This commit is contained in:
Max
2026-03-12 19:13:30 -06:00
committed by GitHub
parent 5b901fc21e
commit 5fa511a26c
49 changed files with 202 additions and 53 deletions
@@ -282,7 +282,7 @@ public class KomgaMapper {
case FB2 -> "application/fictionbook2+zip";
case MOBI -> "application/x-mobipocket-ebook";
case AZW3 -> "application/vnd.amazon.ebook";
case AUDIOBOOK -> "audio/mp4";
case AUDIOBOOK -> "audio/*";
};
}
@@ -128,7 +128,7 @@ public class BookFileEntity {
.filter(java.nio.file.Files::isRegularFile)
.filter(p -> {
String name = p.getFileName().toString().toLowerCase();
return name.endsWith(".mp3") || name.endsWith(".m4a") || name.endsWith(".m4b");
return name.endsWith(".mp3") || name.endsWith(".m4a") || name.endsWith(".m4b") || name.endsWith(".opus");
})
.sorted()
.findFirst()
@@ -20,7 +20,8 @@ public enum BookFileExtension {
FB2("fb2", BookFileType.FB2),
M4B("m4b", BookFileType.AUDIOBOOK),
M4A("m4a", BookFileType.AUDIOBOOK),
MP3("mp3", BookFileType.AUDIOBOOK);
MP3("mp3", BookFileType.AUDIOBOOK),
OPUS("opus", BookFileType.AUDIOBOOK);
private final String extension;
private final BookFileType type;
@@ -15,7 +15,7 @@ public enum BookFileType {
FB2(Set.of("fb2")),
MOBI(Set.of("mobi")),
AZW3(Set.of("azw3", "azw")),
AUDIOBOOK(Set.of("m4b", "m4a", "mp3"));
AUDIOBOOK(Set.of("m4b", "m4a", "mp3", "opus"));
private final Set<String> extensions;
@@ -85,6 +85,6 @@ public class FileFingerprint {
private static boolean isAudioFile(String fileName) {
if (fileName == null) return false;
String lower = fileName.toLowerCase();
return lower.endsWith(".mp3") || lower.endsWith(".m4a") || lower.endsWith(".m4b");
return lower.endsWith(".mp3") || lower.endsWith(".m4a") || lower.endsWith(".m4b") || lower.endsWith(".opus");
}
}
@@ -40,7 +40,7 @@ public class MetadataExtractorFactory {
case FB2 -> fb2MetadataExtractor.extractMetadata(file);
case MOBI -> mobiMetadataExtractor.extractMetadata(file);
case AZW3, AZW -> azw3MetadataExtractor.extractMetadata(file);
case M4B, M4A, MP3 -> audiobookMetadataExtractor.extractMetadata(file);
case M4B, M4A, MP3, OPUS -> audiobookMetadataExtractor.extractMetadata(file);
};
}
@@ -52,7 +52,7 @@ public class MetadataExtractorFactory {
case FB2 -> fb2MetadataExtractor.extractCover(file);
case MOBI -> mobiMetadataExtractor.extractCover(file);
case AZW3, AZW -> azw3MetadataExtractor.extractCover(file);
case M4B, M4A, MP3 -> audiobookMetadataExtractor.extractCover(file);
case M4B, M4A, MP3, OPUS -> audiobookMetadataExtractor.extractCover(file);
};
}
@@ -694,6 +694,7 @@ public class OpdsFeedService {
case AUDIOBOOK -> {
String lower = bookFile.getFileName().toLowerCase();
if (lower.endsWith(".mp3")) yield "audio/mpeg";
if (lower.endsWith(".opus")) yield "audio/opus";
yield "audio/mp4";
}
};
@@ -20,7 +20,7 @@ import java.util.stream.Stream;
public class AudioFileUtilityService {
private static final Set<String> AUDIO_EXTENSIONS = Set.of(
".mp3", ".m4a", ".m4b"
".mp3", ".m4a", ".m4b", ".opus"
);
/**
@@ -55,6 +55,8 @@ public class AudioFileUtilityService {
return "audio/mp4";
} else if (fileName.endsWith(".mp3")) {
return "audio/mpeg";
} else if (fileName.endsWith(".opus")) {
return "audio/opus";
}
return "application/octet-stream";
}
@@ -140,11 +140,10 @@ public class AudioMetadataService {
totalDurationMs += trackDurationMs;
if (i == 0) {
long bitrateValue = header.getBitRateAsNumber();
bitrate = bitrateValue > 0 ? (int) bitrateValue : null;
codec = header.getEncodingType();
sampleRate = header.getSampleRateAsNumber();
channels = parseChannels(header.getChannels());
bitrate = safeBitrate(header, trackPath);
codec = safeEncodingType(header, trackPath);
sampleRate = safeSampleRate(header, trackPath);
channels = parseChannels(safeChannels(header, trackPath));
if (tag != null) {
title = getTagValue(tag, FieldKey.ALBUM, FieldKey.TITLE);
author = getTagValue(tag, FieldKey.ALBUM_ARTIST, FieldKey.ARTIST);
@@ -222,13 +221,12 @@ public class AudioMetadataService {
AudioHeader header = audioFile.getAudioHeader();
Tag tag = audioFile.getTag();
long durationMs = (long) (header.getPreciseTrackLength() * 1000);
long bitrateValue = header.getBitRateAsNumber();
long durationMs = safeDurationMs(header, audioPath);
builder.durationMs(durationMs)
.bitrate(bitrateValue > 0 ? (int) bitrateValue : null)
.codec(header.getEncodingType())
.sampleRate(header.getSampleRateAsNumber())
.channels(parseChannels(header.getChannels()));
.bitrate(safeBitrate(header, audioPath))
.codec(safeEncodingType(header, audioPath))
.sampleRate(safeSampleRate(header, audioPath))
.channels(parseChannels(safeChannels(header, audioPath)));
if (tag != null) {
builder.title(getTagValue(tag, FieldKey.TITLE, FieldKey.ALBUM))
@@ -360,4 +358,52 @@ public class AudioMetadataService {
return null;
}
}
private long safeDurationMs(AudioHeader header, Path audioPath) {
try {
return (long) (header.getPreciseTrackLength() * 1000);
} catch (RuntimeException e) {
log.warn("Failed to read track duration from {}", audioPath, e);
return 0L;
}
}
private Integer safeBitrate(AudioHeader header, Path audioPath) {
try {
long bitrateValue = header.getBitRateAsNumber();
return bitrateValue > 0 ? (int) bitrateValue : null;
} catch (RuntimeException e) {
log.debug("Audio header has no bitrate for {}", audioPath, e);
return null;
}
}
private Integer safeSampleRate(AudioHeader header, Path audioPath) {
try {
int sampleRate = header.getSampleRateAsNumber();
return sampleRate > 0 ? sampleRate : null;
} catch (RuntimeException e) {
log.debug("Audio header has no sample rate for {}", audioPath, e);
return null;
}
}
private String safeEncodingType(AudioHeader header, Path audioPath) {
try {
String encodingType = header.getEncodingType();
return encodingType != null && !encodingType.isBlank() ? encodingType : null;
} catch (RuntimeException e) {
log.debug("Audio header has no encoding type for {}", audioPath, e);
return null;
}
}
private String safeChannels(AudioHeader header, Path audioPath) {
try {
return header.getChannels();
} catch (RuntimeException e) {
log.debug("Audio header has no channel info for {}", audioPath, e);
return null;
}
}
}
@@ -155,7 +155,7 @@ public class FileUtils {
public boolean isAudioFile(String fileName) {
if (fileName == null) return false;
String lower = fileName.toLowerCase();
return lower.endsWith(".mp3") || lower.endsWith(".m4a") || lower.endsWith(".m4b");
return lower.endsWith(".mp3") || lower.endsWith(".m4a") || lower.endsWith(".m4b") || lower.endsWith(".opus");
}
/**
@@ -234,10 +234,11 @@ class AudioFileUtilityServiceTest {
void getSupportedExtensions_returnsAllExpectedExtensions() {
var extensions = audioFileUtility.getSupportedExtensions();
assertEquals(3, extensions.size());
assertEquals(4, extensions.size());
assertTrue(extensions.contains(".mp3"));
assertTrue(extensions.contains(".m4a"));
assertTrue(extensions.contains(".m4b"));
assertTrue(extensions.contains(".opus"));
}
@Test
@@ -569,6 +569,89 @@ class AudioMetadataServiceTest {
}
}
@Test
void getMetadata_singleFile_survivesMissingHeaderFields() throws Exception {
AudioFile audioFile = mock(AudioFile.class);
AudioHeader header = mock(AudioHeader.class);
Tag tag = mock(Tag.class);
when(audioFile.getAudioHeader()).thenReturn(header);
when(audioFile.getTag()).thenReturn(tag);
when(header.getPreciseTrackLength()).thenReturn(3600.0);
when(header.getBitRateAsNumber()).thenThrow(new NullPointerException("bitRate"));
when(header.getEncodingType()).thenReturn("Opus");
when(header.getSampleRateAsNumber()).thenThrow(new NullPointerException("sampleRate"));
when(header.getChannels()).thenThrow(new NullPointerException("channels"));
when(tag.getFirst(FieldKey.TITLE)).thenReturn("Test Opus");
try (MockedStatic<AudioFileIO> audioFileIOMock = mockStatic(AudioFileIO.class)) {
audioFileIOMock.when(() -> AudioFileIO.read(audioPath.toFile())).thenReturn(audioFile);
AudiobookInfo info = audioMetadataService.getMetadata(bookFileEntity, audioPath);
assertNotNull(info);
assertEquals(3600000L, info.getDurationMs());
assertNull(info.getBitrate());
assertEquals("Opus", info.getCodec());
assertNull(info.getSampleRate());
assertNull(info.getChannels());
assertEquals("Test Opus", info.getTitle());
}
}
@Test
void getMetadata_folderBased_survivesMissingFirstTrackHeaderFields() throws Exception {
bookFileEntity.setFolderBased(true);
Path folderPath = tempDir.resolve("opus_folder");
Files.createDirectory(folderPath);
Path track1 = folderPath.resolve("parts-01.opus");
Path track2 = folderPath.resolve("parts-02.opus");
Files.createFile(track1);
Files.createFile(track2);
when(audioFileUtility.listAudioFiles(folderPath)).thenReturn(List.of(track1, track2));
when(audioFileUtility.getTrackTitleFromFilename("parts-02.opus")).thenReturn("parts-02");
AudioFile audioFile1 = mock(AudioFile.class);
AudioFile audioFile2 = mock(AudioFile.class);
AudioHeader header1 = mock(AudioHeader.class);
AudioHeader header2 = mock(AudioHeader.class);
Tag tag1 = mock(Tag.class);
when(audioFile1.getAudioHeader()).thenReturn(header1);
when(audioFile1.getTag()).thenReturn(tag1);
when(header1.getPreciseTrackLength()).thenReturn(1800.0);
when(header1.getBitRateAsNumber()).thenThrow(new NullPointerException("bitRate"));
when(header1.getEncodingType()).thenReturn("Opus");
when(header1.getSampleRateAsNumber()).thenThrow(new NullPointerException("sampleRate"));
when(header1.getChannels()).thenThrow(new NullPointerException("channels"));
when(tag1.getFirst(FieldKey.ALBUM)).thenReturn("Test Album");
when(tag1.getFirst(FieldKey.TITLE)).thenReturn("Part 1");
when(audioFile2.getAudioHeader()).thenReturn(header2);
when(audioFile2.getTag()).thenReturn(null);
when(header2.getPreciseTrackLength()).thenReturn(1200.0);
try (MockedStatic<AudioFileIO> audioFileIOMock = mockStatic(AudioFileIO.class)) {
audioFileIOMock.when(() -> AudioFileIO.read(track1.toFile())).thenReturn(audioFile1);
audioFileIOMock.when(() -> AudioFileIO.read(track2.toFile())).thenReturn(audioFile2);
AudiobookInfo info = audioMetadataService.getMetadata(bookFileEntity, folderPath);
assertNotNull(info);
assertTrue(info.isFolderBased());
assertEquals(3000000L, info.getDurationMs());
assertEquals("Test Album", info.getTitle());
assertEquals("Opus", info.getCodec());
assertNull(info.getBitrate());
assertNull(info.getSampleRate());
assertNull(info.getChannels());
assertEquals("Part 1", info.getTracks().get(0).getTitle());
assertEquals("parts-02", info.getTracks().get(1).getTitle());
}
}
@Test
void getMetadata_backfillsChapters_whenDbHasMetadataButNoChapters() throws Exception {
bookFileEntity.setDurationSeconds(3600L);