From ad5f057733238a172856d78a2ce49654155ad81c Mon Sep 17 00:00:00 2001 From: Anthony Stirling <77850077+Frooodle@users.noreply.github.com> Date: Sat, 12 Aug 2023 19:53:14 +0100 Subject: [PATCH] Fix for #306 --- .../api/security/PasswordController.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/main/java/stirling/software/SPDF/controller/api/security/PasswordController.java b/src/main/java/stirling/software/SPDF/controller/api/security/PasswordController.java index 909be730..bcc7e37f 100644 --- a/src/main/java/stirling/software/SPDF/controller/api/security/PasswordController.java +++ b/src/main/java/stirling/software/SPDF/controller/api/security/PasswordController.java @@ -52,13 +52,13 @@ public class PasswordController { @RequestPart(required = true, value = "fileInput") @Parameter(description = "The input PDF file to which the password should be added", required = true) MultipartFile fileInput, - @RequestParam(value = "", name = "ownerPassword") + @RequestParam(value = "", name = "ownerPassword", required = false, defaultValue = "") @Parameter(description = "The owner password to be added to the PDF file (Restricts what can be done with the document once it is opened)") String ownerPassword, - @RequestParam( name = "password", required = false) + @RequestParam( name = "password", required = false, defaultValue = "") @Parameter(description = "The password to be added to the PDF file (Restricts the opening of the document itself.)") String password, - @RequestParam( name = "keyLength", required = false) + @RequestParam( name = "keyLength", required = false, defaultValue = "256") @Parameter(description = "The length of the encryption key", schema = @Schema(allowableValues = {"40", "128", "256"})) int keyLength, @RequestParam( name = "canAssembleDocument", required = false) @@ -98,15 +98,15 @@ public class PasswordController { ap.setCanPrint(!canPrint); ap.setCanPrintFaithful(!canPrintFaithful); StandardProtectionPolicy spp = new StandardProtectionPolicy(ownerPassword, password, ap); - - - - spp.setEncryptionKeyLength(keyLength); + if(!"".equals(ownerPassword) || !"".equals(password)) { + spp.setEncryptionKeyLength(keyLength); + } spp.setPermissions(ap); - document.protect(spp); + if("".equals(ownerPassword) && "".equals(password)) + return WebResponseUtils.pdfDocToWebResponse(document, fileInput.getOriginalFilename().replaceFirst("[.][^.]+$", "") + "_permissions.pdf"); return WebResponseUtils.pdfDocToWebResponse(document, fileInput.getOriginalFilename().replaceFirst("[.][^.]+$", "") + "_passworded.pdf"); }