Fix additional file uploader bugs and move formats to metadata header (#2816)
This commit is contained in:
@@ -20,7 +20,7 @@ public enum ApiError {
|
||||
INVALID_VIEWER_SETTING(HttpStatus.BAD_REQUEST, "Invalid viewer setting for the book"),
|
||||
FILE_READ_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "Error reading files from path: %s"),
|
||||
IMAGE_NOT_FOUND(HttpStatus.NOT_FOUND, "Image not found or not readable"),
|
||||
INVALID_FILE_FORMAT(HttpStatus.BAD_REQUEST, "Invalid file format, only pdf and epub are supported"),
|
||||
INVALID_FILE_FORMAT(HttpStatus.BAD_REQUEST, "Invalid file format: %s"),
|
||||
LIBRARY_NOT_FOUND(HttpStatus.NOT_FOUND, "Library not found with ID: %d"),
|
||||
FILE_TOO_LARGE(HttpStatus.BAD_REQUEST, "File size exceeds the limit: %d MB"),
|
||||
CACHE_TOO_LARGE(HttpStatus.BAD_REQUEST, "Book archive is too large to cache with current settings"),
|
||||
|
||||
@@ -128,9 +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")
|
||||
|| name.endsWith(".flac") || name.endsWith(".ogg") || name.endsWith(".opus")
|
||||
|| name.endsWith(".aac");
|
||||
return name.endsWith(".mp3") || name.endsWith(".m4a") || name.endsWith(".m4b");
|
||||
})
|
||||
.sorted()
|
||||
.findFirst()
|
||||
|
||||
@@ -20,11 +20,7 @@ public enum BookFileExtension {
|
||||
FB2("fb2", BookFileType.FB2),
|
||||
M4B("m4b", BookFileType.AUDIOBOOK),
|
||||
M4A("m4a", BookFileType.AUDIOBOOK),
|
||||
MP3("mp3", BookFileType.AUDIOBOOK),
|
||||
AAC("aac", BookFileType.AUDIOBOOK),
|
||||
FLAC("flac", BookFileType.AUDIOBOOK),
|
||||
OPUS("opus", BookFileType.AUDIOBOOK),
|
||||
OGG("ogg", BookFileType.AUDIOBOOK);
|
||||
MP3("mp3", 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", "aac", "flac", "opus", "ogg"));
|
||||
AUDIOBOOK(Set.of("m4b", "m4a", "mp3"));
|
||||
|
||||
private final Set<String> extensions;
|
||||
|
||||
|
||||
+2
-22
@@ -1,7 +1,6 @@
|
||||
package org.booklore.repository;
|
||||
|
||||
import org.booklore.model.entity.BookFileEntity;
|
||||
import org.booklore.model.enums.BookFileType;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Modifying;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
@@ -18,31 +17,12 @@ public interface BookAdditionalFileRepository extends JpaRepository<BookFileEnti
|
||||
|
||||
List<BookFileEntity> findByBookIdAndIsBookFormat(Long bookId, boolean isBookFormat);
|
||||
|
||||
List<BookFileEntity> findByBookIdAndBookType(Long bookId, BookFileType bookType);
|
||||
|
||||
/**
|
||||
* Finds a {@link BookFileEntity} by its alternative format current hash.
|
||||
* <p>
|
||||
* This method queries against the {@code alt_format_current_hash} virtual column, which is indexed
|
||||
* and only contains values for files where the {@code additional_file_type} is 'ALTERNATIVE_FORMAT'.
|
||||
* This implicitly filters by the file type and provides an efficient lookup.
|
||||
*
|
||||
* @param altFormatCurrentHash The current hash of the file, which is only considered for alternative format files.
|
||||
* @return an {@link Optional} containing the found entity, or an empty {@link Optional} if no match is found.
|
||||
*/
|
||||
Optional<BookFileEntity> findByAltFormatCurrentHash(String altFormatCurrentHash);
|
||||
|
||||
@Query("SELECT bf FROM BookFileEntity bf WHERE bf.book.libraryPath.id = :libraryPathId AND bf.fileSubPath = :fileSubPath AND bf.fileName = :fileName")
|
||||
Optional<BookFileEntity> findByLibraryPath_IdAndFileSubPathAndFileName(@Param("libraryPathId") Long libraryPathId,
|
||||
@Param("fileSubPath") String fileSubPath,
|
||||
@Param("fileName") String fileName);
|
||||
|
||||
List<BookFileEntity> findByIsBookFormat(boolean isBookFormat);
|
||||
|
||||
List<BookFileEntity> findByBookType(BookFileType bookType);
|
||||
|
||||
@Query("SELECT COUNT(bf) FROM BookFileEntity bf WHERE bf.book.id = :bookId AND bf.isBookFormat = :isBookFormat")
|
||||
long countByBookIdAndIsBookFormat(@Param("bookId") Long bookId, @Param("isBookFormat") boolean isBookFormat);
|
||||
@Param("fileSubPath") String fileSubPath,
|
||||
@Param("fileName") String fileName);
|
||||
|
||||
@Query("SELECT bf FROM BookFileEntity bf WHERE bf.book.library.id = :libraryId")
|
||||
List<BookFileEntity> findByLibraryId(@Param("libraryId") Long libraryId);
|
||||
|
||||
@@ -85,8 +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")
|
||||
|| lower.endsWith(".flac") || lower.endsWith(".ogg") || lower.endsWith(".opus")
|
||||
|| lower.endsWith(".aac");
|
||||
return lower.endsWith(".mp3") || lower.endsWith(".m4a") || lower.endsWith(".m4b");
|
||||
}
|
||||
}
|
||||
|
||||
+2
-2
@@ -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, AAC, FLAC, OPUS, OGG -> audiobookMetadataExtractor.extractMetadata(file);
|
||||
case M4B, M4A, MP3 -> 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, AAC, FLAC, OPUS, OGG -> audiobookMetadataExtractor.extractCover(file);
|
||||
case M4B, M4A, MP3 -> audiobookMetadataExtractor.extractCover(file);
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -691,11 +691,7 @@ public class OpdsFeedService {
|
||||
}
|
||||
case AUDIOBOOK -> {
|
||||
String lower = bookFile.getFileName().toLowerCase();
|
||||
if (lower.endsWith(".m4b") || lower.endsWith(".m4a")) yield "audio/mp4";
|
||||
if (lower.endsWith(".mp3")) yield "audio/mpeg";
|
||||
if (lower.endsWith(".flac")) yield "audio/flac";
|
||||
if (lower.endsWith(".ogg") || lower.endsWith(".opus")) yield "audio/ogg";
|
||||
if (lower.endsWith(".aac")) yield "audio/aac";
|
||||
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", ".aac", ".flac", ".ogg", ".opus"
|
||||
".mp3", ".m4a", ".m4b"
|
||||
);
|
||||
|
||||
/**
|
||||
@@ -55,14 +55,6 @@ public class AudioFileUtilityService {
|
||||
return "audio/mp4";
|
||||
} else if (fileName.endsWith(".mp3")) {
|
||||
return "audio/mpeg";
|
||||
} else if (fileName.endsWith(".aac")) {
|
||||
return "audio/aac";
|
||||
} else if (fileName.endsWith(".flac")) {
|
||||
return "audio/flac";
|
||||
} else if (fileName.endsWith(".ogg")) {
|
||||
return "audio/ogg";
|
||||
} else if (fileName.endsWith(".opus")) {
|
||||
return "audio/opus";
|
||||
}
|
||||
return "application/octet-stream";
|
||||
}
|
||||
|
||||
@@ -59,7 +59,7 @@ public class BookFileGroupingUtils {
|
||||
// Known book file extensions - only strip these, not arbitrary dots in folder names
|
||||
private static final Set<String> KNOWN_EXTENSIONS = Set.of(
|
||||
"pdf", "epub", "cbz", "cbr", "cb7", "mobi", "azw3", "azw", "fb2",
|
||||
"m4b", "m4a", "mp3", "aac", "flac", "opus", "ogg"
|
||||
"m4b", "m4a", "mp3"
|
||||
);
|
||||
|
||||
public String extractGroupingKey(String fileName) {
|
||||
|
||||
@@ -125,9 +125,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")
|
||||
|| lower.endsWith(".flac") || lower.endsWith(".ogg") || lower.endsWith(".opus")
|
||||
|| lower.endsWith(".aac");
|
||||
return lower.endsWith(".mp3") || lower.endsWith(".m4a") || lower.endsWith(".m4b");
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+8
-21
@@ -39,7 +39,7 @@ class AudioFileUtilityServiceTest {
|
||||
// Create audio files
|
||||
Files.createFile(tempDir.resolve("track1.mp3"));
|
||||
Files.createFile(tempDir.resolve("track2.m4b"));
|
||||
Files.createFile(tempDir.resolve("track3.flac"));
|
||||
Files.createFile(tempDir.resolve("track3.m4a"));
|
||||
|
||||
// Create non-audio files
|
||||
Files.createFile(tempDir.resolve("readme.txt"));
|
||||
@@ -51,7 +51,7 @@ class AudioFileUtilityServiceTest {
|
||||
assertEquals(3, result.size());
|
||||
assertTrue(result.stream().anyMatch(p -> p.getFileName().toString().equals("track1.mp3")));
|
||||
assertTrue(result.stream().anyMatch(p -> p.getFileName().toString().equals("track2.m4b")));
|
||||
assertTrue(result.stream().anyMatch(p -> p.getFileName().toString().equals("track3.flac")));
|
||||
assertTrue(result.stream().anyMatch(p -> p.getFileName().toString().equals("track3.m4a")));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -112,20 +112,16 @@ class AudioFileUtilityServiceTest {
|
||||
Files.createFile(tempDir.resolve("track.mp3"));
|
||||
Files.createFile(tempDir.resolve("track.m4a"));
|
||||
Files.createFile(tempDir.resolve("track.m4b"));
|
||||
Files.createFile(tempDir.resolve("track.aac"));
|
||||
Files.createFile(tempDir.resolve("track.flac"));
|
||||
Files.createFile(tempDir.resolve("track.ogg"));
|
||||
Files.createFile(tempDir.resolve("track.opus"));
|
||||
|
||||
List<Path> result = audioFileUtility.listAudioFiles(tempDir);
|
||||
|
||||
assertEquals(7, result.size());
|
||||
assertEquals(3, result.size());
|
||||
}
|
||||
|
||||
// ==================== isAudioFile tests ====================
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"track.mp3", "book.m4b", "audio.m4a", "sound.aac", "music.flac", "song.ogg", "voice.opus"})
|
||||
@ValueSource(strings = {"track.mp3", "book.m4b", "audio.m4a"})
|
||||
void isAudioFile_returnsTrueForAudioExtensions(String filename) throws IOException {
|
||||
Path file = tempDir.resolve(filename);
|
||||
Files.createFile(file);
|
||||
@@ -133,7 +129,7 @@ class AudioFileUtilityServiceTest {
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"TRACK.MP3", "Book.M4B", "Audio.M4A", "SOUND.AAC", "Music.FLAC", "Song.OGG", "Voice.OPUS"})
|
||||
@ValueSource(strings = {"TRACK.MP3", "Book.M4B", "Audio.M4A"})
|
||||
void isAudioFile_isCaseInsensitive(String filename) throws IOException {
|
||||
Path file = tempDir.resolve(filename);
|
||||
Files.createFile(file);
|
||||
@@ -161,11 +157,7 @@ class AudioFileUtilityServiceTest {
|
||||
@CsvSource({
|
||||
"track.m4b, audio/mp4",
|
||||
"track.m4a, audio/mp4",
|
||||
"track.mp3, audio/mpeg",
|
||||
"track.aac, audio/aac",
|
||||
"track.flac, audio/flac",
|
||||
"track.ogg, audio/ogg",
|
||||
"track.opus, audio/opus"
|
||||
"track.mp3, audio/mpeg"
|
||||
})
|
||||
void getContentType_returnsCorrectMimeType(String filename, String expectedMimeType) {
|
||||
Path file = tempDir.resolve(filename);
|
||||
@@ -175,8 +167,7 @@ class AudioFileUtilityServiceTest {
|
||||
@ParameterizedTest
|
||||
@CsvSource({
|
||||
"TRACK.M4B, audio/mp4",
|
||||
"TRACK.MP3, audio/mpeg",
|
||||
"Track.Flac, audio/flac"
|
||||
"TRACK.MP3, audio/mpeg"
|
||||
})
|
||||
void getContentType_isCaseInsensitive(String filename, String expectedMimeType) {
|
||||
Path file = tempDir.resolve(filename);
|
||||
@@ -243,14 +234,10 @@ class AudioFileUtilityServiceTest {
|
||||
void getSupportedExtensions_returnsAllExpectedExtensions() {
|
||||
var extensions = audioFileUtility.getSupportedExtensions();
|
||||
|
||||
assertEquals(7, extensions.size());
|
||||
assertEquals(3, extensions.size());
|
||||
assertTrue(extensions.contains(".mp3"));
|
||||
assertTrue(extensions.contains(".m4a"));
|
||||
assertTrue(extensions.contains(".m4b"));
|
||||
assertTrue(extensions.contains(".aac"));
|
||||
assertTrue(extensions.contains(".flac"));
|
||||
assertTrue(extensions.contains(".ogg"));
|
||||
assertTrue(extensions.contains(".opus"));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -134,7 +134,7 @@ class FileUploadServiceTest {
|
||||
.isThrownBy(() -> service.uploadFileBookDrop(file))
|
||||
.satisfies(ex -> {
|
||||
assertThat(ex.getStatus()).isEqualTo(ApiError.INVALID_FILE_FORMAT.getStatus());
|
||||
assertThat(ex.getMessage()).contains("Invalid file format, only pdf and epub are supported");
|
||||
assertThat(ex.getMessage()).contains("Invalid file format");
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user