Fix: string comparison and formatting inconsistencies in CompressController (#3168)

# Description of Changes

Please provide a summary of the changes, including:

- Replaced `format.equals("jpeg")` with `"jpeg".equals(format)` to
prevent potential `NullPointerException`
- Standardized percentage reduction logging by formatting values before
passing them into the log statement
- Fixed inconsistent formatting in log messages by replacing `{:.1f}%`
with pre-formatted string values

`63.32 MB → 61.77 MB (reduced by {:.1f}%)` -> `63.32 MB → 61.77 MB
(reduced by 2.5%)`

These changes improve code robustness and ensure consistent logging
output.

---

## Checklist

### General

- [x] I have read the [Contribution
Guidelines](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/CONTRIBUTING.md)
- [x] I have read the [Stirling-PDF Developer
Guide](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/DeveloperGuide.md)
(if applicable)
- [ ] I have read the [How to add new languages to
Stirling-PDF](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/HowToAddNewLanguage.md)
(if applicable)
- [x] I have performed a self-review of my own code
- [x] My changes generate no new warnings

### Documentation

- [ ] I have updated relevant docs on [Stirling-PDF's doc
repo](https://github.com/Stirling-Tools/Stirling-Tools.github.io/blob/main/docs/)
(if functionality has heavily changed)
- [ ] I have read the section [Add New Translation
Tags](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/HowToAddNewLanguage.md#add-new-translation-tags)
(for new translation tags only)

### UI Changes (if applicable)

- [ ] Screenshots or videos demonstrating the UI changes are attached
(e.g., as comments or direct attachments in the PR)

### Testing (if applicable)

- [ ] I have tested my changes locally. Refer to the [Testing
Guide](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/DeveloperGuide.md#6-testing)
for more details.
This commit is contained in:
Ludy 2025-03-13 10:22:06 +01:00 committed by GitHub
parent e4dbe7f9b0
commit 4408ecfa5b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 12 additions and 9 deletions

View File

@ -289,12 +289,15 @@ tasks.wrapper {
// options.compilerArgs << "-Xlint:deprecation"
//}
configurations.all {
// Remove all commons-logging dependencies so that only spring-jcl is used
exclude group: 'commons-logging', module: 'commons-logging'
// Exclude Tomcat
exclude group: "org.springframework.boot", module: "spring-boot-starter-tomcat"
}
dependencies {
//tmp for security bumps
implementation 'ch.qos.logback:logback-core:1.5.17'
implementation 'ch.qos.logback:logback-core:1.5.17'
implementation 'ch.qos.logback:logback-classic:1.5.17'

View File

@ -209,7 +209,7 @@ public class CompressController {
// First get the actual size of the original image by encoding it to the chosen
// format
ByteArrayOutputStream originalImageStream = new ByteArrayOutputStream();
if (format.equals("jpeg")) {
if ("jpeg".equals(format)) {
// Get the best available JPEG writer (prioritizes TwelveMonkeys if
// available)
Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName("jpeg");
@ -252,7 +252,7 @@ public class CompressController {
// Now compress the scaled image
ByteArrayOutputStream compressedImageStream = new ByteArrayOutputStream();
if (format.equals("jpeg")) {
if ("jpeg".equals(format)) {
Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName(format);
if (writers.hasNext()) {
ImageWriter writer = writers.next();
@ -338,10 +338,10 @@ public class CompressController {
compressedImages,
skippedImages);
log.info(
"Total original image size: {}, compressed: {} (reduced by {:.1f}%)",
"Total original image size: {}, compressed: {} (reduced by {}%)",
GeneralUtils.formatBytes(totalOriginalBytes),
GeneralUtils.formatBytes(totalCompressedBytes),
overallImageReduction);
String.format("%.1f", overallImageReduction));
// Save the document
log.info("Saving compressed PDF to {}", pdfFile.toString());
@ -351,10 +351,10 @@ public class CompressController {
long compressedFileSize = Files.size(pdfFile);
double overallReduction = 100.0 - ((compressedFileSize * 100.0) / originalFileSize);
log.info(
"Overall PDF compression: {} → {} (reduced by {:.1f}%)",
"Overall PDF compression: {} → {} (reduced by {}%)",
GeneralUtils.formatBytes(originalFileSize),
GeneralUtils.formatBytes(compressedFileSize),
overallReduction);
String.format("%.1f", overallReduction));
}
}
@ -477,8 +477,8 @@ public class CompressController {
long postQpdfSize = Files.size(tempOutputFile);
double qpdfReduction = 100.0 - ((postQpdfSize * 100.0) / preQpdfSize);
log.info(
"Post-QPDF file size: {} (reduced by {:.1f}%)",
GeneralUtils.formatBytes(postQpdfSize), qpdfReduction);
"Post-QPDF file size: {} (reduced by {}%)",
GeneralUtils.formatBytes(postQpdfSize), String.format("%.1f", qpdfReduction));
} else {
tempOutputFile = tempInputFile;