Compare commits

..

4 Commits

Author SHA1 Message Date
Dario Ghunney Ware
da767eccbf fixing access to attachments 2025-06-17 13:25:18 +01:00
Dario Ghunney Ware
279dc3f912 wip - working on opening attachments in a viewer 2025-06-16 16:39:59 +01:00
DarioGii
a0b176b076 wip - fixing embed feature 2025-06-13 14:59:54 +01:00
Dario Ghunney Ware
0f5c549fb4 fixing routing to page 2025-06-12 18:16:45 +01:00
50 changed files with 16 additions and 928 deletions

View File

@ -27,7 +27,6 @@ import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDDocumentNameDictionary;
import org.apache.pdfbox.pdmodel.PDEmbeddedFilesNameTreeNode;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PageMode;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.apache.pdfbox.pdmodel.common.filespecification.PDComplexFileSpecification;
import org.apache.pdfbox.pdmodel.common.filespecification.PDEmbeddedFile;
@ -1134,7 +1133,7 @@ public class EmlToPdf {
efTree.setNames(efMap);
// Set catalog viewer preferences to automatically show attachments pane
setCatalogViewerPreferences(document, PageMode.USE_ATTACHMENTS);
setCatalogViewerPreferences(document);
}
// Add attachment annotations to the first page for each embedded file

View File

