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:
Balázs Szücs
2026-01-27 10:26:31 -07:00
committed by GitHub
parent 496a67a784
commit fab7c14030
@@ -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.*;
@@ -91,12 +90,14 @@ public class CbxConversionService {
* <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
@@ -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());
} }
@@ -310,6 +312,10 @@ 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") ||
@@ -318,6 +324,20 @@ public class CbxConversionService {
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) {
Set<String> jpegExtensions = Set.of(".jpg", ".jpeg"); Set<String> jpegExtensions = Set.of(".jpg", ".jpeg");
String fileName = path.getFileName().toString().toLowerCase(); String fileName = path.getFileName().toString().toLowerCase();
@@ -362,7 +382,8 @@ 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<>();
@@ -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);
@@ -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);