fix(os_x-conversion): ignore __MACOSX directory for conversions (#2479)
Signed-off-by: Balázs Szücs <bszucs1209@gmail.com>
This commit is contained in:
+64
-43
@@ -4,30 +4,29 @@ import com.adityachandel.booklore.model.entity.AuthorEntity;
|
|||||||
import com.adityachandel.booklore.model.entity.BookEntity;
|
import com.adityachandel.booklore.model.entity.BookEntity;
|
||||||
import com.adityachandel.booklore.model.entity.CategoryEntity;
|
import com.adityachandel.booklore.model.entity.CategoryEntity;
|
||||||
import com.adityachandel.booklore.model.entity.TagEntity;
|
import com.adityachandel.booklore.model.entity.TagEntity;
|
||||||
|
import com.adityachandel.booklore.util.ArchiveUtils;
|
||||||
|
import com.adityachandel.booklore.util.FileService;
|
||||||
|
import com.github.junrar.Archive;
|
||||||
|
import com.github.junrar.exception.RarException;
|
||||||
|
import com.github.junrar.rarfile.FileHeader;
|
||||||
import freemarker.cache.ClassTemplateLoader;
|
import freemarker.cache.ClassTemplateLoader;
|
||||||
import freemarker.template.Configuration;
|
import freemarker.template.Configuration;
|
||||||
import freemarker.template.Template;
|
import freemarker.template.Template;
|
||||||
import freemarker.template.TemplateException;
|
import freemarker.template.TemplateException;
|
||||||
import freemarker.template.TemplateExceptionHandler;
|
import freemarker.template.TemplateExceptionHandler;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.apache.commons.compress.archivers.sevenz.SevenZArchiveEntry;
|
||||||
|
import org.apache.commons.compress.archivers.sevenz.SevenZFile;
|
||||||
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
|
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
|
||||||
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
|
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
|
||||||
import org.apache.commons.compress.archivers.zip.ZipFile;
|
import org.apache.commons.compress.archivers.zip.ZipFile;
|
||||||
import org.apache.commons.compress.archivers.sevenz.SevenZArchiveEntry;
|
|
||||||
import org.apache.commons.compress.archivers.sevenz.SevenZFile;
|
|
||||||
import com.github.junrar.Archive;
|
|
||||||
import com.github.junrar.rarfile.FileHeader;
|
|
||||||
import com.github.junrar.exception.RarException;
|
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.util.FileSystemUtils;
|
import org.springframework.util.FileSystemUtils;
|
||||||
|
|
||||||
import com.adityachandel.booklore.util.ArchiveUtils;
|
import javax.imageio.IIOImage;
|
||||||
import com.adityachandel.booklore.util.FileService;
|
|
||||||
|
|
||||||
import javax.imageio.ImageIO;
|
import javax.imageio.ImageIO;
|
||||||
import javax.imageio.ImageWriteParam;
|
import javax.imageio.ImageWriteParam;
|
||||||
import javax.imageio.ImageWriter;
|
import javax.imageio.ImageWriter;
|
||||||
import javax.imageio.IIOImage;
|
|
||||||
import javax.imageio.stream.ImageOutputStream;
|
import javax.imageio.stream.ImageOutputStream;
|
||||||
import java.awt.image.BufferedImage;
|
import java.awt.image.BufferedImage;
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
@@ -43,9 +42,9 @@ import java.util.*;
|
|||||||
* <p>
|
* <p>
|
||||||
* This service supports the following comic book archive formats:
|
* This service supports the following comic book archive formats:
|
||||||
* <ul>
|
* <ul>
|
||||||
* <li><b>CBZ</b> - Comic book ZIP archive</li>
|
* <li><b>CBZ</b> - Comic book ZIP archive</li>
|
||||||
* <li><b>CBR</b> - Comic book RAR archive</li>
|
* <li><b>CBR</b> - Comic book RAR archive</li>
|
||||||
* <li><b>CB7</b> - Comic book 7z archive</li>
|
* <li><b>CB7</b> - Comic book 7z archive</li>
|
||||||
* </ul>
|
* </ul>
|
||||||
* </p>
|
* </p>
|
||||||
* <p>
|
* <p>
|
||||||
@@ -54,7 +53,7 @@ import java.util.*;
|
|||||||
*
|
*
|
||||||
* <h3>Size Limits</h3>
|
* <h3>Size Limits</h3>
|
||||||
* <ul>
|
* <ul>
|
||||||
* <li>Maximum individual image size: 50 MB</li>
|
* <li>Maximum individual image size: 50 MB</li>
|
||||||
* </ul>
|
* </ul>
|
||||||
*
|
*
|
||||||
* @see KepubConversionService
|
* @see KepubConversionService
|
||||||
@@ -88,22 +87,24 @@ public class CbxConversionService {
|
|||||||
* <p>
|
* <p>
|
||||||
* The conversion process:
|
* The conversion process:
|
||||||
* <ol>
|
* <ol>
|
||||||
* <li>Extracts all images from the archive to a temporary directory</li>
|
* <li>Extracts all images from the archive to a temporary directory</li>
|
||||||
* <li>Creates an EPUB structure with one XHTML page per image</li>
|
* <li>Creates an EPUB structure with one XHTML page per image</li>
|
||||||
* <li>Includes proper EPUB metadata from the book entity</li>
|
* <li>Includes proper EPUB metadata from the book entity</li>
|
||||||
* <li>JPEG images are passed through directly; other formats are converted to JPEG (85% quality)</li>
|
* <li>JPEG images are passed through directly; other formats are converted to
|
||||||
|
* JPEG (85% quality)</li>
|
||||||
* </ol>
|
* </ol>
|
||||||
* </p>
|
* </p>
|
||||||
*
|
*
|
||||||
* @param cbxFile the comic book archive file (must be CBZ, CBR, or CB7)
|
* @param cbxFile the comic book archive file (must be CBZ, CBR, or CB7)
|
||||||
* @param tempDir the temporary directory where the output EPUB will be created
|
* @param tempDir the temporary directory where the output EPUB will be
|
||||||
|
* created
|
||||||
* @param bookEntity the book metadata to include in the EPUB
|
* @param bookEntity the book metadata to include in the EPUB
|
||||||
* @return the converted EPUB file
|
* @return the converted EPUB file
|
||||||
* @throws IOException if file I/O operations fail
|
* @throws IOException if file I/O operations fail
|
||||||
* @throws TemplateException if EPUB template processing fails
|
* @throws TemplateException if EPUB template processing fails
|
||||||
* @throws RarException if RAR extraction fails (for CBR files)
|
* @throws RarException if RAR extraction fails (for CBR files)
|
||||||
* @throws IllegalArgumentException if the file format is not supported
|
* @throws IllegalArgumentException if the file format is not supported
|
||||||
* @throws IllegalStateException if no valid images are found in the archive
|
* @throws IllegalStateException if no valid images are found in the archive
|
||||||
*/
|
*/
|
||||||
public File convertCbxToEpub(File cbxFile, File tempDir, BookEntity bookEntity, int compressionPercentage)
|
public File convertCbxToEpub(File cbxFile, File tempDir, BookEntity bookEntity, int compressionPercentage)
|
||||||
throws IOException, TemplateException, RarException {
|
throws IOException, TemplateException, RarException {
|
||||||
@@ -111,14 +112,14 @@ public class CbxConversionService {
|
|||||||
|
|
||||||
log.info("Starting CBX to EPUB conversion for: {}", cbxFile.getName());
|
log.info("Starting CBX to EPUB conversion for: {}", cbxFile.getName());
|
||||||
|
|
||||||
File outputFile = executeCbxConversion(cbxFile, tempDir, bookEntity,compressionPercentage);
|
File outputFile = executeCbxConversion(cbxFile, tempDir, bookEntity, compressionPercentage);
|
||||||
|
|
||||||
log.info("Successfully converted {} to {} (size: {} bytes)",
|
log.info("Successfully converted {} to {} (size: {} bytes)",
|
||||||
cbxFile.getName(), outputFile.getName(), outputFile.length());
|
cbxFile.getName(), outputFile.getName(), outputFile.length());
|
||||||
return outputFile;
|
return outputFile;
|
||||||
}
|
}
|
||||||
|
|
||||||
private File executeCbxConversion(File cbxFile, File tempDir, BookEntity bookEntity,int compressionPercentage)
|
private File executeCbxConversion(File cbxFile, File tempDir, BookEntity bookEntity, int compressionPercentage)
|
||||||
throws IOException, TemplateException, RarException {
|
throws IOException, TemplateException, RarException {
|
||||||
|
|
||||||
Path epubFilePath = Paths.get(tempDir.getAbsolutePath(), cbxFile.getName() + ".epub");
|
Path epubFilePath = Paths.get(tempDir.getAbsolutePath(), cbxFile.getName() + ".epub");
|
||||||
@@ -139,7 +140,7 @@ public class CbxConversionService {
|
|||||||
addMetaInfContainer(zipOut);
|
addMetaInfContainer(zipOut);
|
||||||
addStylesheet(zipOut);
|
addStylesheet(zipOut);
|
||||||
|
|
||||||
List<EpubContentFileGroup> contentGroups = addImagesAndPages(zipOut, imagePaths,compressionPercentage);
|
List<EpubContentFileGroup> contentGroups = addImagesAndPages(zipOut, imagePaths, compressionPercentage);
|
||||||
|
|
||||||
addContentOpf(zipOut, bookEntity, contentGroups);
|
addContentOpf(zipOut, bookEntity, contentGroups);
|
||||||
addTocNcx(zipOut, bookEntity, contentGroups);
|
addTocNcx(zipOut, bookEntity, contentGroups);
|
||||||
@@ -203,7 +204,8 @@ public class CbxConversionService {
|
|||||||
.setIgnoreLocalFileHeader(true)
|
.setIgnoreLocalFileHeader(true)
|
||||||
.get()) {
|
.get()) {
|
||||||
List<Path> paths = extractImagesFromZipFile(zipFile, extractedImagesDir);
|
List<Path> paths = extractImagesFromZipFile(zipFile, extractedImagesDir);
|
||||||
if (!paths.isEmpty()) return paths;
|
if (!paths.isEmpty())
|
||||||
|
return paths;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.debug("Fast path extraction failed for {}: {}", cbzFile.getName(), e.getMessage());
|
log.debug("Fast path extraction failed for {}: {}", cbzFile.getName(), e.getMessage());
|
||||||
}
|
}
|
||||||
@@ -283,7 +285,7 @@ public class CbxConversionService {
|
|||||||
try {
|
try {
|
||||||
Path outputPath = extractedImagesDir.resolve(extractFileName(entry.getName()));
|
Path outputPath = extractedImagesDir.resolve(extractFileName(entry.getName()));
|
||||||
try (InputStream entryInputStream = sevenZFile.getInputStream(entry);
|
try (InputStream entryInputStream = sevenZFile.getInputStream(entry);
|
||||||
OutputStream fileOutputStream = Files.newOutputStream(outputPath)) {
|
OutputStream fileOutputStream = Files.newOutputStream(outputPath)) {
|
||||||
entryInputStream.transferTo(fileOutputStream);
|
entryInputStream.transferTo(fileOutputStream);
|
||||||
}
|
}
|
||||||
imagePaths.add(outputPath);
|
imagePaths.add(outputPath);
|
||||||
@@ -310,12 +312,30 @@ public class CbxConversionService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private boolean isImageFile(String fileName) {
|
private boolean isImageFile(String fileName) {
|
||||||
|
if (shouldIgnoreEntry(fileName)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
String lowerName = fileName.toLowerCase();
|
String lowerName = fileName.toLowerCase();
|
||||||
|
|
||||||
return lowerName.endsWith(".jpg") || lowerName.endsWith(".jpeg") ||
|
return lowerName.endsWith(".jpg") || lowerName.endsWith(".jpeg") ||
|
||||||
lowerName.endsWith(".png") || lowerName.endsWith(".webp") ||
|
lowerName.endsWith(".png") || lowerName.endsWith(".webp") ||
|
||||||
lowerName.endsWith(".gif") || lowerName.endsWith(".bmp") ||
|
lowerName.endsWith(".gif") || lowerName.endsWith(".bmp") ||
|
||||||
lowerName.endsWith(".avif") || lowerName.endsWith(".heic");
|
lowerName.endsWith(".avif") || lowerName.endsWith(".heic");
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean shouldIgnoreEntry(String entryName) {
|
||||||
|
if (entryName.contains("__MACOSX")) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
String fileName = entryName;
|
||||||
|
int lastSlash = entryName.lastIndexOf('/');
|
||||||
|
if (lastSlash >= 0) {
|
||||||
|
fileName = entryName.substring(lastSlash + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
return fileName.startsWith("._");
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean isJpegFile(Path path) {
|
private boolean isJpegFile(Path path) {
|
||||||
@@ -362,13 +382,14 @@ public class CbxConversionService {
|
|||||||
zipOut.closeArchiveEntry();
|
zipOut.closeArchiveEntry();
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<EpubContentFileGroup> addImagesAndPages(ZipArchiveOutputStream zipOut, List<Path> imagePaths,int compressionPercentage)
|
private List<EpubContentFileGroup> addImagesAndPages(ZipArchiveOutputStream zipOut, List<Path> imagePaths,
|
||||||
|
int compressionPercentage)
|
||||||
throws IOException, TemplateException {
|
throws IOException, TemplateException {
|
||||||
|
|
||||||
List<EpubContentFileGroup> contentGroups = new ArrayList<>();
|
List<EpubContentFileGroup> contentGroups = new ArrayList<>();
|
||||||
|
|
||||||
if (!imagePaths.isEmpty()) {
|
if (!imagePaths.isEmpty()) {
|
||||||
addImageToZipFromPath(zipOut, COVER_IMAGE_PATH, imagePaths.getFirst(),compressionPercentage);
|
addImageToZipFromPath(zipOut, COVER_IMAGE_PATH, imagePaths.getFirst(), compressionPercentage);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0; i < imagePaths.size(); i++) {
|
for (int i = 0; i < imagePaths.size(); i++) {
|
||||||
@@ -380,7 +401,7 @@ public class CbxConversionService {
|
|||||||
String imagePath = IMAGE_ROOT_PATH + imageFileName;
|
String imagePath = IMAGE_ROOT_PATH + imageFileName;
|
||||||
String htmlPath = HTML_ROOT_PATH + htmlFileName;
|
String htmlPath = HTML_ROOT_PATH + htmlFileName;
|
||||||
|
|
||||||
addImageToZipFromPath(zipOut, imagePath, imageSourcePath,compressionPercentage);
|
addImageToZipFromPath(zipOut, imagePath, imageSourcePath, compressionPercentage);
|
||||||
|
|
||||||
String htmlContent = generatePageHtml(imageFileName, i + 1);
|
String htmlContent = generatePageHtml(imageFileName, i + 1);
|
||||||
ZipArchiveEntry htmlEntry = new ZipArchiveEntry(htmlPath);
|
ZipArchiveEntry htmlEntry = new ZipArchiveEntry(htmlPath);
|
||||||
@@ -394,7 +415,8 @@ public class CbxConversionService {
|
|||||||
return contentGroups;
|
return contentGroups;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void addImageToZipFromPath(ZipArchiveOutputStream zipOut, String epubImagePath, Path sourceImagePath,int compressionPercentage)
|
private void addImageToZipFromPath(ZipArchiveOutputStream zipOut, String epubImagePath, Path sourceImagePath,
|
||||||
|
int compressionPercentage)
|
||||||
throws IOException {
|
throws IOException {
|
||||||
ZipArchiveEntry imageEntry = new ZipArchiveEntry(epubImagePath);
|
ZipArchiveEntry imageEntry = new ZipArchiveEntry(epubImagePath);
|
||||||
zipOut.putArchiveEntry(imageEntry);
|
zipOut.putArchiveEntry(imageEntry);
|
||||||
@@ -413,7 +435,7 @@ public class CbxConversionService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (image != null) {
|
if (image != null) {
|
||||||
writeJpegImage(image, zipOut, compressionPercentage/100f);
|
writeJpegImage(image, zipOut, compressionPercentage / 100f);
|
||||||
} else {
|
} else {
|
||||||
log.warn("Could not decode image {}, copying raw bytes", sourceImagePath.getFileName());
|
log.warn("Could not decode image {}, copying raw bytes", sourceImagePath.getFileName());
|
||||||
try (InputStream rawStream = Files.newInputStream(sourceImagePath)) {
|
try (InputStream rawStream = Files.newInputStream(sourceImagePath)) {
|
||||||
@@ -470,7 +492,7 @@ public class CbxConversionService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void addContentOpf(ZipArchiveOutputStream zipOut, BookEntity bookEntity,
|
private void addContentOpf(ZipArchiveOutputStream zipOut, BookEntity bookEntity,
|
||||||
List<EpubContentFileGroup> contentGroups) throws IOException, TemplateException {
|
List<EpubContentFileGroup> contentGroups) throws IOException, TemplateException {
|
||||||
|
|
||||||
Map<String, Object> model = createBookMetadataModel(bookEntity);
|
Map<String, Object> model = createBookMetadataModel(bookEntity);
|
||||||
|
|
||||||
@@ -478,8 +500,7 @@ public class CbxConversionService {
|
|||||||
.map(group -> new EpubContentFileGroup(
|
.map(group -> new EpubContentFileGroup(
|
||||||
group.contentKey(),
|
group.contentKey(),
|
||||||
makeRelativeToOebps(group.imagePath()),
|
makeRelativeToOebps(group.imagePath()),
|
||||||
makeRelativeToOebps(group.htmlPath())
|
makeRelativeToOebps(group.htmlPath())))
|
||||||
))
|
|
||||||
.toList();
|
.toList();
|
||||||
|
|
||||||
model.put("contentFileGroups", relativeContentGroups);
|
model.put("contentFileGroups", relativeContentGroups);
|
||||||
@@ -498,7 +519,7 @@ public class CbxConversionService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void addTocNcx(ZipArchiveOutputStream zipOut, BookEntity bookEntity,
|
private void addTocNcx(ZipArchiveOutputStream zipOut, BookEntity bookEntity,
|
||||||
List<EpubContentFileGroup> contentGroups) throws IOException, TemplateException {
|
List<EpubContentFileGroup> contentGroups) throws IOException, TemplateException {
|
||||||
|
|
||||||
Map<String, Object> model = createBookMetadataModel(bookEntity);
|
Map<String, Object> model = createBookMetadataModel(bookEntity);
|
||||||
model.put("contentFileGroups", contentGroups);
|
model.put("contentFileGroups", contentGroups);
|
||||||
@@ -512,7 +533,7 @@ public class CbxConversionService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void addNavXhtml(ZipArchiveOutputStream zipOut, BookEntity bookEntity,
|
private void addNavXhtml(ZipArchiveOutputStream zipOut, BookEntity bookEntity,
|
||||||
List<EpubContentFileGroup> contentGroups) throws IOException, TemplateException {
|
List<EpubContentFileGroup> contentGroups) throws IOException, TemplateException {
|
||||||
|
|
||||||
Map<String, Object> model = createBookMetadataModel(bookEntity);
|
Map<String, Object> model = createBookMetadataModel(bookEntity);
|
||||||
model.put("contentFileGroups", contentGroups);
|
model.put("contentFileGroups", contentGroups);
|
||||||
@@ -650,8 +671,8 @@ public class CbxConversionService {
|
|||||||
}
|
}
|
||||||
String lowerName = fileName.toLowerCase();
|
String lowerName = fileName.toLowerCase();
|
||||||
return lowerName.endsWith(".cbz") ||
|
return lowerName.endsWith(".cbz") ||
|
||||||
lowerName.endsWith(".cbr") ||
|
lowerName.endsWith(".cbr") ||
|
||||||
lowerName.endsWith(".cb7");
|
lowerName.endsWith(".cb7");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user