mirror of
https://github.com/Frooodle/Stirling-PDF.git
synced 2025-09-08 17:51:20 +02:00
Compare commits
4 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
a438a15105 | ||
|
8192b1a44f | ||
|
47bce86ae2 | ||
|
5e72dce0de |
@ -124,8 +124,8 @@ public class FileToPdf {
|
||||
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))
|
||||
try (Stream<Path> walk = Files.walk(sourceDir)) {
|
||||
walk.filter(path -> !Files.isDirectory(path))
|
||||
.forEach(
|
||||
path -> {
|
||||
ZipEntry zipEntry =
|
||||
@ -140,6 +140,7 @@ public class FileToPdf {
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void deleteDirectory(Path dir) throws IOException {
|
||||
Files.walkFileTree(
|
||||
|
@ -552,10 +552,10 @@ public class PdfUtils {
|
||||
public boolean containsTextInFile(PDDocument pdfDocument, String text, String pagesToCheck)
|
||||
throws IOException {
|
||||
PDFTextStripper textStripper = new PDFTextStripper();
|
||||
String pdfText = "";
|
||||
StringBuilder pdfText = new StringBuilder();
|
||||
|
||||
if (pagesToCheck == null || "all".equals(pagesToCheck)) {
|
||||
pdfText = textStripper.getText(pdfDocument);
|
||||
pdfText = new StringBuilder(textStripper.getText(pdfDocument));
|
||||
} else {
|
||||
// remove whitespaces
|
||||
pagesToCheck = pagesToCheck.replaceAll("\\s+", "");
|
||||
@ -571,21 +571,21 @@ public class PdfUtils {
|
||||
for (int i = startPage; i <= endPage; i++) {
|
||||
textStripper.setStartPage(i);
|
||||
textStripper.setEndPage(i);
|
||||
pdfText += textStripper.getText(pdfDocument);
|
||||
pdfText.append(textStripper.getText(pdfDocument));
|
||||
}
|
||||
} else {
|
||||
// Handle individual page
|
||||
int page = Integer.parseInt(splitPoint);
|
||||
textStripper.setStartPage(page);
|
||||
textStripper.setEndPage(page);
|
||||
pdfText += textStripper.getText(pdfDocument);
|
||||
pdfText.append(textStripper.getText(pdfDocument));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pdfDocument.close();
|
||||
|
||||
return pdfText.contains(text);
|
||||
return pdfText.toString().contains(text);
|
||||
}
|
||||
|
||||
public boolean pageCount(PDDocument pdfDocument, int pageCount, String comparator)
|
||||
|
@ -9,6 +9,7 @@ import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Stream;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipOutputStream;
|
||||
|
||||
@ -150,10 +151,11 @@ public class ConvertImgPDFController {
|
||||
.runCommandWithOutputHandling(command);
|
||||
|
||||
// Find all WebP files in the output directory
|
||||
List<Path> webpFiles =
|
||||
Files.walk(tempOutputDir)
|
||||
.filter(path -> path.toString().endsWith(".webp"))
|
||||
.toList();
|
||||
List<Path> webpFiles;
|
||||
try (Stream<Path> walkStream = Files.walk(tempOutputDir)) {
|
||||
webpFiles =
|
||||
walkStream.filter(path -> path.toString().endsWith(".webp")).toList();
|
||||
}
|
||||
|
||||
if (webpFiles.isEmpty()) {
|
||||
log.error("No WebP files were created in: {}", tempOutputDir.toString());
|
||||
|
@ -48,10 +48,12 @@ public class FilterController {
|
||||
String text = request.getText();
|
||||
String pageNumber = request.getPageNumbers();
|
||||
|
||||
PDDocument pdfDocument = pdfDocumentFactory.load(inputFile);
|
||||
if (PdfUtils.hasText(pdfDocument, pageNumber, text))
|
||||
try (PDDocument pdfDocument = pdfDocumentFactory.load(inputFile)) {
|
||||
if (PdfUtils.hasText(pdfDocument, pageNumber, text)) {
|
||||
return WebResponseUtils.pdfDocToWebResponse(
|
||||
pdfDocument, Filenames.toSimpleFileName(inputFile.getOriginalFilename()));
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
@ -94,14 +94,14 @@ public class AutoRenameController {
|
||||
// Merge lines with same font size
|
||||
List<LineInfo> mergedLineInfos = new ArrayList<>();
|
||||
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;
|
||||
while (i + 1 < lineInfos.size()
|
||||
&& lineInfos.get(i + 1).fontSize == fontSize) {
|
||||
mergedText += " " + lineInfos.get(i + 1).text;
|
||||
mergedText.append(" ").append(lineInfos.get(i + 1).text);
|
||||
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
|
||||
|
@ -8,6 +8,7 @@ import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Stream;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipOutputStream;
|
||||
|
||||
@ -142,7 +143,10 @@ public class ExtractImageScansController {
|
||||
.runCommandWithOutputHandling(command);
|
||||
|
||||
// Read the output photos in temp directory
|
||||
List<Path> tempOutputFiles = Files.list(tempDir).sorted().toList();
|
||||
List<Path> tempOutputFiles;
|
||||
try (Stream<Path> listStream = Files.list(tempDir)) {
|
||||
tempOutputFiles = listStream.sorted().toList();
|
||||
}
|
||||
for (Path tempOutputFile : tempOutputFiles) {
|
||||
byte[] imageBytes = Files.readAllBytes(tempOutputFile);
|
||||
processedImageBytes.add(imageBytes);
|
||||
|
@ -1,8 +1,9 @@
|
||||
package stirling.software.SPDF.controller.api.misc;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Map;
|
||||
|
||||
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 org.apache.pdfbox.pdmodel.PDDocument;
|
||||
import org.apache.pdfbox.pdmodel.common.PDNameTreeNode;
|
||||
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.RestController;
|
||||
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.service.CustomPDFDocumentFactory;
|
||||
import stirling.software.common.util.WebResponseUtils;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Map;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/v1/misc")
|
||||
@Tag(name = "Misc", description = "Miscellaneous APIs")
|
||||
@ -38,7 +35,8 @@ public class ShowJavascript {
|
||||
description = "desc. Input:PDF Output:JS Type:SISO")
|
||||
public ResponseEntity<byte[]> extractHeader(@ModelAttribute PDFFile file) throws Exception {
|
||||
MultipartFile inputFile = file.getFileInput();
|
||||
String script = "";
|
||||
StringBuilder script = new StringBuilder();
|
||||
boolean foundScript = false;
|
||||
|
||||
try (PDDocument document = pdfDocumentFactory.load(inputFile)) {
|
||||
|
||||
@ -55,28 +53,28 @@ public class ShowJavascript {
|
||||
PDActionJavaScript jsAction = entry.getValue();
|
||||
String jsCodeStr = jsAction.getAction();
|
||||
|
||||
script +=
|
||||
"// File: "
|
||||
+ Filenames.toSimpleFileName(
|
||||
inputFile.getOriginalFilename())
|
||||
+ ", Script: "
|
||||
+ name
|
||||
+ "\n"
|
||||
+ jsCodeStr
|
||||
+ "\n";
|
||||
if (jsCodeStr != null && !jsCodeStr.trim().isEmpty()) {
|
||||
script.append("// File: ")
|
||||
.append(Filenames.toSimpleFileName(inputFile.getOriginalFilename()))
|
||||
.append(", Script: ")
|
||||
.append(name)
|
||||
.append("\n")
|
||||
.append(jsCodeStr)
|
||||
.append("\n");
|
||||
foundScript = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (script.isEmpty()) {
|
||||
script =
|
||||
"PDF '"
|
||||
+ Filenames.toSimpleFileName(inputFile.getOriginalFilename())
|
||||
+ "' does not contain Javascript";
|
||||
if (!foundScript) {
|
||||
script = new StringBuilder("PDF '")
|
||||
.append(Filenames.toSimpleFileName(inputFile.getOriginalFilename()))
|
||||
.append("' does not contain Javascript");
|
||||
}
|
||||
|
||||
return WebResponseUtils.bytesToWebResponse(
|
||||
script.getBytes(StandardCharsets.UTF_8),
|
||||
script.toString().getBytes(StandardCharsets.UTF_8),
|
||||
Filenames.toSimpleFileName(inputFile.getOriginalFilename()) + ".js",
|
||||
MediaType.TEXT_PLAIN);
|
||||
}
|
||||
|
@ -7,6 +7,7 @@ import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.thymeleaf.util.StringUtils;
|
||||
@ -66,11 +67,12 @@ public class SignatureService {
|
||||
|
||||
private List<SignatureFile> getSignaturesFromFolder(Path folder, String category)
|
||||
throws IOException {
|
||||
return Files.list(folder)
|
||||
.filter(path -> isImageFile(path))
|
||||
try (Stream<Path> stream = Files.list(folder)) {
|
||||
return stream.filter(this::isImageFile)
|
||||
.map(path -> new SignatureFile(path.getFileName().toString(), category))
|
||||
.toList();
|
||||
}
|
||||
}
|
||||
|
||||
public byte[] getSignatureBytes(String username, String fileName) throws IOException {
|
||||
validateFileName(fileName);
|
||||
|
@ -1896,12 +1896,12 @@ editTableOfContents.replaceExisting=Meglévő könyvjelzők cseréje (törölje
|
||||
editTableOfContents.editorTitle=Könyvjelző szerkesztő
|
||||
editTableOfContents.editorDesc=Könyvjelzők hozzáadása és rendezése lent. Kattintson a + gombra gyermek könyvjelzők hozzáadásához.
|
||||
editTableOfContents.addBookmark=Új könyvjelző hozzáadása
|
||||
editTableOfContents.importBookmarksDefault=Import
|
||||
editTableOfContents.importBookmarksFromJsonFile=Upload JSON file
|
||||
editTableOfContents.importBookmarksFromClipboard=Paste from clipboard
|
||||
editTableOfContents.exportBookmarksDefault=Export
|
||||
editTableOfContents.exportBookmarksAsJson=Download as JSON
|
||||
editTableOfContents.exportBookmarksAsText=Copy as text
|
||||
editTableOfContents.importBookmarksDefault=Importálás
|
||||
editTableOfContents.importBookmarksFromJsonFile=JSON fájl feltöltése
|
||||
editTableOfContents.importBookmarksFromClipboard=Beillesztés vágólapról
|
||||
editTableOfContents.exportBookmarksDefault=Exportálás
|
||||
editTableOfContents.exportBookmarksAsJson=Letöltés JSON formátumban
|
||||
editTableOfContents.exportBookmarksAsText=Másolás szövegként
|
||||
editTableOfContents.desc.1=Ez az eszköz lehetővé teszi a tartalomjegyzék (könyvjelzők) hozzáadását vagy szerkesztését egy PDF dokumentumban.
|
||||
editTableOfContents.desc.2=Hierarchikus struktúrákat hozhat létre, ha gyermek könyvjelzőket ad a szülő könyvjelzőkhöz.
|
||||
editTableOfContents.desc.3=Minden könyvjelzőhöz szükséges egy cím és egy céloldalszám.
|
||||
|
@ -8,6 +8,7 @@ import org.springframework.stereotype.Service;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
import stirling.software.common.model.ApplicationProperties;
|
||||
import stirling.software.proprietary.security.database.repository.UserRepository;
|
||||
import stirling.software.proprietary.security.model.AuthenticationType;
|
||||
import stirling.software.proprietary.security.model.User;
|
||||
@ -20,6 +21,8 @@ public class CustomUserDetailsService implements UserDetailsService {
|
||||
|
||||
private final LoginAttemptService loginAttemptService;
|
||||
|
||||
private final ApplicationProperties.Security securityProperties;
|
||||
|
||||
@Override
|
||||
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
|
||||
User user =
|
||||
@ -35,12 +38,53 @@ public class CustomUserDetailsService implements UserDetailsService {
|
||||
"Your account has been locked due to too many failed login attempts.");
|
||||
}
|
||||
|
||||
// Handle legacy users without authenticationType (from versions < 1.3.0)
|
||||
String authTypeStr = user.getAuthenticationType();
|
||||
if (authTypeStr == null || authTypeStr.isEmpty()) {
|
||||
// Migrate legacy users by detecting authentication type based on password presence
|
||||
AuthenticationType detectedType;
|
||||
if (user.hasPassword()) {
|
||||
// Users with passwords are likely traditional web authentication users
|
||||
detectedType = AuthenticationType.WEB;
|
||||
} else {
|
||||
// Users without passwords are SSO users (OAuth2/SAML2/etc)
|
||||
// Choose the appropriate SSO type based on what's enabled
|
||||
detectedType = determinePreferredSSOType();
|
||||
}
|
||||
|
||||
authTypeStr = detectedType.name();
|
||||
// Update the user record to set the detected authentication type
|
||||
user.setAuthenticationType(detectedType);
|
||||
userRepository.save(user);
|
||||
}
|
||||
|
||||
AuthenticationType userAuthenticationType =
|
||||
AuthenticationType.valueOf(user.getAuthenticationType().toUpperCase());
|
||||
AuthenticationType.valueOf(authTypeStr.toUpperCase());
|
||||
if (!user.hasPassword() && userAuthenticationType == AuthenticationType.WEB) {
|
||||
throw new IllegalArgumentException("Password must not be null");
|
||||
}
|
||||
|
||||
return user;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines the preferred SSO authentication type based on what's enabled in the application
|
||||
* configuration.
|
||||
*
|
||||
* @return The preferred AuthenticationType for SSO users
|
||||
*/
|
||||
private AuthenticationType determinePreferredSSOType() {
|
||||
// Check what SSO types are enabled and prefer in order: OAUTH2 > SAML2 > fallback to OAUTH2
|
||||
boolean oauth2Enabled = securityProperties.getOauth2() != null && securityProperties.getOauth2().getEnabled();
|
||||
boolean saml2Enabled = securityProperties.getSaml2() != null && securityProperties.getSaml2().getEnabled();
|
||||
|
||||
if (oauth2Enabled) {
|
||||
return AuthenticationType.OAUTH2;
|
||||
} else if (saml2Enabled) {
|
||||
return AuthenticationType.SAML2;
|
||||
} else {
|
||||
// Fallback to OAUTH2 (better than deprecated SSO)
|
||||
return AuthenticationType.OAUTH2;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -65,7 +65,7 @@ repositories {
|
||||
|
||||
allprojects {
|
||||
group = 'stirling.software'
|
||||
version = '1.3.1'
|
||||
version = '1.3.2'
|
||||
|
||||
configurations.configureEach {
|
||||
exclude group: 'commons-logging', module: 'commons-logging'
|
||||
|
Loading…
Reference in New Issue
Block a user