fix(archive): infer archive type via Magic Numbers instead of filename (#79)
* chore(deps): bump actions/setup-node from 6.2.0 to 6.3.0 (#1) Dependabot couldn't find the original pull request head commit, ea510f40fc1855aa17a782e05b0d30547e84955e. Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * chore(deps): bump docker/setup-buildx-action from 3.12.0 to 4.0.0 (#2) Dependabot couldn't find the original pull request head commit, faed6bf8a712c5fc793fa628157de25d97888131. Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * chore(deps): bump docker/build-push-action from 6.19.2 to 7.0.0 (#3) Dependabot couldn't find the original pull request head commit, f110823720b6dd773df014ba288a126d8837fe28. Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * chore(deps): bump docker/login-action from 3.7.0 to 4.0.0 (#6) Dependabot couldn't find the original pull request head commit, 9a8d7a1daf5afff06bb35e36607c3da9aa730e21. Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * fix(archive): infer archive type via Magic Numbers instead of filename * fix(archive): improve archive type detection and improve logging for cover image retrieval --------- Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
This commit is contained in:
@@ -104,7 +104,7 @@ public class CbxProcessor extends AbstractFileProcessor implements BookFileProce
|
||||
image.flush(); // Release resources after processing
|
||||
}
|
||||
} else {
|
||||
log.warn("Could not find cover image in CBZ file '{}'", bookFile.getFileName());
|
||||
log.warn("Could not find cover image in '{}' archive", bookFile.getFileName());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("Error generating cover for '{}': {}", bookFile.getFileName(), e.getMessage());
|
||||
|
||||
@@ -166,7 +166,8 @@ public class CbxConversionService {
|
||||
throw new IllegalArgumentException("Invalid CBX file: " + cbxFile);
|
||||
}
|
||||
|
||||
if (!isSupportedCbxFormat(cbxFile.getName())) {
|
||||
ArchiveUtils.ArchiveType type = ArchiveUtils.detectArchiveType(cbxFile);
|
||||
if (type == ArchiveUtils.ArchiveType.UNKNOWN) {
|
||||
throw new IllegalArgumentException("Unsupported file format: " + cbxFile.getName() +
|
||||
". Supported formats: CBZ, CBR, CB7");
|
||||
}
|
||||
@@ -694,14 +695,4 @@ public class CbxConversionService {
|
||||
return crc32.getValue();
|
||||
}
|
||||
|
||||
public boolean isSupportedCbxFormat(String fileName) {
|
||||
if (fileName == null) {
|
||||
return false;
|
||||
}
|
||||
String lowerName = fileName.toLowerCase();
|
||||
return lowerName.endsWith(".cbz") ||
|
||||
lowerName.endsWith(".cbr") ||
|
||||
lowerName.endsWith(".cb7");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -673,22 +673,15 @@ public class OpdsFeedService {
|
||||
|
||||
if (hasValidFilePath(bookFile)) {
|
||||
ArchiveUtils.ArchiveType type = ArchiveUtils.detectArchiveType(new File(bookFile.getFilePath()));
|
||||
// We only trust detection if it found something definite (not UNKNOWN)
|
||||
if (type != ArchiveUtils.ArchiveType.UNKNOWN) {
|
||||
yield switch (type) {
|
||||
case RAR -> "application/vnd.comicbook-rar";
|
||||
case ZIP -> "application/vnd.comicbook+zip";
|
||||
case SEVEN_ZIP -> "application/x-7z-compressed";
|
||||
default -> "application/vnd.comicbook+zip"; // Should not happen given the if check
|
||||
default -> "application/vnd.comicbook+zip";
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
String lower = bookFile.getFileName().toLowerCase();
|
||||
if (lower.endsWith(".cbr")) yield "application/vnd.comicbook-rar";
|
||||
if (lower.endsWith(".cbz")) yield "application/vnd.comicbook+zip";
|
||||
if (lower.endsWith(".cb7")) yield "application/x-7z-compressed";
|
||||
if (lower.endsWith(".cbt")) yield "application/x-tar";
|
||||
yield "application/vnd.comicbook+zip";
|
||||
}
|
||||
case AUDIOBOOK -> {
|
||||
|
||||
@@ -14,6 +14,7 @@ import org.booklore.model.entity.BookEntity;
|
||||
import org.booklore.model.entity.BookFileEntity;
|
||||
import org.booklore.model.enums.BookFileType;
|
||||
import org.booklore.repository.BookRepository;
|
||||
import org.booklore.util.ArchiveUtils;
|
||||
import org.booklore.util.FileUtils;
|
||||
import org.booklore.util.UnrarHelper;
|
||||
import org.springframework.stereotype.Service;
|
||||
@@ -38,9 +39,6 @@ import java.util.stream.IntStream;
|
||||
@RequiredArgsConstructor
|
||||
public class CbxReaderService {
|
||||
|
||||
private static final String CBZ_EXTENSION = ".cbz";
|
||||
private static final String CBR_EXTENSION = ".cbr";
|
||||
private static final String CB7_EXTENSION = ".cb7";
|
||||
private static final String[] SUPPORTED_IMAGE_EXTENSIONS = {".jpg", ".jpeg", ".png", ".webp", ".avif", ".heic"};
|
||||
private static final Charset[] ENCODINGS_TO_TRY = {
|
||||
StandardCharsets.UTF_8,
|
||||
@@ -195,19 +193,20 @@ public class CbxReaderService {
|
||||
}
|
||||
|
||||
private CachedArchiveMetadata scanArchiveMetadata(Path cbxPath) throws IOException {
|
||||
String filename = cbxPath.getFileName().toString().toLowerCase();
|
||||
long lastModified = Files.getLastModifiedTime(cbxPath).toMillis();
|
||||
if (filename.endsWith(CBZ_EXTENSION)) {
|
||||
return scanZipMetadata(cbxPath, lastModified);
|
||||
} else if (filename.endsWith(CB7_EXTENSION)) {
|
||||
List<String> entries = getImageEntriesFrom7z(cbxPath);
|
||||
return new CachedArchiveMetadata(entries, lastModified, null, false);
|
||||
} else if (filename.endsWith(CBR_EXTENSION)) {
|
||||
List<String> entries = getImageEntriesFromRar(cbxPath);
|
||||
return new CachedArchiveMetadata(entries, lastModified, null, false);
|
||||
} else {
|
||||
throw new IOException("Unsupported archive format: " + cbxPath.getFileName());
|
||||
}
|
||||
ArchiveUtils.ArchiveType type = ArchiveUtils.detectArchiveType(cbxPath.toFile());
|
||||
return switch (type) {
|
||||
case ZIP -> scanZipMetadata(cbxPath, lastModified);
|
||||
case SEVEN_ZIP -> {
|
||||
List<String> entries = getImageEntriesFrom7z(cbxPath);
|
||||
yield new CachedArchiveMetadata(entries, lastModified, null, false);
|
||||
}
|
||||
case RAR -> {
|
||||
List<String> entries = getImageEntriesFromRar(cbxPath);
|
||||
yield new CachedArchiveMetadata(entries, lastModified, null, false);
|
||||
}
|
||||
default -> throw new IOException("Unsupported archive format: " + cbxPath.getFileName());
|
||||
};
|
||||
}
|
||||
|
||||
private CachedArchiveMetadata scanZipMetadata(Path cbxPath, long lastModified) throws IOException {
|
||||
@@ -316,15 +315,12 @@ public class CbxReaderService {
|
||||
|
||||
|
||||
private void streamEntryFromArchive(Path cbxPath, String entryName, OutputStream outputStream, CachedArchiveMetadata metadata) throws IOException {
|
||||
String filename = cbxPath.getFileName().toString().toLowerCase();
|
||||
if (filename.endsWith(CBZ_EXTENSION)) {
|
||||
streamEntryFromZip(cbxPath, entryName, outputStream, metadata);
|
||||
} else if (filename.endsWith(CB7_EXTENSION)) {
|
||||
streamEntryFrom7z(cbxPath, entryName, outputStream);
|
||||
} else if (filename.endsWith(CBR_EXTENSION)) {
|
||||
streamEntryFromRar(cbxPath, entryName, outputStream);
|
||||
} else {
|
||||
throw new IOException("Unsupported archive format: " + cbxPath.getFileName());
|
||||
ArchiveUtils.ArchiveType type = ArchiveUtils.detectArchiveType(cbxPath.toFile());
|
||||
switch (type) {
|
||||
case ZIP -> streamEntryFromZip(cbxPath, entryName, outputStream, metadata);
|
||||
case SEVEN_ZIP -> streamEntryFrom7z(cbxPath, entryName, outputStream);
|
||||
case RAR -> streamEntryFromRar(cbxPath, entryName, outputStream);
|
||||
default -> throw new IOException("Unsupported archive format: " + cbxPath.getFileName());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user