Stirling-PDF/app/common
Ludy fdc8fab545
refactor(core): centralize temp file handling in CompressController via TempFileManager (#4629)
# Description of Changes

## What was changed
- Introduced `TempFileManager` and injected it into `CompressController`
to centralize and control temporary file lifecycle.
- Replaced ad-hoc `Files.createTempFile(...)` usages with a new
`TempFile` abstraction:
- `compressImagesInPDF(...)` now returns a `TempFile` instead of a
`Path`.
- All intermediate artifacts (original/working/GS/QPDF outputs) are
created via `TempFile` and managed with try-with-resources where
applicable.
- Removed the mutable `List<Path> tempFiles` bookkeeping; cleanup is
handled by `TempFile.close()` and a single `finally` block that closes
all tracked `TempFile` instances.
- Updated save/copy calls to use `TempFile` accessors (`getPath()`,
`getAbsolutePath()`, `getFile()`).
- Hardened error handling:
- Ensured `TempFile` is closed on early exceptions (e.g., in
`compressImagesInPDF`).
- Ghostscript/QPDF helpers now encapsulate their output lifecycle and no
longer accept/require a temp file list.
- Minor Java refinements:
- Used pattern matching for `instanceof` (e.g., `if (ref instanceof
NestedImageReference nestedRef)`).
- Improved and wrapped long log messages for readability and
consistency.

## Why the change was made
- **Resource safety:** Prevent orphaned temp files and reduce
file-descriptor leaks under failure conditions or multi-step pipelines.
- **Consistency:** Establish a single, testable mechanism for temp file
creation, placement, and cleanup across compression flows.
- **Portability & stability:** Avoid Windows file-locking/delete-in-use
issues by using explicit close semantics and predictable lifetimes.
- **Maintainability:** Simplify control flow by removing ad-hoc temp
tracking and pushing lifecycle ownership into `TempFile`.

---

## 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/devGuide/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/devGuide/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/devGuide/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/devGuide/DeveloperGuide.md#6-testing)
for more details.
2025-10-29 23:21:30 +00:00
..
src refactor(core): centralize temp file handling in CompressController via TempFileManager (#4629) 2025-10-29 23:21:30 +00:00
.gitignore refactor: move modules under app/ directory and update file paths (#3938) 2025-07-14 20:53:11 +01:00
build.gradle build(deps): bump com.github.junrar:junrar from 7.5.5 to 7.5.7 (#4715) 2025-10-21 13:41:00 +01:00