diff --git a/backend/src/main/java/org/booklore/service/metadata/writer/EpubMetadataWriter.java b/backend/src/main/java/org/booklore/service/metadata/writer/EpubMetadataWriter.java index e2dadc5d0..6a45bb661 100644 --- a/backend/src/main/java/org/booklore/service/metadata/writer/EpubMetadataWriter.java +++ b/backend/src/main/java/org/booklore/service/metadata/writer/EpubMetadataWriter.java @@ -538,6 +538,12 @@ public class EpubMetadataWriter implements MetadataWriter { if (!entryPath.startsWith(targetDir)) { throw new IOException("ZIP entry outside target directory: " + name); } + + if (Files.exists(entryPath)) { + log.warn("EPUB Entry already exists, skipping: {}", entryPath); + continue; + } + Files.createDirectories(entryPath.getParent()); archiveService.extractEntryToPath(zipPath, name, entryPath); } diff --git a/backend/src/test/java/org/booklore/service/metadata/writer/EpubMetadataWriterTest.java b/backend/src/test/java/org/booklore/service/metadata/writer/EpubMetadataWriterTest.java index d3ce58991..ab2a30b2c 100644 --- a/backend/src/test/java/org/booklore/service/metadata/writer/EpubMetadataWriterTest.java +++ b/backend/src/test/java/org/booklore/service/metadata/writer/EpubMetadataWriterTest.java @@ -1,5 +1,7 @@ package org.booklore.service.metadata.writer; +import org.apache.commons.compress.archivers.zip.ZipArchiveEntry; +import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream; import org.booklore.model.MetadataClearFlags; import org.booklore.model.dto.settings.AppSettings; import org.booklore.model.dto.settings.MetadataPersistenceSettings; @@ -343,9 +345,75 @@ class EpubMetadataWriterTest { } @Nested - @DisplayName("Mimetype ZIP Entry Tests") + @DisplayName("ZIP Entry Tests") class MimetypeZipTests { + @Test + @DisplayName("Should handle duplicate entries in zip") + void saveMetadata_handlesDuplicateCovers() throws Exception { + String opfContent = """ + + + + Original Title + + + + + + """; + + byte[] coverImageA = new byte[]{0x0A}; + byte[] coverImageB = new byte[]{0x0B}; + byte[] coverImageC = new byte[]{0x0C}; + + File coverImageFile = tempDir.resolve("new-cover-" + System.nanoTime() + ".png").toFile(); + Files.write(coverImageFile.toPath(), coverImageC); + + File epubFile = tempDir.resolve("test-duplicate-" + System.nanoTime() + ".epub").toFile(); + + try (var zos = new ZipArchiveOutputStream(new FileOutputStream(epubFile))) { + String containerXml = """ + + + + + + + """; + + zos.putArchiveEntry(new ZipArchiveEntry("mimetype")); + zos.write("application/epub+zip".getBytes(StandardCharsets.UTF_8)); + zos.closeArchiveEntry(); + + zos.putArchiveEntry(new ZipArchiveEntry("META-INF/container.xml")); + zos.write(containerXml.getBytes(StandardCharsets.UTF_8)); + zos.closeArchiveEntry(); + + zos.putArchiveEntry(new ZipArchiveEntry("content.opf")); + zos.write(opfContent.getBytes(StandardCharsets.UTF_8)); + zos.closeArchiveEntry(); + + zos.putArchiveEntry(new ZipArchiveEntry("cover.png")); + zos.write(coverImageA); + zos.closeArchiveEntry(); + + zos.putArchiveEntry(new ZipArchiveEntry("cover.png")); + zos.write(coverImageB); + zos.closeArchiveEntry(); + } + + writer.saveMetadataToFile(epubFile, metadata, coverImageFile.toString(), new MetadataClearFlags()); + + try (ZipFile zf = new ZipFile(epubFile)) { + var entry = zf.getEntry("cover.png"); + + try (InputStream is = zf.getInputStream(entry)) { + assertThat(is.readAllBytes()).isEqualTo(coverImageC); + } + } + } + @Test @DisplayName("Should store mimetype as first uncompressed entry in ZIP") void saveMetadata_shouldStoreMimetypeFirst() throws Exception {