@ -10,7 +10,7 @@ import org.apache.pdfbox.pdmodel.PageMode;
@Slf4j
public class PDFAttachmentUtils {
public static void setCatalogViewerPreferences(PDDocument document, PageMode pageMode) {
public static void setCatalogViewerPreferences(PDDocument document) {
try {
PDDocumentCatalog catalog = document.getDocumentCatalog();
if (catalog != null) {
@ -19,8 +19,7 @@ public class PDFAttachmentUtils {
// Set PageMode to UseAttachments - this is the standard PDF specification approach
// PageMode values: UseNone, UseOutlines, UseThumbs, FullScreen, UseOC, UseAttachments
catalog.setPageMode(pageMode);
catalogDict.setName(COSName.PAGE_MODE, pageMode.stringValue());
catalogDict.setName(COSName.PAGE_MODE, PageMode.USE_ATTACHMENTS.stringValue());
// Also set viewer preferences for better attachment viewing experience
COSDictionary viewerPrefs = (COSDictionary) catalogDict.getDictionaryObject(COSName.VIEWER_PREFERENCES);
@ -30,7 +29,7 @@ public class PDFAttachmentUtils {
}
// Set NonFullScreenPageMode to UseAttachments as fallback for viewers that support it
viewerPrefs.setName(COSName.getPDFName("NonFullScreenPageMode"), pageMode.stringValue());
viewerPrefs.setName(COSName.getPDFName("NonFullScreenPageMode"), PageMode.USE_ATTACHMENTS.stringValue());
// Additional viewer preferences that may help with attachment display
viewerPrefs.setBoolean(COSName.getPDFName("DisplayDocTitle"), true);

View File

@ -61,6 +61,7 @@ public class WebResponseUtils {
// Open Byte Array and save document to it
ByteArrayOutputStream baos = new ByteArrayOutputStream();
document.save(baos);
// Close the document
document.close();
return baosToWebResponse(baos, docName);

View File

@ -62,6 +62,9 @@ public class AttachmentsController {
catalog.setNames(documentNames);
pdfAttachmentService.addAttachment(document, embeddedFilesTree, attachments);
// Set PageMode to UseAttachments to show the attachments panel
catalog.setPageMode(PageMode.USE_ATTACHMENTS);
return WebResponseUtils.pdfDocToWebResponse(
document,
Filenames.toSimpleFileName(pdfFile.getOriginalFilename())

View File

@ -195,7 +195,7 @@ public class OtherWebController {
@GetMapping("/add-attachments")
@Hidden
public String attachmentsForm(Model model) {
model.addAttribute("currentPage", "add-attachments");
model.addAttribute("currentPage", "attachments");
return "misc/add-attachments";
}
}

View File

@ -9,7 +9,6 @@ import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDEmbeddedFilesNameTreeNode;
import org.apache.pdfbox.pdmodel.PageMode;
import org.apache.pdfbox.pdmodel.common.filespecification.PDComplexFileSpecification;
import org.apache.pdfbox.pdmodel.common.filespecification.PDEmbeddedFile;
import org.apache.pdfbox.pdmodel.encryption.AccessPermission;
@ -92,8 +91,9 @@ public class PDFAttachmentService implements PDFAttachmentServiceInterface {
embeddedFilesTree.setNames(existingNames);
// Ensure document has proper access permissions for embedded files
grantAccessPermissions(document);
PDFAttachmentUtils.setCatalogViewerPreferences(document, PageMode.USE_ATTACHMENTS);
PDFAttachmentUtils.setCatalogViewerPreferences(document);
}
private void grantAccessPermissions(PDDocument document) {

View File

@ -471,10 +471,6 @@ home.addImage.title=????? ???? ??? ??? PDF
home.addImage.desc=????? ???? ??? ???? ???? ?? PDF (????? ??? ??????)
addImage.tags=????,jpg,????,???? ??????????
home.attachments.title=Add Attachments
home.attachments.desc=Add or remove embedded files (attachments) to/from a PDF
attachments.tags=embed,attach,file,attachment,attachments
home.watermark.title=????? ????? ?????
home.watermark.desc=??? ????? ????? ????? ??? ????? PDF ????? ??.
watermark.tags=??,?????,?????,???,???? ?????,????? ??????,????,jpg,????,???? ??????????
@ -1646,17 +1642,3 @@ fakeScan.blur=Blur
fakeScan.noise=Noise
fakeScan.yellowish=Yellowish (simulate old paper)
fakeScan.resolution=Resolution (DPI)
#attachments
home.attachments.title=Add Attachments
home.attachments.desc=Add or remove embedded files (attachments) to/from a PDF
attachments.tags=embed,attach,file,attachment,attachments
attachments.title=Attachments
attachments.header=Add attachments
attachments.removeHeader=Remove attachments from PDF
attachments.selectFiles=Select files to attach
attachments.description=Allows you to add attachments to the PDF
attachments.descriptionPlaceholder=Enter a description for the attachments...
attachments.addButton=Add Attachments
attachments.removeDescription=This will remove all embedded files from the PDF.
attachments.removeButton=Remove All Attachments

View File

@ -1642,17 +1642,3 @@ fakeScan.blur=Blur
fakeScan.noise=Noise
fakeScan.yellowish=Yellowish (simulate old paper)
fakeScan.resolution=Resolution (DPI)
#attachments
home.attachments.title=Add Attachments
home.attachments.desc=Add or remove embedded files (attachments) to/from a PDF
attachments.tags=embed,attach,file,attachment,attachments
attachments.title=Attachments
attachments.header=Add attachments
attachments.removeHeader=Remove attachments from PDF
attachments.selectFiles=Select files to attach
attachments.description=Allows you to add attachments to the PDF
attachments.descriptionPlaceholder=Enter a description for the attachments...
attachments.addButton=Add Attachments
attachments.removeDescription=This will remove all embedded files from the PDF.
attachments.removeButton=Remove All Attachments

View File

@ -1642,17 +1642,3 @@ fakeScan.blur=Blur
fakeScan.noise=Noise
fakeScan.yellowish=Yellowish (simulate old paper)
fakeScan.resolution=Resolution (DPI)
#attachments
home.attachments.title=Add Attachments
home.attachments.desc=Add or remove embedded files (attachments) to/from a PDF
attachments.tags=embed,attach,file,attachment,attachments
attachments.title=Attachments
attachments.header=Add attachments
attachments.removeHeader=Remove attachments from PDF
attachments.selectFiles=Select files to attach
attachments.description=Allows you to add attachments to the PDF
attachments.descriptionPlaceholder=Enter a description for the attachments...
attachments.addButton=Add Attachments
attachments.removeDescription=This will remove all embedded files from the PDF.
attachments.removeButton=Remove All Attachments

View File

@ -1642,17 +1642,3 @@ fakeScan.blur=Blur
fakeScan.noise=Noise
fakeScan.yellowish=Yellowish (simulate old paper)
fakeScan.resolution=Resolution (DPI)
#attachments
home.attachments.title=Add Attachments
home.attachments.desc=Add or remove embedded files (attachments) to/from a PDF
attachments.tags=embed,attach,file,attachment,attachments
attachments.title=Attachments
attachments.header=Add attachments
attachments.removeHeader=Remove attachments from PDF
attachments.selectFiles=Select files to attach
attachments.description=Allows you to add attachments to the PDF
attachments.descriptionPlaceholder=Enter a description for the attachments...
attachments.addButton=Add Attachments
attachments.removeDescription=This will remove all embedded files from the PDF.
attachments.removeButton=Remove All Attachments

View File

@ -1642,17 +1642,3 @@ fakeScan.blur=Blur
fakeScan.noise=Noise
fakeScan.yellowish=Yellowish (simulate old paper)
fakeScan.resolution=Resolution (DPI)
#attachments
home.attachments.title=Add Attachments
home.attachments.desc=Add or remove embedded files (attachments) to/from a PDF
attachments.tags=embed,attach,file,attachment,attachments
attachments.title=Attachments
attachments.header=Add attachments
attachments.removeHeader=Remove attachments from PDF
attachments.selectFiles=Select files to attach
attachments.description=Allows you to add attachments to the PDF
attachments.descriptionPlaceholder=Enter a description for the attachments...
attachments.addButton=Add Attachments
attachments.removeDescription=This will remove all embedded files from the PDF.
attachments.removeButton=Remove All Attachments

View File

@ -1642,16 +1642,3 @@ fakeScan.blur=Blur
fakeScan.noise=Noise
fakeScan.yellowish=Yellowish (simulate old paper)
fakeScan.resolution=Resolution (DPI)
#attachments
home.attachments.title=Add Attachments
home.attachments.desc=Add or remove embedded files (attachments) to/from a PDF
attachments.tags=embed,attach,file,attachment,attachments
attachments.title=Attachments
attachments.header=Add attachments
attachments.removeHeader=Remove attachments from PDF
attachments.selectFiles=Select files to attach
attachments.description=Allows you to add attachments to the PDF
attachments.descriptionPlaceholder=Enter a description for the attachments...
attachments.addButton=Add Attachments
attachments.removeDescription=This will remove all embedded files from the PDF.
attachments.removeButton=Remove All Attachments

View File

@ -1642,17 +1642,3 @@ fakeScan.blur=Blur
fakeScan.noise=Noise
fakeScan.yellowish=Yellowish (simulate old paper)
fakeScan.resolution=Resolution (DPI)
#attachments
home.attachments.title=Add Attachments
home.attachments.desc=Add or remove embedded files (attachments) to/from a PDF
attachments.tags=embed,attach,file,attachment,attachments
attachments.title=Attachments
attachments.header=Add attachments
attachments.removeHeader=Remove attachments from PDF
attachments.selectFiles=Select files to attach
attachments.description=Allows you to add attachments to the PDF
attachments.descriptionPlaceholder=Enter a description for the attachments...
attachments.addButton=Add Attachments
attachments.removeDescription=This will remove all embedded files from the PDF.
attachments.removeButton=Remove All Attachments

View File

@ -1642,17 +1642,3 @@ fakeScan.blur=Verwischen
fakeScan.noise=Rauschen
fakeScan.yellowish=Gelblich (simulieren Sie altes Papier)
fakeScan.resolution=Auflösung (DPI)
#attachments
home.attachments.title=Add Attachments
home.attachments.desc=Add or remove embedded files (attachments) to/from a PDF
attachments.tags=embed,attach,file,attachment,attachments
attachments.title=Attachments
attachments.header=Add attachments
attachments.removeHeader=Remove attachments from PDF
attachments.selectFiles=Select files to attach
attachments.description=Allows you to add attachments to the PDF
attachments.descriptionPlaceholder=Enter a description for the attachments...
attachments.addButton=Add Attachments
attachments.removeDescription=This will remove all embedded files from the PDF.
attachments.removeButton=Remove All Attachments

View File

@ -1642,17 +1642,3 @@ fakeScan.blur=Blur
fakeScan.noise=Noise
fakeScan.yellowish=Yellowish (simulate old paper)
fakeScan.resolution=Resolution (DPI)
#attachments
home.attachments.title=Add Attachments
home.attachments.desc=Add or remove embedded files (attachments) to/from a PDF
attachments.tags=embed,attach,file,attachment,attachments
attachments.title=Attachments
attachments.header=Add attachments
attachments.removeHeader=Remove attachments from PDF
attachments.selectFiles=Select files to attach
attachments.description=Allows you to add attachments to the PDF
attachments.descriptionPlaceholder=Enter a description for the attachments...
attachments.addButton=Add Attachments
attachments.removeDescription=This will remove all embedded files from the PDF.
attachments.removeButton=Remove All Attachments

View File

@ -513,7 +513,7 @@ home.addImage.title=Add image
home.addImage.desc=Adds a image onto a set location on the PDF
addImage.tags=img,jpg,picture,photo
home.attachments.title=Add Attachments
home.attachments.title=Attachments
home.attachments.desc=Add or remove embedded files (attachments) to/from a PDF
attachments.tags=embed,attach,file,attachment,attachments
@ -1198,8 +1198,8 @@ addImage.upload=Add image
addImage.submit=Add image
#attachments
attachments.title=Add Attachments
attachments.header=Add attachments
attachments.title=Attachments
attachments.header=Add attachments to PDF
attachments.removeHeader=Remove attachments from PDF
attachments.selectFiles=Select files to attach
attachments.description=Allows you to add attachments to the PDF

View File

@ -472,7 +472,7 @@ home.addImage.title=Add image
home.addImage.desc=Adds a image onto a set location on the PDF
addImage.tags=img,jpg,picture,photo
home.attachments.title=Add Attachments
home.attachments.title=Attachments
home.attachments.desc=Add or remove embedded files (attachments) to/from a PDF
attachments.tags=embed,attach,file,attachment,attachments
@ -1158,7 +1158,7 @@ addImage.submit=Add image
#attachments
attachments.title=Attachments
attachments.header=Add attachments
attachments.header=Add attachments to PDF
attachments.removeHeader=Remove attachments from PDF
attachments.selectFiles=Select files to attach
attachments.description=Allows you to add attachments to the PDF

View File

@ -1642,17 +1642,3 @@ fakeScan.blur=Blur
fakeScan.noise=Noise
fakeScan.yellowish=Yellowish (simulate old paper)
fakeScan.resolution=Resolution (DPI)
#attachments
home.attachments.title=Add Attachments
home.attachments.desc=Add or remove embedded files (attachments) to/from a PDF
attachments.tags=embed,attach,file,attachment,attachments
attachments.title=Attachments
attachments.header=Add attachments
attachments.removeHeader=Remove attachments from PDF
attachments.selectFiles=Select files to attach
attachments.description=Allows you to add attachments to the PDF
attachments.descriptionPlaceholder=Enter a description for the attachments...
attachments.addButton=Add Attachments
attachments.removeDescription=This will remove all embedded files from the PDF.
attachments.removeButton=Remove All Attachments

View File

@ -1642,16 +1642,3 @@ fakeScan.blur=Blur
fakeScan.noise=Noise
fakeScan.yellowish=Yellowish (simulate old paper)
fakeScan.resolution=Resolution (DPI)
#attachments
home.attachments.title=Add Attachments
home.attachments.desc=Add or remove embedded files (attachments) to/from a PDF
attachments.tags=embed,attach,file,attachment,attachments
attachments.title=Attachments
attachments.header=Add attachments
attachments.removeHeader=Remove attachments from PDF
attachments.selectFiles=Select files to attach
attachments.description=Allows you to add attachments to the PDF
attachments.descriptionPlaceholder=Enter a description for the attachments...
attachments.addButton=Add Attachments
attachments.removeDescription=This will remove all embedded files from the PDF.
attachments.removeButton=Remove All Attachments

View File

@ -1642,17 +1642,3 @@ fakeScan.blur=Blur
fakeScan.noise=Noise
fakeScan.yellowish=Yellowish (simulate old paper)
fakeScan.resolution=Resolution (DPI)
#attachments
home.attachments.title=Add Attachments
home.attachments.desc=Add or remove embedded files (attachments) to/from a PDF
attachments.tags=embed,attach,file,attachment,attachments
attachments.title=Attachments
attachments.header=Add attachments
attachments.removeHeader=Remove attachments from PDF
attachments.selectFiles=Select files to attach
attachments.description=Allows you to add attachments to the PDF
attachments.descriptionPlaceholder=Enter a description for the attachments...
attachments.addButton=Add Attachments
attachments.removeDescription=This will remove all embedded files from the PDF.
attachments.removeButton=Remove All Attachments

View File

@ -1642,17 +1642,3 @@ fakeScan.blur=Blur
fakeScan.noise=Noise
fakeScan.yellowish=Yellowish (simulate old paper)
fakeScan.resolution=Resolution (DPI)
#attachments
home.attachments.title=Add Attachments
home.attachments.desc=Add or remove embedded files (attachments) to/from a PDF
attachments.tags=embed,attach,file,attachment,attachments
attachments.title=Attachments
attachments.header=Add attachments
attachments.removeHeader=Remove attachments from PDF
attachments.selectFiles=Select files to attach
attachments.description=Allows you to add attachments to the PDF
attachments.descriptionPlaceholder=Enter a description for the attachments...
attachments.addButton=Add Attachments
attachments.removeDescription=This will remove all embedded files from the PDF.
attachments.removeButton=Remove All Attachments

View File

@ -1642,17 +1642,3 @@ fakeScan.blur=Blur
fakeScan.noise=Noise
fakeScan.yellowish=Yellowish (simulate old paper)
fakeScan.resolution=Resolution (DPI)
#attachments
home.attachments.title=Add Attachments
home.attachments.desc=Add or remove embedded files (attachments) to/from a PDF
attachments.tags=embed,attach,file,attachment,attachments
attachments.title=Attachments
attachments.header=Add attachments
attachments.removeHeader=Remove attachments from PDF
attachments.selectFiles=Select files to attach
attachments.description=Allows you to add attachments to the PDF
attachments.descriptionPlaceholder=Enter a description for the attachments...
attachments.addButton=Add Attachments
attachments.removeDescription=This will remove all embedded files from the PDF.
attachments.removeButton=Remove All Attachments

View File

@ -1642,17 +1642,3 @@ fakeScan.blur=Blur
fakeScan.noise=Noise
fakeScan.yellowish=Yellowish (simulate old paper)
fakeScan.resolution=Resolution (DPI)
#attachments
home.attachments.title=Add Attachments
home.attachments.desc=Add or remove embedded files (attachments) to/from a PDF
attachments.tags=embed,attach,file,attachment,attachments
attachments.title=Attachments
attachments.header=Add attachments
attachments.removeHeader=Remove attachments from PDF
attachments.selectFiles=Select files to attach
attachments.description=Allows you to add attachments to the PDF
attachments.descriptionPlaceholder=Enter a description for the attachments...
attachments.addButton=Add Attachments
attachments.removeDescription=This will remove all embedded files from the PDF.
attachments.removeButton=Remove All Attachments

View File

@ -1642,17 +1642,3 @@ fakeScan.blur=Blur
fakeScan.noise=Noise
fakeScan.yellowish=Yellowish (simulate old paper)
fakeScan.resolution=Resolution (DPI)
#attachments
home.attachments.title=Add Attachments
home.attachments.desc=Add or remove embedded files (attachments) to/from a PDF
attachments.tags=embed,attach,file,attachment,attachments
attachments.title=Attachments
attachments.header=Add attachments
attachments.removeHeader=Remove attachments from PDF
attachments.selectFiles=Select files to attach
attachments.description=Allows you to add attachments to the PDF
attachments.descriptionPlaceholder=Enter a description for the attachments...
attachments.addButton=Add Attachments
attachments.removeDescription=This will remove all embedded files from the PDF.
attachments.removeButton=Remove All Attachments

View File

@ -1642,18 +1642,3 @@ fakeScan.blur=Elmos
fakeScan.noise=Zaj
fakeScan.yellowish=Sárgás (régi papír szimulálása)
fakeScan.resolution=Felbontás (DPI)
#attachments
home.attachments.title=Add Attachments
home.attachments.desc=Add or remove embedded files (attachments) to/from a PDF
attachments.tags=embed,attach,file,attachment,attachments
attachments.title=Attachments
attachments.header=Add attachments
attachments.removeHeader=Remove attachments from PDF
attachments.selectFiles=Select files to attach
attachments.description=Allows you to add attachments to the PDF
attachments.descriptionPlaceholder=Enter a description for the attachments...
attachments.addButton=Add Attachments
attachments.removeDescription=This will remove all embedded files from the PDF.
attachments.removeButton=Remove All Attachments

View File

@ -1642,18 +1642,3 @@ fakeScan.blur=Blur
fakeScan.noise=Noise
fakeScan.yellowish=Yellowish (simulate old paper)
fakeScan.resolution=Resolution (DPI)
#attachments
home.attachments.title=Add Attachments
home.attachments.desc=Add or remove embedded files (attachments) to/from a PDF
attachments.tags=embed,attach,file,attachment,attachments
attachments.title=Attachments
attachments.header=Add attachments
attachments.removeHeader=Remove attachments from PDF
attachments.selectFiles=Select files to attach
attachments.description=Allows you to add attachments to the PDF
attachments.descriptionPlaceholder=Enter a description for the attachments...
attachments.addButton=Add Attachments
attachments.removeDescription=This will remove all embedded files from the PDF.
attachments.removeButton=Remove All Attachments

View File

@ -1642,18 +1642,3 @@ fakeScan.blur=Sfocatura
fakeScan.noise=Rumore
fakeScan.yellowish=Giallastro (simula carta vecchia)
fakeScan.resolution=Risoluzione (DPI)
#attachments
home.attachments.title=Add Attachments
home.attachments.desc=Add or remove embedded files (attachments) to/from a PDF
attachments.tags=embed,attach,file,attachment,attachments
attachments.title=Attachments
attachments.header=Add attachments
attachments.removeHeader=Remove attachments from PDF
attachments.selectFiles=Select files to attach
attachments.description=Allows you to add attachments to the PDF
attachments.descriptionPlaceholder=Enter a description for the attachments...
attachments.addButton=Add Attachments
attachments.removeDescription=This will remove all embedded files from the PDF.
attachments.removeButton=Remove All Attachments

View File

@ -1642,18 +1642,3 @@ fakeScan.blur=Blur
fakeScan.noise=Noise
fakeScan.yellowish=Yellowish (simulate old paper)
fakeScan.resolution=Resolution (DPI)
#attachments
home.attachments.title=Add Attachments
home.attachments.desc=Add or remove embedded files (attachments) to/from a PDF
attachments.tags=embed,attach,file,attachment,attachments
attachments.title=Attachments
attachments.header=Add attachments
attachments.removeHeader=Remove attachments from PDF
attachments.selectFiles=Select files to attach
attachments.description=Allows you to add attachments to the PDF
attachments.descriptionPlaceholder=Enter a description for the attachments...
attachments.addButton=Add Attachments
attachments.removeDescription=This will remove all embedded files from the PDF.
attachments.removeButton=Remove All Attachments

View File

@ -1642,18 +1642,3 @@ fakeScan.blur=Blur
fakeScan.noise=Noise
fakeScan.yellowish=Yellowish (simulate old paper)
fakeScan.resolution=Resolution (DPI)
#attachments
home.attachments.title=Add Attachments
home.attachments.desc=Add or remove embedded files (attachments) to/from a PDF
attachments.tags=embed,attach,file,attachment,attachments
attachments.title=Attachments
attachments.header=Add attachments
attachments.removeHeader=Remove attachments from PDF
attachments.selectFiles=Select files to attach
attachments.description=Allows you to add attachments to the PDF
attachments.descriptionPlaceholder=Enter a description for the attachments...
attachments.addButton=Add Attachments
attachments.removeDescription=This will remove all embedded files from the PDF.
attachments.removeButton=Remove All Attachments

View File

@ -1642,18 +1642,3 @@ fakeScan.blur=Blur
fakeScan.noise=Noise
fakeScan.yellowish=Yellowish (simulate old paper)
fakeScan.resolution=Resolution (DPI)
#attachments
home.attachments.title=Add Attachments
home.attachments.desc=Add or remove embedded files (attachments) to/from a PDF
attachments.tags=embed,attach,file,attachment,attachments
attachments.title=Attachments
attachments.header=Add attachments
attachments.removeHeader=Remove attachments from PDF
attachments.selectFiles=Select files to attach
attachments.description=Allows you to add attachments to the PDF
attachments.descriptionPlaceholder=Enter a description for the attachments...
attachments.addButton=Add Attachments
attachments.removeDescription=This will remove all embedded files from the PDF.
attachments.removeButton=Remove All Attachments

View File

@ -1475,17 +1475,3 @@ cookieBanner.preferencesModal.necessary.description=വെബ്സൈറ്റ
cookieBanner.preferencesModal.analytics.title=അനലിറ്റിക്സ്
cookieBanner.preferencesModal.analytics.description=ഞങ്ങളുടെ ടൂളുകൾ എങ്ങനെ ഉപയോഗിക്കുന്നുവെന്ന് മനസ്സിലാക്കാൻ ഈ കുക്കികൾ ഞങ്ങളെ സഹായിക്കുന്നു, അതിനാൽ ഞങ്ങളുടെ കമ്മ്യൂണിറ്റി ഏറ്റവും കൂടുതൽ വിലമതിക്കുന്ന ഫീച്ചറുകൾ നിർമ്മിക്കുന്നതിൽ ഞങ്ങൾക്ക് ശ്രദ്ധ കേന്ദ്രീകരിക്കാൻ കഴിയും. ഉറപ്പാക്കുക—സ്റ്റെർലിംഗ് PDF-ന് നിങ്ങൾ പ്രവർത്തിക്കുന്ന പ്രമാണങ്ങളുടെ ഉള്ളടക്കം ട്രാക്ക് ചെയ്യാൻ കഴിയില്ല, ഒരിക്കലും കഴിയില്ല.
#attachments
home.attachments.title=Add Attachments
home.attachments.desc=Add or remove embedded files (attachments) to/from a PDF
attachments.tags=embed,attach,file,attachment,attachments
attachments.title=Attachments
attachments.header=Add attachments
attachments.removeHeader=Remove attachments from PDF
attachments.selectFiles=Select files to attach
attachments.description=Allows you to add attachments to the PDF
attachments.descriptionPlaceholder=Enter a description for the attachments...
attachments.addButton=Add Attachments
attachments.removeDescription=This will remove all embedded files from the PDF.
attachments.removeButton=Remove All Attachments

View File

@ -1642,18 +1642,3 @@ fakeScan.blur=Blur
fakeScan.noise=Noise
fakeScan.yellowish=Yellowish (simulate old paper)
fakeScan.resolution=Resolution (DPI)
#attachments
home.attachments.title=Add Attachments
home.attachments.desc=Add or remove embedded files (attachments) to/from a PDF
attachments.tags=embed,attach,file,attachment,attachments
attachments.title=Attachments
attachments.header=Add attachments
attachments.removeHeader=Remove attachments from PDF
attachments.selectFiles=Select files to attach
attachments.description=Allows you to add attachments to the PDF
attachments.descriptionPlaceholder=Enter a description for the attachments...
attachments.addButton=Add Attachments
attachments.removeDescription=This will remove all embedded files from the PDF.
attachments.removeButton=Remove All Attachments

View File

@ -1642,18 +1642,3 @@ fakeScan.blur=Blur
fakeScan.noise=Noise
fakeScan.yellowish=Yellowish (simulate old paper)
fakeScan.resolution=Resolution (DPI)
#attachments
home.attachments.title=Add Attachments
home.attachments.desc=Add or remove embedded files (attachments) to/from a PDF
attachments.tags=embed,attach,file,attachment,attachments
attachments.title=Attachments
attachments.header=Add attachments
attachments.removeHeader=Remove attachments from PDF
attachments.selectFiles=Select files to attach
attachments.description=Allows you to add attachments to the PDF
attachments.descriptionPlaceholder=Enter a description for the attachments...
attachments.addButton=Add Attachments
attachments.removeDescription=This will remove all embedded files from the PDF.
attachments.removeButton=Remove All Attachments

View File

@ -1642,18 +1642,3 @@ fakeScan.blur=Blur
fakeScan.noise=Noise
fakeScan.yellowish=Yellowish (simulate old paper)
fakeScan.resolution=Resolution (DPI)
#attachments
home.attachments.title=Add Attachments
home.attachments.desc=Add or remove embedded files (attachments) to/from a PDF
attachments.tags=embed,attach,file,attachment,attachments
attachments.title=Attachments
attachments.header=Add attachments
attachments.removeHeader=Remove attachments from PDF
attachments.selectFiles=Select files to attach
attachments.description=Allows you to add attachments to the PDF
attachments.descriptionPlaceholder=Enter a description for the attachments...
attachments.addButton=Add Attachments
attachments.removeDescription=This will remove all embedded files from the PDF.
attachments.removeButton=Remove All Attachments

View File

@ -1642,18 +1642,3 @@ fakeScan.blur=Blur
fakeScan.noise=Noise
fakeScan.yellowish=Yellowish (simulate old paper)
fakeScan.resolution=Resolution (DPI)
#attachments
home.attachments.title=Add Attachments
home.attachments.desc=Add or remove embedded files (attachments) to/from a PDF
attachments.tags=embed,attach,file,attachment,attachments
attachments.title=Attachments
attachments.header=Add attachments
attachments.removeHeader=Remove attachments from PDF
attachments.selectFiles=Select files to attach
attachments.description=Allows you to add attachments to the PDF
attachments.descriptionPlaceholder=Enter a description for the attachments...
attachments.addButton=Add Attachments
attachments.removeDescription=This will remove all embedded files from the PDF.
attachments.removeButton=Remove All Attachments

View File

@ -1642,19 +1642,3 @@ fakeScan.blur=Blur
fakeScan.noise=Noise
fakeScan.yellowish=Yellowish (simulate old paper)
fakeScan.resolution=Resolution (DPI)
#attachments
home.attachments.title=Add Attachments
home.attachments.desc=Add or remove embedded files (attachments) to/from a PDF
attachments.tags=embed,attach,file,attachment,attachments
attachments.title=Attachments
attachments.header=Add attachments
attachments.removeHeader=Remove attachments from PDF
attachments.selectFiles=Select files to attach
attachments.description=Allows you to add attachments to the PDF
attachments.descriptionPlaceholder=Enter a description for the attachments...
attachments.addButton=Add Attachments
attachments.removeDescription=This will remove all embedded files from the PDF.
attachments.removeButton=Remove All Attachments

View File

@ -1642,18 +1642,3 @@ fakeScan.blur=Blur
fakeScan.noise=Noise
fakeScan.yellowish=Yellowish (simulate old paper)
fakeScan.resolution=Resolution (DPI)
#attachments
home.attachments.title=Add Attachments
home.attachments.desc=Add or remove embedded files (attachments) to/from a PDF
attachments.tags=embed,attach,file,attachment,attachments
attachments.title=Attachments
attachments.header=Add attachments
attachments.removeHeader=Remove attachments from PDF
attachments.selectFiles=Select files to attach
attachments.description=Allows you to add attachments to the PDF
attachments.descriptionPlaceholder=Enter a description for the attachments...
attachments.addButton=Add Attachments
attachments.removeDescription=This will remove all embedded files from the PDF.
attachments.removeButton=Remove All Attachments

View File

@ -1642,18 +1642,3 @@ fakeScan.blur=Blur
fakeScan.noise=Noise
fakeScan.yellowish=Yellowish (simulate old paper)
fakeScan.resolution=Resolution (DPI)
#attachments
home.attachments.title=Add Attachments
home.attachments.desc=Add or remove embedded files (attachments) to/from a PDF
attachments.tags=embed,attach,file,attachment,attachments
attachments.title=Attachments
attachments.header=Add attachments
attachments.removeHeader=Remove attachments from PDF
attachments.selectFiles=Select files to attach
attachments.description=Allows you to add attachments to the PDF
attachments.descriptionPlaceholder=Enter a description for the attachments...
attachments.addButton=Add Attachments
attachments.removeDescription=This will remove all embedded files from the PDF.
attachments.removeButton=Remove All Attachments

View File

@ -1642,18 +1642,3 @@ fakeScan.blur=Blur
fakeScan.noise=Noise
fakeScan.yellowish=Yellowish (simulate old paper)
fakeScan.resolution=Resolution (DPI)
#attachments
home.attachments.title=Add Attachments
home.attachments.desc=Add or remove embedded files (attachments) to/from a PDF
attachments.tags=embed,attach,file,attachment,attachments
attachments.title=Attachments
attachments.header=Add attachments
attachments.removeHeader=Remove attachments from PDF
attachments.selectFiles=Select files to attach
attachments.description=Allows you to add attachments to the PDF
attachments.descriptionPlaceholder=Enter a description for the attachments...
attachments.addButton=Add Attachments
attachments.removeDescription=This will remove all embedded files from the PDF.
attachments.removeButton=Remove All Attachments

View File

@ -1642,18 +1642,3 @@ fakeScan.blur=Blur
fakeScan.noise=Noise
fakeScan.yellowish=Yellowish (simulate old paper)
fakeScan.resolution=Resolution (DPI)
#attachments
home.attachments.title=Add Attachments
home.attachments.desc=Add or remove embedded files (attachments) to/from a PDF
attachments.tags=embed,attach,file,attachment,attachments
attachments.title=Attachments
attachments.header=Add attachments
attachments.removeHeader=Remove attachments from PDF
attachments.selectFiles=Select files to attach
attachments.description=Allows you to add attachments to the PDF
attachments.descriptionPlaceholder=Enter a description for the attachments...
attachments.addButton=Add Attachments
attachments.removeDescription=This will remove all embedded files from the PDF.
attachments.removeButton=Remove All Attachments

View File

@ -1642,18 +1642,3 @@ fakeScan.blur=Blur
fakeScan.noise=Noise
fakeScan.yellowish=Yellowish (simulate old paper)
fakeScan.resolution=Resolution (DPI)
#attachments
home.attachments.title=Add Attachments
home.attachments.desc=Add or remove embedded files (attachments) to/from a PDF
attachments.tags=embed,attach,file,attachment,attachments
attachments.title=Attachments
attachments.header=Add attachments
attachments.removeHeader=Remove attachments from PDF
attachments.selectFiles=Select files to attach
attachments.description=Allows you to add attachments to the PDF
attachments.descriptionPlaceholder=Enter a description for the attachments...
attachments.addButton=Add Attachments
attachments.removeDescription=This will remove all embedded files from the PDF.
attachments.removeButton=Remove All Attachments

View File

@ -1642,18 +1642,3 @@ fakeScan.blur=Blur
fakeScan.noise=Noise
fakeScan.yellowish=Yellowish (simulate old paper)
fakeScan.resolution=Resolution (DPI)
#attachments
home.attachments.title=Add Attachments
home.attachments.desc=Add or remove embedded files (attachments) to/from a PDF
attachments.tags=embed,attach,file,attachment,attachments
attachments.title=Attachments
attachments.header=Add attachments
attachments.removeHeader=Remove attachments from PDF
attachments.selectFiles=Select files to attach
attachments.description=Allows you to add attachments to the PDF
attachments.descriptionPlaceholder=Enter a description for the attachments...
attachments.addButton=Add Attachments
attachments.removeDescription=This will remove all embedded files from the PDF.
attachments.removeButton=Remove All Attachments

View File

@ -1642,18 +1642,3 @@ fakeScan.blur=Blur
fakeScan.noise=Noise
fakeScan.yellowish=Yellowish (simulate old paper)
fakeScan.resolution=Resolution (DPI)
#attachments
home.attachments.title=Add Attachments
home.attachments.desc=Add or remove embedded files (attachments) to/from a PDF
attachments.tags=embed,attach,file,attachment,attachments
attachments.title=Attachments
attachments.header=Add attachments
attachments.removeHeader=Remove attachments from PDF
attachments.selectFiles=Select files to attach
attachments.description=Allows you to add attachments to the PDF
attachments.descriptionPlaceholder=Enter a description for the attachments...
attachments.addButton=Add Attachments
attachments.removeDescription=This will remove all embedded files from the PDF.
attachments.removeButton=Remove All Attachments

View File

@ -1642,18 +1642,3 @@ fakeScan.blur=Blur
fakeScan.noise=Noise
fakeScan.yellowish=Yellowish (simulate old paper)
fakeScan.resolution=Resolution (DPI)
#attachments
home.attachments.title=Add Attachments
home.attachments.desc=Add or remove embedded files (attachments) to/from a PDF
attachments.tags=embed,attach,file,attachment,attachments
attachments.title=Attachments
attachments.header=Add attachments
attachments.removeHeader=Remove attachments from PDF
attachments.selectFiles=Select files to attach
attachments.description=Allows you to add attachments to the PDF
attachments.descriptionPlaceholder=Enter a description for the attachments...
attachments.addButton=Add Attachments
attachments.removeDescription=This will remove all embedded files from the PDF.
attachments.removeButton=Remove All Attachments

View File

@ -1642,18 +1642,3 @@ fakeScan.blur=Blur
fakeScan.noise=Noise
fakeScan.yellowish=Yellowish (simulate old paper)
fakeScan.resolution=Resolution (DPI)
#attachments
home.attachments.title=Add Attachments
home.attachments.desc=Add or remove embedded files (attachments) to/from a PDF
attachments.tags=embed,attach,file,attachment,attachments
attachments.title=Attachments
attachments.header=Add attachments
attachments.removeHeader=Remove attachments from PDF
attachments.selectFiles=Select files to attach
attachments.description=Allows you to add attachments to the PDF
attachments.descriptionPlaceholder=Enter a description for the attachments...
attachments.addButton=Add Attachments
attachments.removeDescription=This will remove all embedded files from the PDF.
attachments.removeButton=Remove All Attachments

View File

@ -1642,18 +1642,3 @@ fakeScan.blur=Blur
fakeScan.noise=Noise
fakeScan.yellowish=Yellowish (simulate old paper)
fakeScan.resolution=Resolution (DPI)
#attachments
home.attachments.title=Add Attachments
home.attachments.desc=Add or remove embedded files (attachments) to/from a PDF
attachments.tags=embed,attach,file,attachment,attachments
attachments.title=Attachments
attachments.header=Add attachments
attachments.removeHeader=Remove attachments from PDF
attachments.selectFiles=Select files to attach
attachments.description=Allows you to add attachments to the PDF
attachments.descriptionPlaceholder=Enter a description for the attachments...
attachments.addButton=Add Attachments
attachments.removeDescription=This will remove all embedded files from the PDF.
attachments.removeButton=Remove All Attachments

View File

@ -1642,18 +1642,3 @@ fakeScan.blur=Blur
fakeScan.noise=Noise
fakeScan.yellowish=Yellowish (simulate old paper)
fakeScan.resolution=Resolution (DPI)
#attachments
home.attachments.title=Add Attachments
home.attachments.desc=Add or remove embedded files (attachments) to/from a PDF
attachments.tags=embed,attach,file,attachment,attachments
attachments.title=Attachments
attachments.header=Add attachments
attachments.removeHeader=Remove attachments from PDF
attachments.selectFiles=Select files to attach
attachments.description=Allows you to add attachments to the PDF
attachments.descriptionPlaceholder=Enter a description for the attachments...
attachments.addButton=Add Attachments
attachments.removeDescription=This will remove all embedded files from the PDF.
attachments.removeButton=Remove All Attachments

View File

@ -1642,18 +1642,3 @@ fakeScan.blur=Blur
fakeScan.noise=Noise
fakeScan.yellowish=Yellowish (simulate old paper)
fakeScan.resolution=Resolution (DPI)
#attachments
home.attachments.title=Add Attachments
home.attachments.desc=Add or remove embedded files (attachments) to/from a PDF
attachments.tags=embed,attach,file,attachment,attachments
attachments.title=Attachments
attachments.header=Add attachments
attachments.removeHeader=Remove attachments from PDF
attachments.selectFiles=Select files to attach
attachments.description=Allows you to add attachments to the PDF
attachments.descriptionPlaceholder=Enter a description for the attachments...
attachments.addButton=Add Attachments
attachments.removeDescription=This will remove all embedded files from the PDF.
attachments.removeButton=Remove All Attachments

View File

@ -1,209 +0,0 @@
package stirling.software.SPDF.controller.api.misc;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
import java.io.IOException;
import java.util.List;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDDocumentCatalog;
import org.apache.pdfbox.pdmodel.PDDocumentNameDictionary;
import org.apache.pdfbox.pdmodel.PDEmbeddedFilesNameTreeNode;
import org.apache.pdfbox.pdmodel.PageMode;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.multipart.MultipartFile;
import stirling.software.SPDF.service.PDFAttachmentServiceInterface;
import stirling.software.common.service.CustomPDFDocumentFactory;
@ExtendWith(MockitoExtension.class)
class AttachmentsControllerTest {
@Mock
private CustomPDFDocumentFactory pdfDocumentFactory;
@Mock
private PDFAttachmentServiceInterface pdfAttachmentService;
@InjectMocks
private AttachmentsController attachmentsController;
private MockMultipartFile pdfFile;
private MockMultipartFile attachment1;
private MockMultipartFile attachment2;
private PDDocument mockDocument;
private PDDocumentCatalog mockCatalog;
private PDDocumentNameDictionary mockNameDict;
private PDEmbeddedFilesNameTreeNode mockEmbeddedFilesTree;
@BeforeEach
void setUp() {
pdfFile = new MockMultipartFile("fileInput", "test.pdf", "application/pdf", "PDF content".getBytes());
attachment1 = new MockMultipartFile("attachment1", "file1.txt", "text/plain", "File 1 content".getBytes());
attachment2 = new MockMultipartFile("attachment2", "file2.jpg", "image/jpeg", "Image content".getBytes());
mockDocument = mock(PDDocument.class);
mockCatalog = mock(PDDocumentCatalog.class);
mockNameDict = mock(PDDocumentNameDictionary.class);
mockEmbeddedFilesTree = mock(PDEmbeddedFilesNameTreeNode.class);
}
@Test
void addAttachments_WithExistingNames() throws IOException {
List<MultipartFile> attachments = List.of(attachment1, attachment2);
when(pdfDocumentFactory.load(pdfFile, false)).thenReturn(mockDocument);
when(mockDocument.getDocumentCatalog()).thenReturn(mockCatalog);
when(mockCatalog.getNames()).thenReturn(mockNameDict);
when(mockNameDict.getEmbeddedFiles()).thenReturn(mockEmbeddedFilesTree);
ResponseEntity<byte[]> response = attachmentsController.addAttachments(pdfFile, attachments);
assertNotNull(response);
assertEquals(HttpStatus.OK, response.getStatusCode());
verify(pdfDocumentFactory).load(pdfFile, false);
verify(mockCatalog).setNames(mockNameDict);
verify(pdfAttachmentService).addAttachment(mockDocument, mockEmbeddedFilesTree, attachments);
}
@Test
void addAttachments_WithoutExistingNames() throws IOException {
List<MultipartFile> attachments = List.of(attachment1);
try (PDDocument realDocument = new PDDocument()) {
when(pdfDocumentFactory.load(pdfFile, false)).thenReturn(realDocument);
ResponseEntity<byte[]> response = attachmentsController.addAttachments(pdfFile, attachments);
assertNotNull(response);
assertEquals(HttpStatus.OK, response.getStatusCode());
verify(pdfDocumentFactory).load(pdfFile, false);
verify(pdfAttachmentService).addAttachment(eq(realDocument), any(PDEmbeddedFilesNameTreeNode.class), eq(attachments));
}
}
@Test
void addAttachments_IOExceptionFromPDFLoad() throws IOException {
List<MultipartFile> attachments = List.of(attachment1);
IOException ioException = new IOException("Failed to load PDF");
when(pdfDocumentFactory.load(pdfFile, false)).thenThrow(ioException);
assertThrows(IOException.class, () -> attachmentsController.addAttachments(pdfFile, attachments));
verify(pdfDocumentFactory).load(pdfFile, false);
verifyNoInteractions(pdfAttachmentService);
}
@Test
void addAttachments_IOExceptionFromAttachmentService() throws IOException {
List<MultipartFile> attachments = List.of(attachment1);
IOException ioException = new IOException("Failed to add attachment");
when(pdfDocumentFactory.load(pdfFile, false)).thenReturn(mockDocument);
when(mockDocument.getDocumentCatalog()).thenReturn(mockCatalog);
when(mockCatalog.getNames()).thenReturn(mockNameDict);
when(mockNameDict.getEmbeddedFiles()).thenReturn(mockEmbeddedFilesTree);
doThrow(ioException).when(pdfAttachmentService).addAttachment(mockDocument, mockEmbeddedFilesTree, attachments);
assertThrows(IOException.class, () -> attachmentsController.addAttachments(pdfFile, attachments));
verify(pdfAttachmentService).addAttachment(mockDocument, mockEmbeddedFilesTree, attachments);
}
@Test
void removeAttachments_WithExistingNames() throws IOException {
when(pdfDocumentFactory.load(pdfFile)).thenReturn(mockDocument);
when(mockDocument.getDocumentCatalog()).thenReturn(mockCatalog);
when(mockCatalog.getNames()).thenReturn(mockNameDict);
ResponseEntity<byte[]> response = attachmentsController.removeAttachments(pdfFile);
assertNotNull(response);
assertEquals(HttpStatus.OK, response.getStatusCode());
verify(pdfDocumentFactory).load(pdfFile);
verify(mockNameDict).setEmbeddedFiles(null);
verify(mockCatalog).setPageMode(PageMode.USE_NONE);
}
@Test
void removeAttachments_WithoutExistingNames() throws IOException {
when(pdfDocumentFactory.load(pdfFile)).thenReturn(mockDocument);
when(mockDocument.getDocumentCatalog()).thenReturn(mockCatalog);
when(mockCatalog.getNames()).thenReturn(null);
ResponseEntity<byte[]> response = attachmentsController.removeAttachments(pdfFile);
assertNotNull(response);
assertEquals(HttpStatus.OK, response.getStatusCode());
verify(pdfDocumentFactory).load(pdfFile);
verify(mockCatalog).setPageMode(PageMode.USE_NONE);
verifyNoInteractions(mockNameDict);
}
@Test
void removeAttachments_IOExceptionFromPDFLoad() throws IOException {
IOException ioException = new IOException("Failed to load PDF");
when(pdfDocumentFactory.load(pdfFile)).thenThrow(ioException);
assertThrows(IOException.class, () -> attachmentsController.removeAttachments(pdfFile));
verify(pdfDocumentFactory).load(pdfFile);
}
@Test
void addAttachments_EmptyAttachmentsList() throws IOException {
List<MultipartFile> emptyAttachments = List.of();
when(pdfDocumentFactory.load(pdfFile, false)).thenReturn(mockDocument);
when(mockDocument.getDocumentCatalog()).thenReturn(mockCatalog);
when(mockCatalog.getNames()).thenReturn(mockNameDict);
when(mockNameDict.getEmbeddedFiles()).thenReturn(mockEmbeddedFilesTree);
ResponseEntity<byte[]> response = attachmentsController.addAttachments(pdfFile, emptyAttachments);
assertNotNull(response);
assertEquals(HttpStatus.OK, response.getStatusCode());
verify(pdfAttachmentService).addAttachment(mockDocument, mockEmbeddedFilesTree, emptyAttachments);
}
@Test
void addAttachments_NullFilename() throws IOException {
MockMultipartFile attachmentWithNullName = new MockMultipartFile("attachment", null, "text/plain", "content".getBytes());
List<MultipartFile> attachments = List.of(attachmentWithNullName);
when(pdfDocumentFactory.load(pdfFile, false)).thenReturn(mockDocument);
when(mockDocument.getDocumentCatalog()).thenReturn(mockCatalog);
when(mockCatalog.getNames()).thenReturn(mockNameDict);
when(mockNameDict.getEmbeddedFiles()).thenReturn(mockEmbeddedFilesTree);
ResponseEntity<byte[]> response = attachmentsController.addAttachments(pdfFile, attachments);
assertNotNull(response);
assertEquals(HttpStatus.OK, response.getStatusCode());
verify(pdfAttachmentService).addAttachment(mockDocument, mockEmbeddedFilesTree, attachments);
}
@Test
void removeAttachments_NullPDFFilename() throws IOException {
MockMultipartFile pdfWithNullName = new MockMultipartFile("fileInput", null, "application/pdf", "PDF content".getBytes());
when(pdfDocumentFactory.load(pdfWithNullName)).thenReturn(mockDocument);
when(mockDocument.getDocumentCatalog()).thenReturn(mockCatalog);
when(mockCatalog.getNames()).thenReturn(null);
ResponseEntity<byte[]> response = attachmentsController.removeAttachments(pdfWithNullName);
assertNotNull(response);
assertEquals(HttpStatus.OK, response.getStatusCode());
verify(mockCatalog).setPageMode(PageMode.USE_NONE);
}
}

View File

@ -1,119 +0,0 @@
package stirling.software.SPDF.service;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDEmbeddedFilesNameTreeNode;
import org.apache.pdfbox.pdmodel.common.filespecification.PDComplexFileSpecification;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.web.multipart.MultipartFile;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.anyMap;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
class PDFAttachmentServiceTest {
private PDFAttachmentService pdfAttachmentService;
@BeforeEach
void setUp() {
pdfAttachmentService = new PDFAttachmentService();
}
@Test
void addAttachmentToPDF() throws IOException {
try (var document = new PDDocument()) {
var embeddedFilesTree = mock(PDEmbeddedFilesNameTreeNode.class);
var attachments = List.of(mock(MultipartFile.class));
var existingNames = new HashMap<String, PDComplexFileSpecification>();
when(embeddedFilesTree.getNames()).thenReturn(existingNames);
when(attachments.get(0).getOriginalFilename()).thenReturn("test.txt");
when(attachments.get(0).getInputStream()).thenReturn(
new ByteArrayInputStream("Test content".getBytes()));
when(attachments.get(0).getSize()).thenReturn(12L);
when(attachments.get(0).getContentType()).thenReturn("text/plain");
pdfAttachmentService.addAttachment(document, embeddedFilesTree, attachments);
verify(embeddedFilesTree).setNames(anyMap());
}
}
@Test
void addAttachmentToPDF_WithNullExistingNames() throws IOException {
try (var document = new PDDocument()) {
var embeddedFilesTree = mock(PDEmbeddedFilesNameTreeNode.class);
var attachments = List.of(mock(MultipartFile.class));
when(embeddedFilesTree.getNames()).thenReturn(null);
when(attachments.get(0).getOriginalFilename()).thenReturn("document.pdf");
when(attachments.get(0).getInputStream()).thenReturn(
new ByteArrayInputStream("PDF content".getBytes()));
when(attachments.get(0).getSize()).thenReturn(15L);
when(attachments.get(0).getContentType()).thenReturn("application/pdf");
pdfAttachmentService.addAttachment(document, embeddedFilesTree, attachments);
verify(embeddedFilesTree).setNames(anyMap());
}
}
@Test
void addAttachmentToPDF_WithBlankContentType() throws IOException {
try (var document = new PDDocument()) {
var embeddedFilesTree = mock(PDEmbeddedFilesNameTreeNode.class);
var attachments = List.of(mock(MultipartFile.class));
var existingNames = new HashMap<String, PDComplexFileSpecification>();
when(embeddedFilesTree.getNames()).thenReturn(existingNames);
when(attachments.get(0).getOriginalFilename()).thenReturn("image.jpg");
when(attachments.get(0).getInputStream()).thenReturn(
new ByteArrayInputStream("Image content".getBytes()));
when(attachments.get(0).getSize()).thenReturn(25L);
when(attachments.get(0).getContentType()).thenReturn("");
pdfAttachmentService.addAttachment(document, embeddedFilesTree, attachments);
verify(embeddedFilesTree).setNames(anyMap());
}
}
@Test
void addAttachmentToPDF_GetNamesThrowsIOException() throws IOException {
var document = mock(PDDocument.class);
var embeddedFilesTree = mock(PDEmbeddedFilesNameTreeNode.class);
var attachments = List.of(mock(MultipartFile.class));
var ioException = new IOException("Failed to retrieve embedded files");
when(embeddedFilesTree.getNames()).thenThrow(ioException);
assertThrows(IOException.class, () -> pdfAttachmentService.addAttachment(document, embeddedFilesTree, attachments));
verify(embeddedFilesTree).getNames();
}
@Test
void addAttachmentToPDF_AttachmentInputStreamThrowsIOException() throws IOException {
try (var document = new PDDocument()) {
var embeddedFilesTree = mock(PDEmbeddedFilesNameTreeNode.class);
var attachments = List.of(mock(MultipartFile.class));
var existingNames = new HashMap<String, PDComplexFileSpecification>();
var ioException = new IOException("Failed to read attachment stream");
when(embeddedFilesTree.getNames()).thenReturn(existingNames);
when(attachments.get(0).getOriginalFilename()).thenReturn("corrupted.file");
when(attachments.get(0).getInputStream()).thenThrow(ioException);
when(attachments.get(0).getSize()).thenReturn(10L);
pdfAttachmentService.addAttachment(document, embeddedFilesTree, attachments);
verify(embeddedFilesTree).setNames(anyMap());
}
}
}