feat: ComicInfo.xml support for CBX files (#1073)
* write, read ComicInfo.xml for CBZ files * updated with recommendations from previous PR https://github.com/booklore-app/booklore/pull/1069 * read ComicInfo.xml on CBR, extract first image for CBZ/CBR files, save CBR metadata in ComicInfo.xml as CBZ * Delete CBR file after CBZ conversion. Update DB * Backs up file before updating. Restores back up if errored * Test classes * Update booklore-api/src/main/java/com/adityachandel/booklore/service/metadata/BookMetadataUpdater.java Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update booklore-api/src/main/java/com/adityachandel/booklore/service/metadata/writer/CbxMetadataWriter.java Updating RAR binary availability check Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update booklore-api/src/main/java/com/adityachandel/booklore/service/metadata/extractor/CbxMetadataExtractor.java Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update booklore-api/src/main/java/com/adityachandel/booklore/service/metadata/extractor/CbxMetadataExtractor.java Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Code updates from PR suggestions * Support to extract ComicInfo.xml for .cb7 files * Writer extension for .cb7 files * Adding com.github.junrar to build.gradle (forgot in previous commit) * Settings toggle to control CBR/CB7 to CBZ conversion * indentation complains * removed duplicated junrar inport * Restore comicinfo.xml metadata in edit view * retrieve ComicInfo.xml metadata for new files in library scan * private class definition was missed after merge * Delete --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
+153
@@ -0,0 +1,153 @@
|
||||
package com.adityachandel.booklore.service.metadata.extractor;
|
||||
|
||||
import com.adityachandel.booklore.model.dto.BookMetadata;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.time.LocalDate;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipOutputStream;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class CbxMetadataExtractorTest {
|
||||
|
||||
private CbxMetadataExtractor extractor;
|
||||
private Path tempDir;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() throws IOException {
|
||||
extractor = new CbxMetadataExtractor();
|
||||
tempDir = Files.createTempDirectory("cbx_test_");
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void tearDown() throws IOException {
|
||||
if (tempDir != null) {
|
||||
// best-effort cleanup
|
||||
Files.walk(tempDir)
|
||||
.sorted((a, b) -> b.compareTo(a))
|
||||
.forEach(p -> { try { Files.deleteIfExists(p); } catch (Exception ignore) {} });
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void extractMetadata_fromCbz_withComicInfo_populatesFields() throws Exception {
|
||||
String xml = "" +
|
||||
"<ComicInfo>" +
|
||||
" <Title>My Comic</Title>" +
|
||||
" <Summary>A short summary</Summary>" +
|
||||
" <Publisher>Indie</Publisher>" +
|
||||
" <Series>Series X</Series>" +
|
||||
" <Number>2.5</Number>" +
|
||||
" <Count>12</Count>" +
|
||||
" <Year>2020</Year><Month>7</Month><Day>14</Day>" +
|
||||
" <PageCount>42</PageCount>" +
|
||||
" <LanguageISO>en</LanguageISO>" +
|
||||
" <Writer>Alice</Writer>" +
|
||||
" <Penciller>Bob</Penciller>" +
|
||||
" <Tags>action;adventure</Tags>" +
|
||||
"</ComicInfo>";
|
||||
|
||||
File cbz = createCbz("with_meta.cbz", new LinkedHashMap<>() {{
|
||||
put("ComicInfo.xml", xml.getBytes(StandardCharsets.UTF_8));
|
||||
put("page1.jpg", new byte[]{1,2,3});
|
||||
}});
|
||||
|
||||
BookMetadata md = extractor.extractMetadata(cbz);
|
||||
assertEquals("My Comic", md.getTitle());
|
||||
assertEquals("A short summary", md.getDescription());
|
||||
assertEquals("Indie", md.getPublisher());
|
||||
assertEquals("Series X", md.getSeriesName());
|
||||
assertEquals(2.5f, md.getSeriesNumber());
|
||||
assertEquals(Integer.valueOf(12), md.getSeriesTotal());
|
||||
assertEquals(LocalDate.of(2020,7,14), md.getPublishedDate());
|
||||
assertEquals(Integer.valueOf(42), md.getPageCount());
|
||||
assertEquals("en", md.getLanguage());
|
||||
assertTrue(md.getAuthors().contains("Alice"));
|
||||
assertTrue(md.getAuthors().contains("Bob"));
|
||||
assertTrue(md.getCategories().contains("action"));
|
||||
assertTrue(md.getCategories().contains("adventure"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void extractCover_fromCbz_usesComicInfoImageFile() throws Exception {
|
||||
String xml = "" +
|
||||
"<ComicInfo>" +
|
||||
" <Pages>" +
|
||||
" <Page Type=\"FrontCover\" ImageFile=\"images/002.jpg\"/>" +
|
||||
" </Pages>" +
|
||||
"</ComicInfo>";
|
||||
|
||||
byte[] img1 = new byte[]{11};
|
||||
byte[] img2 = new byte[]{22, 22}; // expect this one
|
||||
byte[] img3 = new byte[]{33, 33, 33};
|
||||
|
||||
File cbz = createCbz("with_cover.cbz", new LinkedHashMap<>() {{
|
||||
put("ComicInfo.xml", xml.getBytes(StandardCharsets.UTF_8));
|
||||
put("images/001.jpg", img1);
|
||||
put("images/002.jpg", img2);
|
||||
put("images/003.jpg", img3);
|
||||
}});
|
||||
|
||||
byte[] cover = extractor.extractCover(cbz);
|
||||
assertArrayEquals(img2, cover);
|
||||
}
|
||||
|
||||
@Test
|
||||
void extractCover_fromCbz_fallbackAlphabeticalFirst() throws Exception {
|
||||
// No ComicInfo.xml, images intentionally added in unsorted order
|
||||
byte[] aPng = new byte[]{1,1}; // alphabetically first (A.png)
|
||||
byte[] bJpg = new byte[]{2};
|
||||
byte[] cJpeg = new byte[]{3,3,3};
|
||||
|
||||
File cbz = createCbz("fallback.cbz", new LinkedHashMap<>() {{
|
||||
put("z/pageC.jpeg", cJpeg);
|
||||
put("A.png", aPng); // should be chosen
|
||||
put("b.jpg", bJpg);
|
||||
}});
|
||||
|
||||
byte[] cover = extractor.extractCover(cbz);
|
||||
assertArrayEquals(aPng, cover);
|
||||
}
|
||||
|
||||
@Test
|
||||
void extractMetadata_nonArchive_fallbackTitle() throws Exception {
|
||||
Path txt = tempDir.resolve("Some Book Title.txt");
|
||||
Files.write(txt, "hello".getBytes(StandardCharsets.UTF_8));
|
||||
BookMetadata md = extractor.extractMetadata(txt.toFile());
|
||||
assertEquals("Some Book Title", md.getTitle());
|
||||
}
|
||||
|
||||
// ---------- helpers ----------
|
||||
|
||||
private File createCbz(String name, Map<String, byte[]> entries) throws IOException {
|
||||
Path out = tempDir.resolve(name);
|
||||
try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(out.toFile()))) {
|
||||
for (Map.Entry<String, byte[]> e : entries.entrySet()) {
|
||||
String entryName = e.getKey();
|
||||
byte[] data = e.getValue();
|
||||
ZipEntry ze = new ZipEntry(entryName);
|
||||
// set a fixed time to avoid platform-dependent headers
|
||||
ze.setTime(0L);
|
||||
zos.putNextEntry(ze);
|
||||
try (InputStream is = new ByteArrayInputStream(data)) {
|
||||
is.transferTo(zos);
|
||||
}
|
||||
zos.closeEntry();
|
||||
}
|
||||
}
|
||||
return out.toFile();
|
||||
}
|
||||
}
|
||||
+190
@@ -0,0 +1,190 @@
|
||||
package com.adityachandel.booklore.service.metadata.writer;
|
||||
|
||||
import com.adityachandel.booklore.model.MetadataClearFlags;
|
||||
import com.adityachandel.booklore.model.entity.BookMetadataEntity;
|
||||
import com.adityachandel.booklore.model.enums.BookFileType;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.w3c.dom.Document;
|
||||
|
||||
import javax.xml.XMLConstants;
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.time.LocalDate;
|
||||
import java.util.Enumeration;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipFile;
|
||||
import java.util.zip.ZipOutputStream;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class CbxMetadataWriterTest {
|
||||
|
||||
private CbxMetadataWriter writer;
|
||||
private Path tempDir;
|
||||
|
||||
@BeforeEach
|
||||
void setup() throws Exception {
|
||||
writer = new CbxMetadataWriter();
|
||||
tempDir = Files.createTempDirectory("cbx_writer_test_");
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void cleanup() throws Exception {
|
||||
if (tempDir != null) {
|
||||
Files.walk(tempDir)
|
||||
.sorted((a, b) -> b.compareTo(a))
|
||||
.forEach(p -> { try { Files.deleteIfExists(p); } catch (Exception ignore) {} });
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void getSupportedBookType_isCbx() {
|
||||
assertEquals(BookFileType.CBX, writer.getSupportedBookType());
|
||||
}
|
||||
|
||||
@Test
|
||||
void writeMetadataToFile_cbz_updatesOrCreatesComicInfo_andPreservesOtherFiles() throws Exception {
|
||||
// Create a CBZ without ComicInfo.xml and with a couple of images
|
||||
File cbz = createCbz(tempDir.resolve("sample.cbz"), new String[]{
|
||||
"images/002.jpg", "images/001.jpg"
|
||||
});
|
||||
|
||||
// Prepare metadata
|
||||
BookMetadataEntity meta = new BookMetadataEntity();
|
||||
meta.setTitle("My Comic");
|
||||
meta.setDescription("Short desc");
|
||||
meta.setPublisher("Indie");
|
||||
meta.setSeriesName("Series X");
|
||||
meta.setSeriesNumber(2.5f);
|
||||
meta.setSeriesTotal(12);
|
||||
meta.setPublishedDate(LocalDate.of(2020,7,14));
|
||||
meta.setPageCount(42);
|
||||
meta.setLanguage("en");
|
||||
Set<String> authors = new HashSet<>();
|
||||
authors.add("Alice");
|
||||
authors.add("Bob");
|
||||
meta.setAuthors(authors);
|
||||
Set<String> cats = new HashSet<>();
|
||||
cats.add("action");
|
||||
cats.add("adventure");
|
||||
meta.setCategories(cats);
|
||||
|
||||
// Execute
|
||||
writer.writeMetadataToFile(cbz, meta, null, false, MetadataClearFlags.builder().build());
|
||||
|
||||
// Assert ComicInfo.xml exists and contains our fields
|
||||
try (ZipFile zip = new ZipFile(cbz)) {
|
||||
ZipEntry ci = zip.getEntry("ComicInfo.xml");
|
||||
assertNotNull(ci, "ComicInfo.xml should be present after write");
|
||||
|
||||
Document doc = parseXml(zip.getInputStream(ci));
|
||||
String title = text(doc, "Title");
|
||||
String summary = text(doc, "Summary");
|
||||
String publisher = text(doc, "Publisher");
|
||||
String series = text(doc, "Series");
|
||||
String number = text(doc, "Number");
|
||||
String count = text(doc, "Count");
|
||||
String year = text(doc, "Year");
|
||||
String month = text(doc, "Month");
|
||||
String day = text(doc, "Day");
|
||||
String pageCount = text(doc, "PageCount");
|
||||
String lang = text(doc, "LanguageISO");
|
||||
String writerEl = text(doc, "Writer");
|
||||
String genre = text(doc, "Genre");
|
||||
|
||||
assertEquals("My Comic", title);
|
||||
assertEquals("Short desc", summary);
|
||||
assertEquals("Indie", publisher);
|
||||
assertEquals("Series X", series);
|
||||
assertEquals("2.5", number);
|
||||
assertEquals("12", count);
|
||||
assertEquals("2020", year);
|
||||
assertEquals("7", month);
|
||||
assertEquals("14", day);
|
||||
assertEquals("42", pageCount);
|
||||
assertEquals("en", lang);
|
||||
assertTrue(writerEl.contains("Alice"));
|
||||
assertTrue(writerEl.contains("Bob"));
|
||||
assertTrue(genre.toLowerCase().contains("action"));
|
||||
assertTrue(genre.toLowerCase().contains("adventure"));
|
||||
|
||||
// Ensure original image entries are preserved
|
||||
assertNotNull(zip.getEntry("images/001.jpg"));
|
||||
assertNotNull(zip.getEntry("images/002.jpg"));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void writeMetadataToFile_cbz_updatesExistingComicInfo() throws Exception {
|
||||
// Create a CBZ *with* an existing ComicInfo.xml
|
||||
Path out = tempDir.resolve("with_meta.cbz");
|
||||
String xml = "<ComicInfo>\n" +
|
||||
" <Title>Old Title</Title>\n" +
|
||||
" <Summary>Old Summary</Summary>\n" +
|
||||
"</ComicInfo>";
|
||||
try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(out.toFile()))) {
|
||||
put(zos, "ComicInfo.xml", xml.getBytes(StandardCharsets.UTF_8));
|
||||
put(zos, "a.jpg", new byte[]{1});
|
||||
}
|
||||
|
||||
BookMetadataEntity meta = new BookMetadataEntity();
|
||||
meta.setTitle("New Title");
|
||||
meta.setDescription("New Summary");
|
||||
|
||||
writer.writeMetadataToFile(out.toFile(), meta, null, false, MetadataClearFlags.builder().build());
|
||||
|
||||
try (ZipFile zip = new ZipFile(out.toFile())) {
|
||||
ZipEntry ci = zip.getEntry("ComicInfo.xml");
|
||||
Document doc = parseXml(zip.getInputStream(ci));
|
||||
assertEquals("New Title", text(doc, "Title"));
|
||||
assertEquals("New Summary", text(doc, "Summary"));
|
||||
// a.jpg should still exist
|
||||
assertNotNull(zip.getEntry("a.jpg"));
|
||||
}
|
||||
}
|
||||
|
||||
// ------------- helpers -------------
|
||||
|
||||
private static File createCbz(Path path, String[] imageNames) throws Exception {
|
||||
try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(path.toFile()))) {
|
||||
for (String name : imageNames) {
|
||||
put(zos, name, new byte[]{1,2,3});
|
||||
}
|
||||
}
|
||||
return path.toFile();
|
||||
}
|
||||
|
||||
private static void put(ZipOutputStream zos, String name, byte[] data) throws Exception {
|
||||
ZipEntry ze = new ZipEntry(name);
|
||||
ze.setTime(0L);
|
||||
zos.putNextEntry(ze);
|
||||
zos.write(data);
|
||||
zos.closeEntry();
|
||||
}
|
||||
|
||||
private static Document parseXml(InputStream is) throws Exception {
|
||||
DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
|
||||
f.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
|
||||
f.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
|
||||
f.setAttribute(XMLConstants.ACCESS_EXTERNAL_SCHEMA, "");
|
||||
DocumentBuilder b = f.newDocumentBuilder();
|
||||
return b.parse(is);
|
||||
}
|
||||
|
||||
private static String text(Document doc, String tag) {
|
||||
var list = doc.getElementsByTagName(tag);
|
||||
if (list.getLength() == 0) return null;
|
||||
return list.item(0).getTextContent();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user