fix(api): use CBX Extractor in the CBX processor (#409)
* fix(api): use CBX Extractor in the CBX processor instead of implementing a separate cover detection algorithm in CBX processor, this uses the CBXMetadataExtractor to fetch the cover image. this means we now will take into account expected file names, `ComicInfo.xml` definitions, and fall back to correctly sorted files * style(api): drop unused imports * fix(api): drop placeholder for cbx this brings the CBX metadata extractor in line with the other metadata extractors that return null when no cover can be found * fix: handle no cover
This commit is contained in:
@@ -11,7 +11,6 @@ import org.booklore.model.entity.ComicMetadataEntity;
|
||||
import org.booklore.model.enums.BookFileType;
|
||||
import org.booklore.repository.BookAdditionalFileRepository;
|
||||
import org.booklore.repository.BookRepository;
|
||||
import org.booklore.service.ArchiveService;
|
||||
import org.booklore.service.book.BookCreatorService;
|
||||
import org.booklore.service.metadata.MetadataMatchService;
|
||||
import org.booklore.service.metadata.extractor.CbxMetadataExtractor;
|
||||
@@ -25,7 +24,6 @@ import org.springframework.stereotype.Service;
|
||||
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.nio.file.Path;
|
||||
import java.util.*;
|
||||
@@ -40,9 +38,7 @@ import static org.booklore.util.FileService.truncate;
|
||||
public class CbxProcessor extends AbstractFileProcessor implements BookFileProcessor {
|
||||
|
||||
private static final Pattern UNDERSCORE_HYPHEN_PATTERN = Pattern.compile("[_\\-]");
|
||||
private static final Pattern IMAGE_EXTENSION_PATTERN = Pattern.compile(".*\\.(jpg|jpeg|png|webp)");
|
||||
private final CbxMetadataExtractor cbxMetadataExtractor;
|
||||
private final ArchiveService archiveService;
|
||||
|
||||
public CbxProcessor(BookRepository bookRepository,
|
||||
BookAdditionalFileRepository bookAdditionalFileRepository,
|
||||
@@ -51,11 +47,9 @@ public class CbxProcessor extends AbstractFileProcessor implements BookFileProce
|
||||
FileService fileService,
|
||||
MetadataMatchService metadataMatchService,
|
||||
SidecarMetadataWriter sidecarMetadataWriter,
|
||||
CbxMetadataExtractor cbxMetadataExtractor,
|
||||
ArchiveService archiveService) {
|
||||
CbxMetadataExtractor cbxMetadataExtractor) {
|
||||
super(bookRepository, bookAdditionalFileRepository, bookCreatorService, bookMapper, fileService, metadataMatchService, sidecarMetadataWriter);
|
||||
this.cbxMetadataExtractor = cbxMetadataExtractor;
|
||||
this.archiveService = archiveService;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -115,27 +109,16 @@ public class CbxProcessor extends AbstractFileProcessor implements BookFileProce
|
||||
}
|
||||
|
||||
private Optional<BufferedImage> extractImagesFromArchive(Path path) {
|
||||
List<String> archiveEntries;
|
||||
try{
|
||||
byte[] coverBytes = cbxMetadataExtractor.extractCover(path);
|
||||
|
||||
try {
|
||||
archiveEntries = archiveService.streamEntryNames(path)
|
||||
.filter(name -> IMAGE_EXTENSION_PATTERN.matcher(name.toLowerCase()).matches())
|
||||
.sorted()
|
||||
.toList();
|
||||
} catch (Exception e) {
|
||||
log.warn("Error reading archive {}: {}", path.getFileName(), e.getMessage());
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
for (String entryName : archiveEntries) {
|
||||
try (
|
||||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
||||
) {
|
||||
archiveService.transferEntryTo(path, entryName, outputStream);
|
||||
return Optional.ofNullable(FileService.readImage(new ByteArrayInputStream(outputStream.toByteArray())));
|
||||
} catch (Exception e) {
|
||||
log.warn("Error reading archive {} entry {}: {}", path.getFileName(), entryName, e.getMessage());
|
||||
if (coverBytes == null) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
return Optional.ofNullable(FileService.readImage(new ByteArrayInputStream(coverBytes)));
|
||||
} catch (Exception e) {
|
||||
log.warn("Error reading archive cover {}: {}", path.getFileName(), e.getMessage());
|
||||
}
|
||||
|
||||
return Optional.empty();
|
||||
|
||||
+1
-32
@@ -511,7 +511,7 @@ public class CbxMetadataExtractor implements FileMetadataExtractor {
|
||||
.filter(Objects::nonNull)
|
||||
.filter(this::canDecode)
|
||||
.findFirst()
|
||||
.orElseGet(() -> generatePlaceholderCover(250, 350));
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
private boolean canDecode(byte[] bytes) {
|
||||
@@ -616,37 +616,6 @@ public class CbxMetadataExtractor implements FileMetadataExtractor {
|
||||
return true;
|
||||
}
|
||||
|
||||
private byte[] generatePlaceholderCover(int width, int height) {
|
||||
BufferedImage image = new BufferedImage(
|
||||
width,
|
||||
height,
|
||||
BufferedImage.TYPE_INT_RGB
|
||||
);
|
||||
Graphics2D g = image.createGraphics();
|
||||
|
||||
g.setColor(Color.LIGHT_GRAY);
|
||||
g.fillRect(0, 0, width, height);
|
||||
|
||||
g.setColor(Color.DARK_GRAY);
|
||||
g.setFont(new Font("SansSerif", Font.BOLD, width / 10));
|
||||
FontMetrics fm = g.getFontMetrics();
|
||||
String text = "Preview Unavailable";
|
||||
|
||||
int textWidth = fm.stringWidth(text);
|
||||
int textHeight = fm.getAscent();
|
||||
g.drawString(text, (width - textWidth) / 2, (height + textHeight) / 2);
|
||||
|
||||
g.dispose();
|
||||
|
||||
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
|
||||
ImageIO.write(image, "jpg", baos);
|
||||
return baos.toByteArray();
|
||||
} catch (IOException e) {
|
||||
log.warn("Failed to generate placeholder image", e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private String findComicInfoEntry(Path cbxPath) {
|
||||
try {
|
||||
return archiveService.streamEntryNames(cbxPath)
|
||||
|
||||
+4
-9
@@ -920,19 +920,14 @@ class CbxMetadataExtractorTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void returnPlaceholderForEmptyCbz() throws IOException {
|
||||
void returnNullForEmptyCbz() throws IOException {
|
||||
Path cbzPath = mockArchiveContents(Map.of(
|
||||
"readme.txt", "no images here".getBytes()
|
||||
));
|
||||
|
||||
byte[] cover = extractor.extractCover(cbzPath);
|
||||
|
||||
assertThat(cover).isNotNull();
|
||||
// Placeholder is a generated image
|
||||
BufferedImage img = ImageIO.read(new ByteArrayInputStream(cover));
|
||||
assertThat(img).isNotNull();
|
||||
assertThat(img.getWidth()).isEqualTo(250);
|
||||
assertThat(img.getHeight()).isEqualTo(350);
|
||||
assertThat(cover).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -1040,7 +1035,7 @@ class CbxMetadataExtractorTest {
|
||||
|
||||
byte[] cover = extractor.extractCover(path);
|
||||
|
||||
assertThat(cover).isNotNull();
|
||||
assertThat(cover).isNull();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1197,7 +1192,7 @@ class CbxMetadataExtractorTest {
|
||||
|
||||
byte[] cover = extractor.extractCover(path);
|
||||
|
||||
assertThat(cover).isNotNull();
|
||||
assertThat(cover).isNull();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user