mirror of
https://github.com/Frooodle/Stirling-PDF.git
synced 2025-09-08 17:51:20 +02:00
performance: Use StringBuilder instead of string concatenation for building strings (#4193)
This commit is contained in:
parent
47bce86ae2
commit
8192b1a44f
@ -552,10 +552,10 @@ public class PdfUtils {
|
|||||||
public boolean containsTextInFile(PDDocument pdfDocument, String text, String pagesToCheck)
|
public boolean containsTextInFile(PDDocument pdfDocument, String text, String pagesToCheck)
|
||||||
throws IOException {
|
throws IOException {
|
||||||
PDFTextStripper textStripper = new PDFTextStripper();
|
PDFTextStripper textStripper = new PDFTextStripper();
|
||||||
String pdfText = "";
|
StringBuilder pdfText = new StringBuilder();
|
||||||
|
|
||||||
if (pagesToCheck == null || "all".equals(pagesToCheck)) {
|
if (pagesToCheck == null || "all".equals(pagesToCheck)) {
|
||||||
pdfText = textStripper.getText(pdfDocument);
|
pdfText = new StringBuilder(textStripper.getText(pdfDocument));
|
||||||
} else {
|
} else {
|
||||||
// remove whitespaces
|
// remove whitespaces
|
||||||
pagesToCheck = pagesToCheck.replaceAll("\\s+", "");
|
pagesToCheck = pagesToCheck.replaceAll("\\s+", "");
|
||||||
@ -571,21 +571,21 @@ public class PdfUtils {
|
|||||||
for (int i = startPage; i <= endPage; i++) {
|
for (int i = startPage; i <= endPage; i++) {
|
||||||
textStripper.setStartPage(i);
|
textStripper.setStartPage(i);
|
||||||
textStripper.setEndPage(i);
|
textStripper.setEndPage(i);
|
||||||
pdfText += textStripper.getText(pdfDocument);
|
pdfText.append(textStripper.getText(pdfDocument));
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Handle individual page
|
// Handle individual page
|
||||||
int page = Integer.parseInt(splitPoint);
|
int page = Integer.parseInt(splitPoint);
|
||||||
textStripper.setStartPage(page);
|
textStripper.setStartPage(page);
|
||||||
textStripper.setEndPage(page);
|
textStripper.setEndPage(page);
|
||||||
pdfText += textStripper.getText(pdfDocument);
|
pdfText.append(textStripper.getText(pdfDocument));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pdfDocument.close();
|
pdfDocument.close();
|
||||||
|
|
||||||
return pdfText.contains(text);
|
return pdfText.toString().contains(text);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean pageCount(PDDocument pdfDocument, int pageCount, String comparator)
|
public boolean pageCount(PDDocument pdfDocument, int pageCount, String comparator)
|
||||||
|
@ -94,14 +94,14 @@ public class AutoRenameController {
|
|||||||
// Merge lines with same font size
|
// Merge lines with same font size
|
||||||
List<LineInfo> mergedLineInfos = new ArrayList<>();
|
List<LineInfo> mergedLineInfos = new ArrayList<>();
|
||||||
for (int i = 0; i < lineInfos.size(); i++) {
|
for (int i = 0; i < lineInfos.size(); i++) {
|
||||||
String mergedText = lineInfos.get(i).text;
|
StringBuilder mergedText = new StringBuilder(lineInfos.get(i).text);
|
||||||
float fontSize = lineInfos.get(i).fontSize;
|
float fontSize = lineInfos.get(i).fontSize;
|
||||||
while (i + 1 < lineInfos.size()
|
while (i + 1 < lineInfos.size()
|
||||||
&& lineInfos.get(i + 1).fontSize == fontSize) {
|
&& lineInfos.get(i + 1).fontSize == fontSize) {
|
||||||
mergedText += " " + lineInfos.get(i + 1).text;
|
mergedText.append(" ").append(lineInfos.get(i + 1).text);
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
mergedLineInfos.add(new LineInfo(mergedText, fontSize));
|
mergedLineInfos.add(new LineInfo(mergedText.toString(), fontSize));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sort lines by font size in descending order and get the first one
|
// Sort lines by font size in descending order and get the first one
|
||||||
|
@ -1,8 +1,9 @@
|
|||||||
package stirling.software.SPDF.controller.api.misc;
|
package stirling.software.SPDF.controller.api.misc;
|
||||||
|
|
||||||
import java.nio.charset.StandardCharsets;
|
import io.github.pixee.security.Filenames;
|
||||||
import java.util.Map;
|
import io.swagger.v3.oas.annotations.Operation;
|
||||||
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.apache.pdfbox.pdmodel.PDDocument;
|
import org.apache.pdfbox.pdmodel.PDDocument;
|
||||||
import org.apache.pdfbox.pdmodel.common.PDNameTreeNode;
|
import org.apache.pdfbox.pdmodel.common.PDNameTreeNode;
|
||||||
import org.apache.pdfbox.pdmodel.interactive.action.PDActionJavaScript;
|
import org.apache.pdfbox.pdmodel.interactive.action.PDActionJavaScript;
|
||||||
@ -13,17 +14,13 @@ import org.springframework.web.bind.annotation.PostMapping;
|
|||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
import io.github.pixee.security.Filenames;
|
|
||||||
import io.swagger.v3.oas.annotations.Operation;
|
|
||||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
|
||||||
|
|
||||||
import lombok.RequiredArgsConstructor;
|
|
||||||
|
|
||||||
import stirling.software.common.model.api.PDFFile;
|
import stirling.software.common.model.api.PDFFile;
|
||||||
import stirling.software.common.service.CustomPDFDocumentFactory;
|
import stirling.software.common.service.CustomPDFDocumentFactory;
|
||||||
import stirling.software.common.util.WebResponseUtils;
|
import stirling.software.common.util.WebResponseUtils;
|
||||||
|
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/api/v1/misc")
|
@RequestMapping("/api/v1/misc")
|
||||||
@Tag(name = "Misc", description = "Miscellaneous APIs")
|
@Tag(name = "Misc", description = "Miscellaneous APIs")
|
||||||
@ -38,7 +35,8 @@ public class ShowJavascript {
|
|||||||
description = "desc. Input:PDF Output:JS Type:SISO")
|
description = "desc. Input:PDF Output:JS Type:SISO")
|
||||||
public ResponseEntity<byte[]> extractHeader(@ModelAttribute PDFFile file) throws Exception {
|
public ResponseEntity<byte[]> extractHeader(@ModelAttribute PDFFile file) throws Exception {
|
||||||
MultipartFile inputFile = file.getFileInput();
|
MultipartFile inputFile = file.getFileInput();
|
||||||
String script = "";
|
StringBuilder script = new StringBuilder();
|
||||||
|
boolean foundScript = false;
|
||||||
|
|
||||||
try (PDDocument document = pdfDocumentFactory.load(inputFile)) {
|
try (PDDocument document = pdfDocumentFactory.load(inputFile)) {
|
||||||
|
|
||||||
@ -55,28 +53,28 @@ public class ShowJavascript {
|
|||||||
PDActionJavaScript jsAction = entry.getValue();
|
PDActionJavaScript jsAction = entry.getValue();
|
||||||
String jsCodeStr = jsAction.getAction();
|
String jsCodeStr = jsAction.getAction();
|
||||||
|
|
||||||
script +=
|
if (jsCodeStr != null && !jsCodeStr.trim().isEmpty()) {
|
||||||
"// File: "
|
script.append("// File: ")
|
||||||
+ Filenames.toSimpleFileName(
|
.append(Filenames.toSimpleFileName(inputFile.getOriginalFilename()))
|
||||||
inputFile.getOriginalFilename())
|
.append(", Script: ")
|
||||||
+ ", Script: "
|
.append(name)
|
||||||
+ name
|
.append("\n")
|
||||||
+ "\n"
|
.append(jsCodeStr)
|
||||||
+ jsCodeStr
|
.append("\n");
|
||||||
+ "\n";
|
foundScript = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (script.isEmpty()) {
|
if (!foundScript) {
|
||||||
script =
|
script = new StringBuilder("PDF '")
|
||||||
"PDF '"
|
.append(Filenames.toSimpleFileName(inputFile.getOriginalFilename()))
|
||||||
+ Filenames.toSimpleFileName(inputFile.getOriginalFilename())
|
.append("' does not contain Javascript");
|
||||||
+ "' does not contain Javascript";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return WebResponseUtils.bytesToWebResponse(
|
return WebResponseUtils.bytesToWebResponse(
|
||||||
script.getBytes(StandardCharsets.UTF_8),
|
script.toString().getBytes(StandardCharsets.UTF_8),
|
||||||
Filenames.toSimpleFileName(inputFile.getOriginalFilename()) + ".js",
|
Filenames.toSimpleFileName(inputFile.getOriginalFilename()) + ".js",
|
||||||
MediaType.TEXT_PLAIN);
|
MediaType.TEXT_PLAIN);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user