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.model.enums.BookFileType;
|
||||||
import org.booklore.repository.BookAdditionalFileRepository;
|
import org.booklore.repository.BookAdditionalFileRepository;
|
||||||
import org.booklore.repository.BookRepository;
|
import org.booklore.repository.BookRepository;
|
||||||
import org.booklore.service.ArchiveService;
|
|
||||||
import org.booklore.service.book.BookCreatorService;
|
import org.booklore.service.book.BookCreatorService;
|
||||||
import org.booklore.service.metadata.MetadataMatchService;
|
import org.booklore.service.metadata.MetadataMatchService;
|
||||||
import org.booklore.service.metadata.extractor.CbxMetadataExtractor;
|
import org.booklore.service.metadata.extractor.CbxMetadataExtractor;
|
||||||
@@ -25,7 +24,6 @@ import org.springframework.stereotype.Service;
|
|||||||
|
|
||||||
import java.awt.image.BufferedImage;
|
import java.awt.image.BufferedImage;
|
||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
import java.io.ByteArrayOutputStream;
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
@@ -40,9 +38,7 @@ import static org.booklore.util.FileService.truncate;
|
|||||||
public class CbxProcessor extends AbstractFileProcessor implements BookFileProcessor {
|
public class CbxProcessor extends AbstractFileProcessor implements BookFileProcessor {
|
||||||
|
|
||||||
private static final Pattern UNDERSCORE_HYPHEN_PATTERN = Pattern.compile("[_\\-]");
|
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 CbxMetadataExtractor cbxMetadataExtractor;
|
||||||
private final ArchiveService archiveService;
|
|
||||||
|
|
||||||
public CbxProcessor(BookRepository bookRepository,
|
public CbxProcessor(BookRepository bookRepository,
|
||||||
BookAdditionalFileRepository bookAdditionalFileRepository,
|
BookAdditionalFileRepository bookAdditionalFileRepository,
|
||||||
@@ -51,11 +47,9 @@ public class CbxProcessor extends AbstractFileProcessor implements BookFileProce
|
|||||||
FileService fileService,
|
FileService fileService,
|
||||||
MetadataMatchService metadataMatchService,
|
MetadataMatchService metadataMatchService,
|
||||||
SidecarMetadataWriter sidecarMetadataWriter,
|
SidecarMetadataWriter sidecarMetadataWriter,
|
||||||
CbxMetadataExtractor cbxMetadataExtractor,
|
CbxMetadataExtractor cbxMetadataExtractor) {
|
||||||
ArchiveService archiveService) {
|
|
||||||
super(bookRepository, bookAdditionalFileRepository, bookCreatorService, bookMapper, fileService, metadataMatchService, sidecarMetadataWriter);
|
super(bookRepository, bookAdditionalFileRepository, bookCreatorService, bookMapper, fileService, metadataMatchService, sidecarMetadataWriter);
|
||||||
this.cbxMetadataExtractor = cbxMetadataExtractor;
|
this.cbxMetadataExtractor = cbxMetadataExtractor;
|
||||||
this.archiveService = archiveService;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -115,27 +109,16 @@ public class CbxProcessor extends AbstractFileProcessor implements BookFileProce
|
|||||||
}
|
}
|
||||||
|
|
||||||
private Optional<BufferedImage> extractImagesFromArchive(Path path) {
|
private Optional<BufferedImage> extractImagesFromArchive(Path path) {
|
||||||
List<String> archiveEntries;
|
try{
|
||||||
|
byte[] coverBytes = cbxMetadataExtractor.extractCover(path);
|
||||||
|
|
||||||
try {
|
if (coverBytes == null) {
|
||||||
archiveEntries = archiveService.streamEntryNames(path)
|
return Optional.empty();
|
||||||
.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());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return Optional.ofNullable(FileService.readImage(new ByteArrayInputStream(coverBytes)));
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.warn("Error reading archive cover {}: {}", path.getFileName(), e.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
|
|||||||
+1
-32
@@ -511,7 +511,7 @@ public class CbxMetadataExtractor implements FileMetadataExtractor {
|
|||||||
.filter(Objects::nonNull)
|
.filter(Objects::nonNull)
|
||||||
.filter(this::canDecode)
|
.filter(this::canDecode)
|
||||||
.findFirst()
|
.findFirst()
|
||||||
.orElseGet(() -> generatePlaceholderCover(250, 350));
|
.orElse(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean canDecode(byte[] bytes) {
|
private boolean canDecode(byte[] bytes) {
|
||||||
@@ -616,37 +616,6 @@ public class CbxMetadataExtractor implements FileMetadataExtractor {
|
|||||||
return true;
|
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) {
|
private String findComicInfoEntry(Path cbxPath) {
|
||||||
try {
|
try {
|
||||||
return archiveService.streamEntryNames(cbxPath)
|
return archiveService.streamEntryNames(cbxPath)
|
||||||
|
|||||||
+4
-9
@@ -920,19 +920,14 @@ class CbxMetadataExtractorTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void returnPlaceholderForEmptyCbz() throws IOException {
|
void returnNullForEmptyCbz() throws IOException {
|
||||||
Path cbzPath = mockArchiveContents(Map.of(
|
Path cbzPath = mockArchiveContents(Map.of(
|
||||||
"readme.txt", "no images here".getBytes()
|
"readme.txt", "no images here".getBytes()
|
||||||
));
|
));
|
||||||
|
|
||||||
byte[] cover = extractor.extractCover(cbzPath);
|
byte[] cover = extractor.extractCover(cbzPath);
|
||||||
|
|
||||||
assertThat(cover).isNotNull();
|
assertThat(cover).isNull();
|
||||||
// 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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -1040,7 +1035,7 @@ class CbxMetadataExtractorTest {
|
|||||||
|
|
||||||
byte[] cover = extractor.extractCover(path);
|
byte[] cover = extractor.extractCover(path);
|
||||||
|
|
||||||
assertThat(cover).isNotNull();
|
assertThat(cover).isNull();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1197,7 +1192,7 @@ class CbxMetadataExtractorTest {
|
|||||||
|
|
||||||
byte[] cover = extractor.extractCover(path);
|
byte[] cover = extractor.extractCover(path);
|
||||||
|
|
||||||
assertThat(cover).isNotNull();
|
assertThat(cover).isNull();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user