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:
+37
-16
@@ -4,30 +4,29 @@ import com.adityachandel.booklore.model.entity.AuthorEntity;
|
||||
import com.adityachandel.booklore.model.entity.BookEntity;
|
||||
import com.adityachandel.booklore.model.entity.CategoryEntity;
|
||||
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.template.Configuration;
|
||||
import freemarker.template.Template;
|
||||
import freemarker.template.TemplateException;
|
||||
import freemarker.template.TemplateExceptionHandler;
|
||||
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.ZipArchiveOutputStream;
|
||||
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.util.FileSystemUtils;
|
||||
|
||||
import com.adityachandel.booklore.util.ArchiveUtils;
|
||||
import com.adityachandel.booklore.util.FileService;
|
||||
|
||||
import javax.imageio.IIOImage;
|
||||
import javax.imageio.ImageIO;
|
||||
import javax.imageio.ImageWriteParam;
|
||||
import javax.imageio.ImageWriter;
|
||||
import javax.imageio.IIOImage;
|
||||
import javax.imageio.stream.ImageOutputStream;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.*;
|
||||
@@ -91,12 +90,14 @@ public class CbxConversionService {
|
||||
* <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>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>
|
||||
* </p>
|
||||
*
|
||||
* @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
|
||||
* @return the converted EPUB file
|
||||
* @throws IOException if file I/O operations fail
|
||||
@@ -203,7 +204,8 @@ public class CbxConversionService {
|
||||
.setIgnoreLocalFileHeader(true)
|
||||
.get()) {
|
||||
List<Path> paths = extractImagesFromZipFile(zipFile, extractedImagesDir);
|
||||
if (!paths.isEmpty()) return paths;
|
||||
if (!paths.isEmpty())
|
||||
return paths;
|
||||
} catch (Exception e) {
|
||||
log.debug("Fast path extraction failed for {}: {}", cbzFile.getName(), e.getMessage());
|
||||
}
|
||||
@@ -310,6 +312,10 @@ public class CbxConversionService {
|
||||
}
|
||||
|
||||
private boolean isImageFile(String fileName) {
|
||||
if (shouldIgnoreEntry(fileName)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
String lowerName = fileName.toLowerCase();
|
||||
|
||||
return lowerName.endsWith(".jpg") || lowerName.endsWith(".jpeg") ||
|
||||
@@ -318,6 +324,20 @@ public class CbxConversionService {
|
||||
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) {
|
||||
Set<String> jpegExtensions = Set.of(".jpg", ".jpeg");
|
||||
String fileName = path.getFileName().toString().toLowerCase();
|
||||
@@ -362,7 +382,8 @@ public class CbxConversionService {
|
||||
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 {
|
||||
|
||||
List<EpubContentFileGroup> contentGroups = new ArrayList<>();
|
||||
@@ -394,7 +415,8 @@ public class CbxConversionService {
|
||||
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 {
|
||||
ZipArchiveEntry imageEntry = new ZipArchiveEntry(epubImagePath);
|
||||
zipOut.putArchiveEntry(imageEntry);
|
||||
@@ -478,8 +500,7 @@ public class CbxConversionService {
|
||||
.map(group -> new EpubContentFileGroup(
|
||||
group.contentKey(),
|
||||
makeRelativeToOebps(group.imagePath()),
|
||||
makeRelativeToOebps(group.htmlPath())
|
||||
))
|
||||
makeRelativeToOebps(group.htmlPath())))
|
||||
.toList();
|
||||
|
||||
model.put("contentFileGroups", relativeContentGroups);
|
||||
|
||||
Reference in New Issue
Block a user