Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

255 lines
10 KiB
Java
Raw Normal View History

2023-08-01 00:03:13 +01:00
package stirling.software.SPDF.utils;
import java.io.ByteArrayInputStream;
2024-02-09 23:24:25 +00:00
import java.io.File;
2024-10-14 22:34:41 +01:00
import java.io.FileOutputStream;
2024-02-09 23:24:25 +00:00
import java.io.FileWriter;
2023-08-01 00:03:13 +01:00
import java.io.IOException;
2024-10-14 22:34:41 +01:00
import java.io.UncheckedIOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileVisitResult;
2023-08-01 00:03:13 +01:00
import java.nio.file.Files;
import java.nio.file.Path;
2024-10-14 22:34:41 +01:00
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
2023-08-01 00:03:13 +01:00
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
2024-10-14 22:34:41 +01:00
import java.util.zip.ZipOutputStream;
2023-08-01 00:03:13 +01:00
2024-02-06 00:00:49 +00:00
import io.github.pixee.security.ZipSecurity;
2024-01-28 17:36:17 +00:00
import stirling.software.SPDF.model.api.converters.HTMLToPdfRequest;
2023-08-01 00:03:13 +01:00
import stirling.software.SPDF.utils.ProcessExecutor.ProcessExecutorResult;
public class FileToPdf {
2024-01-09 22:39:21 +00:00
public static byte[] convertHtmlToPdf(
2024-01-28 17:36:17 +00:00
HTMLToPdfRequest request,
byte[] fileBytes,
String fileName,
boolean htmlFormatsInstalled)
2023-08-01 00:03:13 +01:00
throws IOException, InterruptedException {
Path tempOutputFile = Files.createTempFile("output_", ".pdf");
Path tempInputFile = null;
byte[] pdfBytes;
try {
if (fileName.endsWith(".html")) {
tempInputFile = Files.createTempFile("input_", ".html");
2024-10-14 22:34:41 +01:00
String sanitizedHtml =
sanitizeHtmlContent(new String(fileBytes, StandardCharsets.UTF_8));
Files.write(tempInputFile, sanitizedHtml.getBytes(StandardCharsets.UTF_8));
} else if (fileName.endsWith(".zip")) {
2024-02-09 23:24:25 +00:00
tempInputFile = Files.createTempFile("input_", ".zip");
Files.write(tempInputFile, fileBytes);
2024-10-14 22:34:41 +01:00
sanitizeHtmlFilesInZip(tempInputFile);
} else {
throw new IllegalArgumentException("Unsupported file format: " + fileName);
2023-08-01 00:03:13 +01:00
}
List<String> command = new ArrayList<>();
2024-01-09 22:39:21 +00:00
if (!htmlFormatsInstalled) {
command.add("weasyprint");
2024-10-14 22:34:41 +01:00
command.add("-e");
command.add("utf-8");
command.add("-v");
2024-02-09 23:24:25 +00:00
command.add(tempInputFile.toString());
command.add(tempOutputFile.toString());
2024-01-09 22:39:21 +00:00
} else {
2024-02-09 23:24:25 +00:00
command.add("ebook-convert");
command.add(tempInputFile.toString());
command.add(tempOutputFile.toString());
command.add("--paper-size");
command.add("a4");
2024-01-28 17:36:17 +00:00
2024-06-05 21:16:22 +01:00
if (request != null && request.getZoom() != 1.0) {
File tempCssFile = Files.createTempFile("customStyle", ".css").toFile();
2024-02-09 23:24:25 +00:00
try (FileWriter writer = new FileWriter(tempCssFile)) {
writer.write("body { zoom: " + request.getZoom() + "; }");
2024-01-28 17:36:17 +00:00
}
2024-02-09 23:24:25 +00:00
command.add("--extra-css");
command.add(tempCssFile.getAbsolutePath());
2024-01-28 17:36:17 +00:00
}
2024-01-09 22:39:21 +00:00
}
2024-01-10 00:33:07 +00:00
2024-10-14 22:34:41 +01:00
ProcessExecutorResult returnCode =
2024-02-09 23:24:25 +00:00
ProcessExecutor.getInstance(ProcessExecutor.Processes.WEASYPRINT)
.runCommandWithOutputHandling(command);
2023-08-01 00:03:13 +01:00
pdfBytes = Files.readAllBytes(tempOutputFile);
2024-01-18 23:28:39 +00:00
} catch (IOException e) {
pdfBytes = Files.readAllBytes(tempOutputFile);
if (pdfBytes.length < 1) {
throw e;
}
2023-08-01 00:03:13 +01:00
} finally {
2024-05-27 17:53:18 +01:00
Files.deleteIfExists(tempOutputFile);
Files.deleteIfExists(tempInputFile);
2023-12-30 19:11:27 +00:00
}
2023-08-01 00:03:13 +01:00
return pdfBytes;
2023-12-30 19:11:27 +00:00
}
2024-10-14 22:34:41 +01:00
private static String sanitizeHtmlContent(String htmlContent) {
return CustomHtmlSanitizer.sanitize(htmlContent);
}
private static void sanitizeHtmlFilesInZip(Path zipFilePath) throws IOException {
Path tempUnzippedDir = Files.createTempDirectory("unzipped_");
try (ZipInputStream zipIn =
ZipSecurity.createHardenedInputStream(
new ByteArrayInputStream(Files.readAllBytes(zipFilePath)))) {
ZipEntry entry = zipIn.getNextEntry();
while (entry != null) {
Path filePath = tempUnzippedDir.resolve(sanitizeZipFilename(entry.getName()));
2024-10-14 22:34:41 +01:00
if (!entry.isDirectory()) {
Files.createDirectories(filePath.getParent());
if (entry.getName().toLowerCase().endsWith(".html")
|| entry.getName().toLowerCase().endsWith(".htm")) {
String content = new String(zipIn.readAllBytes(), StandardCharsets.UTF_8);
String sanitizedContent = sanitizeHtmlContent(content);
Files.write(filePath, sanitizedContent.getBytes(StandardCharsets.UTF_8));
} else {
Files.copy(zipIn, filePath);
}
}
zipIn.closeEntry();
entry = zipIn.getNextEntry();
}
}
// Repack the sanitized files
zipDirectory(tempUnzippedDir, zipFilePath);
// Clean up
deleteDirectory(tempUnzippedDir);
}
private static void zipDirectory(Path sourceDir, Path zipFilePath) throws IOException {
try (ZipOutputStream zos =
new ZipOutputStream(new FileOutputStream(zipFilePath.toFile()))) {
Files.walk(sourceDir)
.filter(path -> !Files.isDirectory(path))
.forEach(
path -> {
ZipEntry zipEntry =
new ZipEntry(sourceDir.relativize(path).toString());
try {
zos.putNextEntry(zipEntry);
Files.copy(path, zos);
zos.closeEntry();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
});
}
}
private static void deleteDirectory(Path dir) throws IOException {
Files.walkFileTree(
dir,
new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
throws IOException {
Files.delete(file);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc)
throws IOException {
Files.delete(dir);
return FileVisitResult.CONTINUE;
}
});
}
2023-08-01 00:03:13 +01:00
private static Path unzipAndGetMainHtml(byte[] fileBytes) throws IOException {
Path tempDirectory = Files.createTempDirectory("unzipped_");
2024-02-06 00:00:49 +00:00
try (ZipInputStream zipIn =
ZipSecurity.createHardenedInputStream(new ByteArrayInputStream(fileBytes))) {
2023-08-01 00:03:13 +01:00
ZipEntry entry = zipIn.getNextEntry();
while (entry != null) {
Path filePath = tempDirectory.resolve(sanitizeZipFilename(entry.getName()));
2023-08-01 00:03:13 +01:00
if (entry.isDirectory()) {
Files.createDirectories(filePath); // Explicitly create the directory structure
2023-12-30 19:11:27 +00:00
} else {
2023-08-01 00:03:13 +01:00
Files.createDirectories(
filePath.getParent()); // Create parent directories if they don't exist
Files.copy(zipIn, filePath);
2023-12-30 19:11:27 +00:00
}
2023-08-01 00:03:13 +01:00
zipIn.closeEntry();
entry = zipIn.getNextEntry();
2023-12-30 19:11:27 +00:00
}
}
2023-08-01 00:03:13 +01:00
// search for the main HTML file.
try (Stream<Path> walk = Files.walk(tempDirectory)) {
List<Path> htmlFiles =
walk.filter(file -> file.toString().endsWith(".html"))
.collect(Collectors.toList());
2023-12-30 19:11:27 +00:00
2023-08-01 00:03:13 +01:00
if (htmlFiles.isEmpty()) {
throw new IOException("No HTML files found in the unzipped directory.");
2023-12-30 19:11:27 +00:00
}
2023-08-01 00:03:13 +01:00
// Prioritize 'index.html' if it exists, otherwise use the first .html file
for (Path htmlFile : htmlFiles) {
if ("index.html".equals(htmlFile.getFileName().toString())) {
2023-08-01 00:03:13 +01:00
return htmlFile;
2023-12-30 19:11:27 +00:00
}
}
2023-08-01 00:03:13 +01:00
return htmlFiles.get(0);
}
2023-12-30 19:11:27 +00:00
}
2024-01-09 22:39:21 +00:00
public static byte[] convertBookTypeToPdf(byte[] bytes, String originalFilename)
throws IOException, InterruptedException {
if (originalFilename == null || originalFilename.lastIndexOf('.') == -1) {
throw new IllegalArgumentException("Invalid original filename.");
}
String fileExtension = originalFilename.substring(originalFilename.lastIndexOf('.'));
List<String> command = new ArrayList<>();
Path tempOutputFile = Files.createTempFile("output_", ".pdf");
Path tempInputFile = null;
try {
// Create temp file with appropriate extension
tempInputFile = Files.createTempFile("input_", fileExtension);
Files.write(tempInputFile, bytes);
command.add("ebook-convert");
command.add(tempInputFile.toString());
command.add(tempOutputFile.toString());
ProcessExecutorResult returnCode =
ProcessExecutor.getInstance(ProcessExecutor.Processes.CALIBRE)
.runCommandWithOutputHandling(command);
return Files.readAllBytes(tempOutputFile);
} finally {
// Clean up temporary files
if (tempInputFile != null) {
Files.deleteIfExists(tempInputFile);
}
Files.deleteIfExists(tempOutputFile);
}
}
static String sanitizeZipFilename(String entryName) {
if (entryName == null || entryName.trim().isEmpty()) {
return entryName;
}
while (entryName.contains("../") || entryName.contains("..\\")) {
entryName = entryName.replace("../", "").replace("..\\", "");
}
return entryName;
}
2023-08-01 00:03:13 +01:00
}