Fix split mode null crash by defaulting to ALL

This commit is contained in:
Ping Lin 2025-08-04 15:22:12 +01:00
parent ad0bdc6d7e
commit 483d8b812b
2 changed files with 30 additions and 26 deletions

View File

@ -4,10 +4,7 @@ import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.ArrayList; import java.util.*;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.zip.ZipEntry; import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream; import java.util.zip.ZipOutputStream;
@ -61,10 +58,14 @@ public class SplitPdfBySectionsController {
MultipartFile file = request.getFileInput(); MultipartFile file = request.getFileInput();
String pageNumbers = request.getPageNumbers(); String pageNumbers = request.getPageNumbers();
String splitMode = request.getSplitMode(); SplitTypes splitMode = Optional.ofNullable(request.getSplitMode())
.map(SplitTypes::valueOf)
.orElse(SplitTypes.SPLIT_ALL);
PDDocument sourceDocument = pdfDocumentFactory.load(file); PDDocument sourceDocument = pdfDocumentFactory.load(file);
Set<Integer> pagesToSplit = getPagesToSplit(pageNumbers, splitMode, sourceDocument.getNumberOfPages()); Set<Integer> pagesToSplit =
getPagesToSplit(pageNumbers, splitMode, sourceDocument.getNumberOfPages());
// Process the PDF based on split parameters // Process the PDF based on split parameters
int horiz = request.getHorizontalDivisions() + 1; int horiz = request.getHorizontalDivisions() + 1;
@ -119,16 +120,17 @@ public class SplitPdfBySectionsController {
} }
// Based on the mode, get the pages that need to be split and return the pages set // Based on the mode, get the pages that need to be split and return the pages set
private Set<Integer> getPagesToSplit(String pageNumbers, String splitMode, int totalPages) { private Set<Integer> getPagesToSplit(String pageNumbers, SplitTypes splitMode, int totalPages) {
Set<Integer> pagesToSplit = new HashSet<>(); Set<Integer> pagesToSplit = new HashSet<>();
switch (SplitTypes.valueOf(splitMode)) { switch (splitMode) {
case CUSTOM: case CUSTOM:
if (pageNumbers == null || pageNumbers.isBlank()) { if (pageNumbers == null || pageNumbers.isBlank()) {
throw new IllegalArgumentException("Custom mode requires page numbers input."); throw new IllegalArgumentException("Custom mode requires page numbers input.");
} }
String[] pageOrderArr = pageNumbers.split(","); String[] pageOrderArr = pageNumbers.split(",");
List<Integer> pageListToSplit = GeneralUtils.parsePageList(pageOrderArr, totalPages, false); List<Integer> pageListToSplit =
GeneralUtils.parsePageList(pageOrderArr, totalPages, false);
pagesToSplit.addAll(pageListToSplit); pagesToSplit.addAll(pageListToSplit);
break; break;
@ -163,9 +165,11 @@ public class SplitPdfBySectionsController {
return pagesToSplit; return pagesToSplit;
} }
public List<PDDocument> splitPdfPages( public List<PDDocument> splitPdfPages(
PDDocument document, int horizontalDivisions, int verticalDivisions, Set<Integer> pagesToSplit) PDDocument document,
int horizontalDivisions,
int verticalDivisions,
Set<Integer> pagesToSplit)
throws IOException { throws IOException {
List<PDDocument> splitDocuments = new ArrayList<>(); List<PDDocument> splitDocuments = new ArrayList<>();