mirror of
https://github.com/Stirling-Tools/Stirling-PDF.git
synced 2025-05-31 14:22:01 +00:00
Fix/full invert crash 2942 (#2957)
# Description of Changes Please provide a summary of the changes, including: - What was changed - Modified the `convertToBufferedImageTpFile` to use `File.createTempFile()` instead of writing to `"image.png"` in the current directory. - This change ensures the file is saved in the default temporary directory, preventing permission issues. - Why the change was made - Previously, the method attempted to save the file in the current working directory, which caused permission errors (`java.io.FileNotFoundException: image.png (Permission denied)`). - Any challenges encountered Closes #2942 --- ## 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) - [x] 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:
parent
c1d7217242
commit
82b1ab4263
@ -30,8 +30,10 @@ public class InvertFullColorStrategy extends ReplaceAndInvertColorStrategy {
|
|||||||
@Override
|
@Override
|
||||||
public InputStreamResource replace() throws IOException {
|
public InputStreamResource replace() throws IOException {
|
||||||
|
|
||||||
|
File file = null;
|
||||||
|
try {
|
||||||
// Create a temporary file, with the original filename from the multipart file
|
// Create a temporary file, with the original filename from the multipart file
|
||||||
File file = Files.createTempFile("temp", getFileInput().getOriginalFilename()).toFile();
|
file = Files.createTempFile("temp", getFileInput().getOriginalFilename()).toFile();
|
||||||
|
|
||||||
// Transfer the content of the multipart file to the file
|
// Transfer the content of the multipart file to the file
|
||||||
getFileInput().transferTo(file);
|
getFileInput().transferTo(file);
|
||||||
@ -50,13 +52,18 @@ public class InvertFullColorStrategy extends ReplaceAndInvertColorStrategy {
|
|||||||
|
|
||||||
// Create a new PDPage from the inverted image
|
// Create a new PDPage from the inverted image
|
||||||
PDPage pdPage = document.getPage(page);
|
PDPage pdPage = document.getPage(page);
|
||||||
|
File tempImageFile = null;
|
||||||
|
try {
|
||||||
|
tempImageFile = convertToBufferedImageTpFile(image);
|
||||||
PDImageXObject pdImage =
|
PDImageXObject pdImage =
|
||||||
PDImageXObject.createFromFileByContent(
|
PDImageXObject.createFromFileByContent(tempImageFile, document);
|
||||||
convertToBufferedImageTpFile(image), document);
|
|
||||||
|
|
||||||
PDPageContentStream contentStream =
|
PDPageContentStream contentStream =
|
||||||
new PDPageContentStream(
|
new PDPageContentStream(
|
||||||
document, pdPage, PDPageContentStream.AppendMode.OVERWRITE, true);
|
document,
|
||||||
|
pdPage,
|
||||||
|
PDPageContentStream.AppendMode.OVERWRITE,
|
||||||
|
true);
|
||||||
contentStream.drawImage(
|
contentStream.drawImage(
|
||||||
pdImage,
|
pdImage,
|
||||||
0,
|
0,
|
||||||
@ -64,6 +71,11 @@ public class InvertFullColorStrategy extends ReplaceAndInvertColorStrategy {
|
|||||||
pdPage.getMediaBox().getWidth(),
|
pdPage.getMediaBox().getWidth(),
|
||||||
pdPage.getMediaBox().getHeight());
|
pdPage.getMediaBox().getHeight());
|
||||||
contentStream.close();
|
contentStream.close();
|
||||||
|
} finally {
|
||||||
|
if (tempImageFile != null && tempImageFile.exists()) {
|
||||||
|
Files.delete(tempImageFile.toPath());
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Save the modified PDF to a ByteArrayOutputStream
|
// Save the modified PDF to a ByteArrayOutputStream
|
||||||
@ -76,6 +88,11 @@ public class InvertFullColorStrategy extends ReplaceAndInvertColorStrategy {
|
|||||||
new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
|
new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
|
||||||
InputStreamResource resource = new InputStreamResource(inputStream);
|
InputStreamResource resource = new InputStreamResource(inputStream);
|
||||||
return resource;
|
return resource;
|
||||||
|
} finally {
|
||||||
|
if (file != null && file.exists()) {
|
||||||
|
Files.delete(file.toPath());
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Method to invert image colors
|
// Method to invert image colors
|
||||||
@ -98,7 +115,7 @@ public class InvertFullColorStrategy extends ReplaceAndInvertColorStrategy {
|
|||||||
|
|
||||||
// Helper method to convert BufferedImage to InputStream
|
// Helper method to convert BufferedImage to InputStream
|
||||||
private File convertToBufferedImageTpFile(BufferedImage image) throws IOException {
|
private File convertToBufferedImageTpFile(BufferedImage image) throws IOException {
|
||||||
File file = new File("image.png");
|
File file = File.createTempFile("image", ".png");
|
||||||
ImageIO.write(image, "png", file);
|
ImageIO.write(image, "png", file);
|
||||||
return file;
|
return file;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user