mirror of
https://github.com/Frooodle/Stirling-PDF.git
synced 2025-08-06 13:48:58 +02:00
refactor: replace switch statements with modern switch expressions for improved readability
This commit is contained in:
parent
a5d219ed05
commit
69a663e7f7
@ -51,16 +51,11 @@ public class SsrfProtectionService {
|
|||||||
|
|
||||||
SsrfProtectionLevel level = parseProtectionLevel(config.getLevel());
|
SsrfProtectionLevel level = parseProtectionLevel(config.getLevel());
|
||||||
|
|
||||||
switch (level) {
|
return switch (level) {
|
||||||
case OFF:
|
case OFF -> true;
|
||||||
return true;
|
case MAX -> isMaxSecurityAllowed(trimmedUrl, config);
|
||||||
case MAX:
|
case MEDIUM -> isMediumSecurityAllowed(trimmedUrl, config);
|
||||||
return isMaxSecurityAllowed(trimmedUrl, config);
|
};
|
||||||
case MEDIUM:
|
|
||||||
return isMediumSecurityAllowed(trimmedUrl, config);
|
|
||||||
default:
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private SsrfProtectionLevel parseProtectionLevel(String level) {
|
private SsrfProtectionLevel parseProtectionLevel(String level) {
|
||||||
|
@ -82,19 +82,16 @@ public class ImageProcessingUtils {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
int orientationTag = directory.getInt(ExifSubIFDDirectory.TAG_ORIENTATION);
|
int orientationTag = directory.getInt(ExifSubIFDDirectory.TAG_ORIENTATION);
|
||||||
switch (orientationTag) {
|
return switch (orientationTag) {
|
||||||
case 1:
|
case 1 -> 0;
|
||||||
return 0;
|
case 6 -> 90;
|
||||||
case 6:
|
case 3 -> 180;
|
||||||
return 90;
|
case 8 -> 270;
|
||||||
case 3:
|
default -> {
|
||||||
return 180;
|
|
||||||
case 8:
|
|
||||||
return 270;
|
|
||||||
default:
|
|
||||||
log.warn("Unknown orientation tag: {}", orientationTag);
|
log.warn("Unknown orientation tag: {}", orientationTag);
|
||||||
return 0;
|
yield 0;
|
||||||
}
|
}
|
||||||
|
};
|
||||||
} catch (ImageProcessingException | MetadataException e) {
|
} catch (ImageProcessingException | MetadataException e) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -62,11 +62,9 @@ public class MergeController {
|
|||||||
|
|
||||||
// Returns a comparator for sorting MultipartFile arrays based on the given sort type
|
// Returns a comparator for sorting MultipartFile arrays based on the given sort type
|
||||||
private Comparator<MultipartFile> getSortComparator(String sortType) {
|
private Comparator<MultipartFile> getSortComparator(String sortType) {
|
||||||
switch (sortType) {
|
return switch (sortType) {
|
||||||
case "byFileName":
|
case "byFileName" -> Comparator.comparing(MultipartFile::getOriginalFilename);
|
||||||
return Comparator.comparing(MultipartFile::getOriginalFilename);
|
case "byDateModified" -> (file1, file2) -> {
|
||||||
case "byDateModified":
|
|
||||||
return (file1, file2) -> {
|
|
||||||
try {
|
try {
|
||||||
BasicFileAttributes attr1 =
|
BasicFileAttributes attr1 =
|
||||||
Files.readAttributes(
|
Files.readAttributes(
|
||||||
@ -81,8 +79,7 @@ public class MergeController {
|
|||||||
return 0; // If there's an error, treat them as equal
|
return 0; // If there's an error, treat them as equal
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
case "byDateCreated":
|
case "byDateCreated" -> (file1, file2) -> {
|
||||||
return (file1, file2) -> {
|
|
||||||
try {
|
try {
|
||||||
BasicFileAttributes attr1 =
|
BasicFileAttributes attr1 =
|
||||||
Files.readAttributes(
|
Files.readAttributes(
|
||||||
@ -97,8 +94,7 @@ public class MergeController {
|
|||||||
return 0; // If there's an error, treat them as equal
|
return 0; // If there's an error, treat them as equal
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
case "byPDFTitle":
|
case "byPDFTitle" -> (file1, file2) -> {
|
||||||
return (file1, file2) -> {
|
|
||||||
try (PDDocument doc1 = pdfDocumentFactory.load(file1);
|
try (PDDocument doc1 = pdfDocumentFactory.load(file1);
|
||||||
PDDocument doc2 = pdfDocumentFactory.load(file2)) {
|
PDDocument doc2 = pdfDocumentFactory.load(file2)) {
|
||||||
String title1 = doc1.getDocumentInformation().getTitle();
|
String title1 = doc1.getDocumentInformation().getTitle();
|
||||||
@ -108,10 +104,8 @@ public class MergeController {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
case "orderProvided":
|
default -> (file1, file2) -> 0; // Default is the order provided
|
||||||
default:
|
};
|
||||||
return (file1, file2) -> 0; // Default is the order provided
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Adds a table of contents to the merged document using filenames as chapter titles
|
// Adds a table of contents to the merged document using filenames as chapter titles
|
||||||
|
@ -207,30 +207,19 @@ public class RearrangePagesPDFController {
|
|||||||
private List<Integer> processSortTypes(String sortTypes, int totalPages, String pageOrder) {
|
private List<Integer> processSortTypes(String sortTypes, int totalPages, String pageOrder) {
|
||||||
try {
|
try {
|
||||||
SortTypes mode = SortTypes.valueOf(sortTypes.toUpperCase());
|
SortTypes mode = SortTypes.valueOf(sortTypes.toUpperCase());
|
||||||
switch (mode) {
|
return switch (mode) {
|
||||||
case REVERSE_ORDER:
|
case REVERSE_ORDER -> reverseOrder(totalPages);
|
||||||
return reverseOrder(totalPages);
|
case DUPLEX_SORT -> duplexSort(totalPages);
|
||||||
case DUPLEX_SORT:
|
case BOOKLET_SORT -> bookletSort(totalPages);
|
||||||
return duplexSort(totalPages);
|
case SIDE_STITCH_BOOKLET_SORT -> sideStitchBooklet(totalPages);
|
||||||
case BOOKLET_SORT:
|
case ODD_EVEN_SPLIT -> oddEvenSplit(totalPages);
|
||||||
return bookletSort(totalPages);
|
case ODD_EVEN_MERGE -> oddEvenMerge(totalPages);
|
||||||
case SIDE_STITCH_BOOKLET_SORT:
|
case REMOVE_FIRST -> removeFirst(totalPages);
|
||||||
return sideStitchBooklet(totalPages);
|
case REMOVE_LAST -> removeLast(totalPages);
|
||||||
case ODD_EVEN_SPLIT:
|
case REMOVE_FIRST_AND_LAST -> removeFirstAndLast(totalPages);
|
||||||
return oddEvenSplit(totalPages);
|
case DUPLICATE -> duplicate(totalPages, pageOrder);
|
||||||
case ODD_EVEN_MERGE:
|
default -> throw new IllegalArgumentException("Unsupported custom mode");
|
||||||
return oddEvenMerge(totalPages);
|
};
|
||||||
case REMOVE_FIRST:
|
|
||||||
return removeFirst(totalPages);
|
|
||||||
case REMOVE_LAST:
|
|
||||||
return removeLast(totalPages);
|
|
||||||
case REMOVE_FIRST_AND_LAST:
|
|
||||||
return removeFirstAndLast(totalPages);
|
|
||||||
case DUPLICATE:
|
|
||||||
return duplicate(totalPages, pageOrder);
|
|
||||||
default:
|
|
||||||
throw new IllegalArgumentException("Unsupported custom mode");
|
|
||||||
}
|
|
||||||
} catch (IllegalArgumentException e) {
|
} catch (IllegalArgumentException e) {
|
||||||
log.error("Unsupported custom mode", e);
|
log.error("Unsupported custom mode", e);
|
||||||
return null;
|
return null;
|
||||||
|
@ -84,21 +84,14 @@ public class FilterController {
|
|||||||
PDDocument document = pdfDocumentFactory.load(inputFile);
|
PDDocument document = pdfDocumentFactory.load(inputFile);
|
||||||
int actualPageCount = document.getNumberOfPages();
|
int actualPageCount = document.getNumberOfPages();
|
||||||
|
|
||||||
boolean valid = false;
|
boolean valid = switch (comparator) {
|
||||||
// Perform the comparison
|
case "Greater" -> actualPageCount > pageCount;
|
||||||
switch (comparator) {
|
case "Equal" -> actualPageCount == pageCount;
|
||||||
case "Greater":
|
case "Less" -> actualPageCount < pageCount;
|
||||||
valid = actualPageCount > pageCount;
|
default ->
|
||||||
break;
|
|
||||||
case "Equal":
|
|
||||||
valid = actualPageCount == pageCount;
|
|
||||||
break;
|
|
||||||
case "Less":
|
|
||||||
valid = actualPageCount < pageCount;
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
throw ExceptionUtils.createInvalidArgumentException("comparator", comparator);
|
throw ExceptionUtils.createInvalidArgumentException("comparator", comparator);
|
||||||
}
|
};
|
||||||
|
// Perform the comparison
|
||||||
|
|
||||||
if (valid) return WebResponseUtils.multiPartFileToWebResponse(inputFile);
|
if (valid) return WebResponseUtils.multiPartFileToWebResponse(inputFile);
|
||||||
return null;
|
return null;
|
||||||
@ -127,21 +120,14 @@ public class FilterController {
|
|||||||
PDRectangle standardSize = PdfUtils.textToPageSize(standardPageSize);
|
PDRectangle standardSize = PdfUtils.textToPageSize(standardPageSize);
|
||||||
float standardArea = standardSize.getWidth() * standardSize.getHeight();
|
float standardArea = standardSize.getWidth() * standardSize.getHeight();
|
||||||
|
|
||||||
boolean valid = false;
|
boolean valid = switch (comparator) {
|
||||||
// Perform the comparison
|
case "Greater" -> actualArea > standardArea;
|
||||||
switch (comparator) {
|
case "Equal" -> actualArea == standardArea;
|
||||||
case "Greater":
|
case "Less" -> actualArea < standardArea;
|
||||||
valid = actualArea > standardArea;
|
default ->
|
||||||
break;
|
|
||||||
case "Equal":
|
|
||||||
valid = actualArea == standardArea;
|
|
||||||
break;
|
|
||||||
case "Less":
|
|
||||||
valid = actualArea < standardArea;
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
throw ExceptionUtils.createInvalidArgumentException("comparator", comparator);
|
throw ExceptionUtils.createInvalidArgumentException("comparator", comparator);
|
||||||
}
|
};
|
||||||
|
// Perform the comparison
|
||||||
|
|
||||||
if (valid) return WebResponseUtils.multiPartFileToWebResponse(inputFile);
|
if (valid) return WebResponseUtils.multiPartFileToWebResponse(inputFile);
|
||||||
return null;
|
return null;
|
||||||
@ -196,21 +182,14 @@ public class FilterController {
|
|||||||
// Get the rotation of the first page
|
// Get the rotation of the first page
|
||||||
PDPage firstPage = document.getPage(0);
|
PDPage firstPage = document.getPage(0);
|
||||||
int actualRotation = firstPage.getRotation();
|
int actualRotation = firstPage.getRotation();
|
||||||
boolean valid = false;
|
boolean valid = switch (comparator) {
|
||||||
// Perform the comparison
|
case "Greater" -> actualRotation > rotation;
|
||||||
switch (comparator) {
|
case "Equal" -> actualRotation == rotation;
|
||||||
case "Greater":
|
case "Less" -> actualRotation < rotation;
|
||||||
valid = actualRotation > rotation;
|
default ->
|
||||||
break;
|
|
||||||
case "Equal":
|
|
||||||
valid = actualRotation == rotation;
|
|
||||||
break;
|
|
||||||
case "Less":
|
|
||||||
valid = actualRotation < rotation;
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
throw ExceptionUtils.createInvalidArgumentException("comparator", comparator);
|
throw ExceptionUtils.createInvalidArgumentException("comparator", comparator);
|
||||||
}
|
};
|
||||||
|
// Perform the comparison
|
||||||
|
|
||||||
if (valid) return WebResponseUtils.multiPartFileToWebResponse(inputFile);
|
if (valid) return WebResponseUtils.multiPartFileToWebResponse(inputFile);
|
||||||
return null;
|
return null;
|
||||||
|
@ -83,25 +83,13 @@ public class StampController {
|
|||||||
float overrideY = request.getOverrideY(); // New field for Y override
|
float overrideY = request.getOverrideY(); // New field for Y override
|
||||||
|
|
||||||
String customColor = request.getCustomColor();
|
String customColor = request.getCustomColor();
|
||||||
float marginFactor;
|
float marginFactor = switch (request.getCustomMargin().toLowerCase()) {
|
||||||
|
case "small" -> 0.02f;
|
||||||
switch (request.getCustomMargin().toLowerCase()) {
|
case "medium" -> 0.035f;
|
||||||
case "small":
|
case "large" -> 0.05f;
|
||||||
marginFactor = 0.02f;
|
case "x-large" -> 0.075f;
|
||||||
break;
|
default -> 0.035f;
|
||||||
case "medium":
|
};
|
||||||
marginFactor = 0.035f;
|
|
||||||
break;
|
|
||||||
case "large":
|
|
||||||
marginFactor = 0.05f;
|
|
||||||
break;
|
|
||||||
case "x-large":
|
|
||||||
marginFactor = 0.075f;
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
marginFactor = 0.035f;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Load the input PDF
|
// Load the input PDF
|
||||||
PDDocument document = pdfDocumentFactory.load(pdfFile);
|
PDDocument document = pdfDocumentFactory.load(pdfFile);
|
||||||
@ -177,27 +165,14 @@ public class StampController {
|
|||||||
throws IOException {
|
throws IOException {
|
||||||
String resourceDir = "";
|
String resourceDir = "";
|
||||||
PDFont font = new PDType1Font(Standard14Fonts.FontName.HELVETICA);
|
PDFont font = new PDType1Font(Standard14Fonts.FontName.HELVETICA);
|
||||||
switch (alphabet) {
|
resourceDir = switch (alphabet) {
|
||||||
case "arabic":
|
case "arabic" -> "static/fonts/NotoSansArabic-Regular.ttf";
|
||||||
resourceDir = "static/fonts/NotoSansArabic-Regular.ttf";
|
case "japanese" -> "static/fonts/Meiryo.ttf";
|
||||||
break;
|
case "korean" -> "static/fonts/malgun.ttf";
|
||||||
case "japanese":
|
case "chinese" -> "static/fonts/SimSun.ttf";
|
||||||
resourceDir = "static/fonts/Meiryo.ttf";
|
case "thai" -> "static/fonts/NotoSansThai-Regular.ttf";
|
||||||
break;
|
default -> "static/fonts/NotoSans-Regular.ttf";
|
||||||
case "korean":
|
};
|
||||||
resourceDir = "static/fonts/malgun.ttf";
|
|
||||||
break;
|
|
||||||
case "chinese":
|
|
||||||
resourceDir = "static/fonts/SimSun.ttf";
|
|
||||||
break;
|
|
||||||
case "thai":
|
|
||||||
resourceDir = "static/fonts/NotoSansThai-Regular.ttf";
|
|
||||||
break;
|
|
||||||
case "roman":
|
|
||||||
default:
|
|
||||||
resourceDir = "static/fonts/NotoSans-Regular.ttf";
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!"".equals(resourceDir)) {
|
if (!"".equals(resourceDir)) {
|
||||||
ClassPathResource classPathResource = new ClassPathResource(resourceDir);
|
ClassPathResource classPathResource = new ClassPathResource(resourceDir);
|
||||||
@ -319,30 +294,28 @@ public class StampController {
|
|||||||
throws IOException {
|
throws IOException {
|
||||||
float actualWidth =
|
float actualWidth =
|
||||||
(text != null) ? calculateTextWidth(text, font, fontSize) : contentWidth;
|
(text != null) ? calculateTextWidth(text, font, fontSize) : contentWidth;
|
||||||
switch (position % 3) {
|
return switch (position % 3) {
|
||||||
case 1: // Left
|
case 1 -> // Left
|
||||||
return pageSize.getLowerLeftX() + margin;
|
pageSize.getLowerLeftX() + margin;
|
||||||
case 2: // Center
|
case 2 -> // Center
|
||||||
return (pageSize.getWidth() - actualWidth) / 2;
|
(pageSize.getWidth() - actualWidth) / 2;
|
||||||
case 0: // Right
|
case 0 -> // Right
|
||||||
return pageSize.getUpperRightX() - actualWidth - margin;
|
pageSize.getUpperRightX() - actualWidth - margin;
|
||||||
default:
|
default -> 0;
|
||||||
return 0;
|
};
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private float calculatePositionY(
|
private float calculatePositionY(
|
||||||
PDRectangle pageSize, int position, float height, float margin) {
|
PDRectangle pageSize, int position, float height, float margin) {
|
||||||
switch ((position - 1) / 3) {
|
return switch ((position - 1) / 3) {
|
||||||
case 0: // Top
|
case 0 -> // Top
|
||||||
return pageSize.getUpperRightY() - height - margin;
|
pageSize.getUpperRightY() - height - margin;
|
||||||
case 1: // Middle
|
case 1 -> // Middle
|
||||||
return (pageSize.getHeight() - height) / 2;
|
(pageSize.getHeight() - height) / 2;
|
||||||
case 2: // Bottom
|
case 2 -> // Bottom
|
||||||
return pageSize.getLowerLeftY() + margin;
|
pageSize.getLowerLeftY() + margin;
|
||||||
default:
|
default -> 0;
|
||||||
return 0;
|
};
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private float calculateTextWidth(String text, PDFont font, float fontSize) throws IOException {
|
private float calculateTextWidth(String text, PDFont font, float fontSize) throws IOException {
|
||||||
|
@ -169,27 +169,14 @@ public class WatermarkController {
|
|||||||
throws IOException {
|
throws IOException {
|
||||||
String resourceDir = "";
|
String resourceDir = "";
|
||||||
PDFont font = new PDType1Font(Standard14Fonts.FontName.HELVETICA);
|
PDFont font = new PDType1Font(Standard14Fonts.FontName.HELVETICA);
|
||||||
switch (alphabet) {
|
resourceDir = switch (alphabet) {
|
||||||
case "arabic":
|
case "arabic" -> "static/fonts/NotoSansArabic-Regular.ttf";
|
||||||
resourceDir = "static/fonts/NotoSansArabic-Regular.ttf";
|
case "japanese" -> "static/fonts/Meiryo.ttf";
|
||||||
break;
|
case "korean" -> "static/fonts/malgun.ttf";
|
||||||
case "japanese":
|
case "chinese" -> "static/fonts/SimSun.ttf";
|
||||||
resourceDir = "static/fonts/Meiryo.ttf";
|
case "thai" -> "static/fonts/NotoSansThai-Regular.ttf";
|
||||||
break;
|
default -> "static/fonts/NotoSans-Regular.ttf";
|
||||||
case "korean":
|
};
|
||||||
resourceDir = "static/fonts/malgun.ttf";
|
|
||||||
break;
|
|
||||||
case "chinese":
|
|
||||||
resourceDir = "static/fonts/SimSun.ttf";
|
|
||||||
break;
|
|
||||||
case "thai":
|
|
||||||
resourceDir = "static/fonts/NotoSansThai-Regular.ttf";
|
|
||||||
break;
|
|
||||||
case "roman":
|
|
||||||
default:
|
|
||||||
resourceDir = "static/fonts/NotoSans-Regular.ttf";
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!"".equals(resourceDir)) {
|
if (!"".equals(resourceDir)) {
|
||||||
ClassPathResource classPathResource = new ClassPathResource(resourceDir);
|
ClassPathResource classPathResource = new ClassPathResource(resourceDir);
|
||||||
|
@ -279,21 +279,16 @@ public class GeneralWebController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public String getFormatFromExtension(String extension) {
|
public String getFormatFromExtension(String extension) {
|
||||||
switch (extension) {
|
return switch (extension) {
|
||||||
case "ttf":
|
case "ttf" -> "truetype";
|
||||||
return "truetype";
|
case "woff" -> "woff";
|
||||||
case "woff":
|
case "woff2" -> "woff2";
|
||||||
return "woff";
|
case "eot" -> "embedded-opentype";
|
||||||
case "woff2":
|
case "svg" -> "svg";
|
||||||
return "woff2";
|
default ->
|
||||||
case "eot":
|
|
||||||
return "embedded-opentype";
|
|
||||||
case "svg":
|
|
||||||
return "svg";
|
|
||||||
default:
|
|
||||||
// or throw an exception if an unexpected extension is encountered
|
// or throw an exception if an unexpected extension is encountered
|
||||||
return "";
|
"";
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/crop")
|
@GetMapping("/crop")
|
||||||
|
Loading…
Reference in New Issue
Block a user