chore(epub): avoid EpubContainer in EpubMetadataWriter (#2162)
This commit is contained in:
+14
-13
@@ -8,6 +8,7 @@ import org.booklore.model.dto.settings.MetadataPersistenceSettings;
|
||||
import org.booklore.model.entity.BookEntity;
|
||||
import org.booklore.model.entity.BookMetadataEntity;
|
||||
import org.booklore.model.enums.BookFileType;
|
||||
import org.booklore.service.ArchiveService;
|
||||
import org.booklore.service.appsettings.AppSettingService;
|
||||
import org.booklore.util.SecureXmlUtils;
|
||||
import org.springframework.stereotype.Component;
|
||||
@@ -32,7 +33,6 @@ import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.net.URI;
|
||||
import java.net.URLDecoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
@@ -46,8 +46,6 @@ import java.util.zip.CRC32;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipOutputStream;
|
||||
|
||||
import org.grimmory.epub4j.archive.EpubContainer;
|
||||
import org.grimmory.epub4j.archive.EpubContainers;
|
||||
import java.util.function.Predicate;
|
||||
import java.io.UncheckedIOException;
|
||||
|
||||
@@ -59,6 +57,7 @@ public class EpubMetadataWriter implements MetadataWriter {
|
||||
private static final String OPF_NS = "http://www.idpf.org/2007/opf";
|
||||
private static final Pattern CALIBRE_PREFIX_PATTERN = Pattern.compile("calibre:\\s*https?://[^\\s]+");
|
||||
private final AppSettingService appSettingService;
|
||||
private final ArchiveService archiveService;
|
||||
|
||||
@Override
|
||||
public void saveMetadataToFile(File epubFile, BookMetadataEntity metadata, String thumbnailUrl, MetadataClearFlags clear) {
|
||||
@@ -528,17 +527,19 @@ public class EpubMetadataWriter implements MetadataWriter {
|
||||
}
|
||||
|
||||
private void extractZipToDirectory(File zipSource, Path targetDir) throws IOException {
|
||||
try (EpubContainer container = EpubContainers.open(zipSource.toPath())) {
|
||||
for (String name : container.listAllFiles()) {
|
||||
Path entryPath = targetDir.resolve(name).normalize();
|
||||
if (!entryPath.startsWith(targetDir)) {
|
||||
throw new IOException("ZIP entry outside target directory: " + name);
|
||||
}
|
||||
Files.createDirectories(entryPath.getParent());
|
||||
try (OutputStream out = Files.newOutputStream(entryPath)) {
|
||||
container.streamTo(name, out);
|
||||
}
|
||||
Path zipPath = zipSource.toPath();
|
||||
|
||||
if (!Files.isRegularFile(zipPath) || !Files.isReadable(zipPath)) {
|
||||
throw new IOException("Target is not a readable regular file.");
|
||||
}
|
||||
|
||||
for (var name : archiveService.getEntryNames(zipPath)) {
|
||||
Path entryPath = targetDir.resolve(name).normalize();
|
||||
if (!entryPath.startsWith(targetDir)) {
|
||||
throw new IOException("ZIP entry outside target directory: " + name);
|
||||
}
|
||||
Files.createDirectories(entryPath.getParent());
|
||||
archiveService.extractEntryToPath(zipPath, name, entryPath);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+12
-2
@@ -8,12 +8,17 @@ import org.booklore.model.entity.BookEntity;
|
||||
import org.booklore.model.entity.BookFileEntity;
|
||||
import org.booklore.model.entity.BookMetadataEntity;
|
||||
import org.booklore.model.entity.LibraryPathEntity;
|
||||
import org.booklore.service.ArchiveService;
|
||||
import org.booklore.service.appsettings.AppSettingService;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Nested;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Spy;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import org.springframework.mock.web.MockMultipartFile;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import org.w3c.dom.Document;
|
||||
@@ -40,12 +45,17 @@ import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
import java.net.URLDecoder;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class EpubMetadataWriterTest {
|
||||
@Mock
|
||||
private AppSettingService appSettingService;
|
||||
|
||||
@Spy
|
||||
private ArchiveService archiveService = new ArchiveService();
|
||||
|
||||
private EpubMetadataWriter writer;
|
||||
private BookMetadataEntity metadata;
|
||||
private BookEntity bookEntity;
|
||||
private AppSettingService appSettingService;
|
||||
|
||||
@TempDir
|
||||
Path tempDir;
|
||||
@@ -67,7 +77,7 @@ class EpubMetadataWriterTest {
|
||||
when(appSettings.getMetadataPersistenceSettings()).thenReturn(metadataPersistenceSettings);
|
||||
when(appSettingService.getAppSettings()).thenReturn(appSettings);
|
||||
|
||||
writer = new EpubMetadataWriter(appSettingService);
|
||||
writer = new EpubMetadataWriter(appSettingService, archiveService);
|
||||
metadata = new BookMetadataEntity();
|
||||
metadata.setTitle("Test Book");
|
||||
AuthorEntity author = new AuthorEntity();
|
||||
|
||||
Reference in New Issue
Block a user