Compare commits
1 Commits
Frooodle-p
...
update-3rd
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
28b424ea5a |
@@ -169,7 +169,7 @@ public class FileToPdf {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Search for the main HTML file.
|
// search for the main HTML file.
|
||||||
try (Stream<Path> walk = Files.walk(tempDirectory)) {
|
try (Stream<Path> walk = Files.walk(tempDirectory)) {
|
||||||
List<Path> htmlFiles =
|
List<Path> htmlFiles =
|
||||||
walk.filter(file -> file.toString().endsWith(".html"))
|
walk.filter(file -> file.toString().endsWith(".html"))
|
||||||
@@ -190,20 +190,46 @@ public class FileToPdf {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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) {
|
static String sanitizeZipFilename(String entryName) {
|
||||||
if (entryName == null || entryName.trim().isEmpty()) {
|
if (entryName == null || entryName.trim().isEmpty()) {
|
||||||
return "";
|
return entryName;
|
||||||
}
|
}
|
||||||
// Remove any drive letters (e.g., "C:\") and leading forward/backslashes
|
|
||||||
entryName = entryName.replaceAll("^[a-zA-Z]:[\\\\/]+", "");
|
|
||||||
entryName = entryName.replaceAll("^[\\\\/]+", "");
|
|
||||||
|
|
||||||
// Recursively remove path traversal sequences
|
|
||||||
while (entryName.contains("../") || entryName.contains("..\\")) {
|
while (entryName.contains("../") || entryName.contains("..\\")) {
|
||||||
entryName = entryName.replace("../", "").replace("..\\", "");
|
entryName = entryName.replace("../", "").replace("..\\", "");
|
||||||
}
|
}
|
||||||
// Normalize all backslashes to forward slashes
|
|
||||||
entryName = entryName.replaceAll("\\\\", "/");
|
|
||||||
return entryName;
|
return entryName;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,6 @@
|
|||||||
package stirling.software.SPDF.utils;
|
package stirling.software.SPDF.utils;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.*;
|
||||||
import java.io.File;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStreamReader;
|
|
||||||
import java.io.InterruptedIOException;
|
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -226,7 +222,7 @@ public class ProcessExecutor {
|
|||||||
boolean isQpdf =
|
boolean isQpdf =
|
||||||
command != null && !command.isEmpty() && command.get(0).contains("qpdf");
|
command != null && !command.isEmpty() && command.get(0).contains("qpdf");
|
||||||
|
|
||||||
if (!outputLines.isEmpty()) {
|
if (outputLines.size() > 0) {
|
||||||
String outputMessage = String.join("\n", outputLines);
|
String outputMessage = String.join("\n", outputLines);
|
||||||
messages += outputMessage;
|
messages += outputMessage;
|
||||||
if (!liveUpdates) {
|
if (!liveUpdates) {
|
||||||
@@ -234,7 +230,7 @@ public class ProcessExecutor {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!errorLines.isEmpty()) {
|
if (errorLines.size() > 0) {
|
||||||
String errorMessage = String.join("\n", errorLines);
|
String errorMessage = String.join("\n", errorLines);
|
||||||
messages += errorMessage;
|
messages += errorMessage;
|
||||||
if (!liveUpdates) {
|
if (!liveUpdates) {
|
||||||
|
|||||||
@@ -51,7 +51,6 @@ document.addEventListener('DOMContentLoaded', function () {
|
|||||||
const storedVersion = localStorage.getItem('surveyVersion');
|
const storedVersion = localStorage.getItem('surveyVersion');
|
||||||
if (storedVersion && storedVersion !== surveyVersion) {
|
if (storedVersion && storedVersion !== surveyVersion) {
|
||||||
localStorage.setItem('pageViews', '0');
|
localStorage.setItem('pageViews', '0');
|
||||||
localStorage.setItem('surveyVersion', surveyVersion));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let pageViews = parseInt(localStorage.getItem('pageViews') || '0');
|
let pageViews = parseInt(localStorage.getItem('pageViews') || '0');
|
||||||
|
|||||||
@@ -5,79 +5,31 @@ import stirling.software.SPDF.model.api.converters.HTMLToPdfRequest;
|
|||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
|
|
||||||
public class FileToPdfTest {
|
public class FileToPdfTest {
|
||||||
|
|
||||||
/**
|
|
||||||
* Test the HTML to PDF conversion.
|
|
||||||
* This test expects an IOException when an empty HTML input is provided.
|
|
||||||
*/
|
|
||||||
@Test
|
@Test
|
||||||
public void testConvertHtmlToPdf() {
|
public void testConvertHtmlToPdf() {
|
||||||
HTMLToPdfRequest request = new HTMLToPdfRequest();
|
HTMLToPdfRequest request = new HTMLToPdfRequest();
|
||||||
byte[] fileBytes = new byte[0]; // Sample file bytes (empty input)
|
byte[] fileBytes = new byte[0]; // Sample file bytes
|
||||||
String fileName = "test.html"; // Sample file name indicating an HTML file
|
String fileName = "test.html"; // Sample file name
|
||||||
boolean disableSanitize = false; // Flag to control sanitization
|
boolean disableSanitize = false; // Sample boolean value
|
||||||
|
|
||||||
// Expect an IOException to be thrown due to empty input
|
// Check if the method throws IOException
|
||||||
Throwable thrown =
|
assertThrows(IOException.class, () -> {
|
||||||
assertThrows(
|
FileToPdf.convertHtmlToPdf("/path/",request, fileBytes, fileName, disableSanitize);
|
||||||
IOException.class,
|
});
|
||||||
() ->
|
|
||||||
FileToPdf.convertHtmlToPdf(
|
|
||||||
"/path/", request, fileBytes, fileName, disableSanitize));
|
|
||||||
assertNotNull(thrown);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Test sanitizeZipFilename with null or empty input.
|
|
||||||
* It should return an empty string in these cases.
|
|
||||||
*/
|
|
||||||
@Test
|
@Test
|
||||||
public void testSanitizeZipFilename_NullOrEmpty() {
|
public void testConvertBookTypeToPdf() {
|
||||||
assertEquals("", FileToPdf.sanitizeZipFilename(null));
|
byte[] bytes = new byte[10]; // Sample bytes
|
||||||
assertEquals("", FileToPdf.sanitizeZipFilename(" "));
|
String originalFilename = "test.epub"; // Sample original filename
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
// Check if the method throws IOException
|
||||||
* Test sanitizeZipFilename to ensure it removes path traversal sequences.
|
assertThrows(IOException.class, () -> {
|
||||||
* This includes removing both forward and backward slash sequences.
|
FileToPdf.convertBookTypeToPdf(bytes, originalFilename);
|
||||||
*/
|
});
|
||||||
@Test
|
|
||||||
public void testSanitizeZipFilename_RemovesTraversalSequences() {
|
|
||||||
String input = "../some/../path/..\\to\\file.txt";
|
|
||||||
String expected = "some/path/to/file.txt";
|
|
||||||
|
|
||||||
// Print output for debugging purposes
|
|
||||||
System.out.println("sanitizeZipFilename " + FileToPdf.sanitizeZipFilename(input));
|
|
||||||
System.out.flush();
|
|
||||||
|
|
||||||
// Expect that the method replaces backslashes with forward slashes
|
|
||||||
// and removes path traversal sequences
|
|
||||||
assertEquals(expected, FileToPdf.sanitizeZipFilename(input));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Test sanitizeZipFilename to ensure that it removes leading drive letters and slashes.
|
|
||||||
*/
|
|
||||||
@Test
|
|
||||||
public void testSanitizeZipFilename_RemovesLeadingDriveAndSlashes() {
|
|
||||||
String input = "C:\\folder\\file.txt";
|
|
||||||
String expected = "folder/file.txt";
|
|
||||||
assertEquals(expected, FileToPdf.sanitizeZipFilename(input));
|
|
||||||
|
|
||||||
input = "/folder/file.txt";
|
|
||||||
expected = "folder/file.txt";
|
|
||||||
assertEquals(expected, FileToPdf.sanitizeZipFilename(input));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Test sanitizeZipFilename to verify that safe filenames remain unchanged.
|
|
||||||
*/
|
|
||||||
@Test
|
|
||||||
public void testSanitizeZipFilename_NoChangeForSafeNames() {
|
|
||||||
String input = "folder/subfolder/file.txt";
|
|
||||||
assertEquals(input, FileToPdf.sanitizeZipFilename(input));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user