From bb4d313b55c686a261f34977292903a6e7b5c233 Mon Sep 17 00:00:00 2001 From: Ludy Date: Tue, 25 Nov 2025 10:54:31 +0100 Subject: [PATCH] refactor(common): remove unused temp directory & HTML unzip helpers, prune imports (#4857) # Description of Changes This pull request primarily removes several unused or redundant utility methods related to temporary directory and file management across the codebase. The changes help simplify the code and reduce maintenance overhead by eliminating code that is no longer needed. **Cleanup of temporary file and directory utilities:** * Removed the `createTempDirectory` method from `CustomPDFDocumentFactory`, which created uniquely named temporary directories. * Removed the `getTempDirectory` method from `GeneralUtils`, which handled custom and default temporary directory configuration. **Codebase simplification in file utilities:** * Deleted the `deleteDirectory` and `unzipAndGetMainHtml` methods from `FileToPdf`, which were used for recursively deleting directories and extracting the main HTML file from a ZIP archive, respectively. * Cleaned up unused imports in `FileToPdf` that were only needed for the removed methods. --- ## 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) ### Translations (if applicable) - [ ] I ran [`scripts/counter_translation.py`](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/docs/counter_translation.md) ### 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. --- .../service/CustomPDFDocumentFactory.java | 5 -- .../software/common/util/FileToPdf.java | 61 ------------------- .../software/common/util/GeneralUtils.java | 26 -------- 3 files changed, 92 deletions(-) diff --git a/app/common/src/main/java/stirling/software/common/service/CustomPDFDocumentFactory.java b/app/common/src/main/java/stirling/software/common/service/CustomPDFDocumentFactory.java index 8d7419eb7..4e2b4c789 100644 --- a/app/common/src/main/java/stirling/software/common/service/CustomPDFDocumentFactory.java +++ b/app/common/src/main/java/stirling/software/common/service/CustomPDFDocumentFactory.java @@ -478,11 +478,6 @@ public class CustomPDFDocumentFactory { return file; } - /** Create a uniquely named temporary directory */ - private Path createTempDirectory(String prefix) throws IOException { - return Files.createTempDirectory(prefix + tempCounter.incrementAndGet() + "-"); - } - /** Create new document bytes based on an existing document */ public byte[] createNewBytesBasedOnOldDocument(byte[] oldDocument) throws IOException { try (PDDocument document = load(oldDocument)) { diff --git a/app/common/src/main/java/stirling/software/common/util/FileToPdf.java b/app/common/src/main/java/stirling/software/common/util/FileToPdf.java index 30a478294..6e0fec08e 100644 --- a/app/common/src/main/java/stirling/software/common/util/FileToPdf.java +++ b/app/common/src/main/java/stirling/software/common/util/FileToPdf.java @@ -5,11 +5,8 @@ import java.io.FileOutputStream; import java.io.IOException; import java.io.UncheckedIOException; import java.nio.charset.StandardCharsets; -import java.nio.file.FileVisitResult; import java.nio.file.Files; import java.nio.file.Path; -import java.nio.file.SimpleFileVisitor; -import java.nio.file.attribute.BasicFileAttributes; import java.util.ArrayList; import java.util.List; import java.util.Locale; @@ -148,64 +145,6 @@ public class FileToPdf { } } - private static void deleteDirectory(Path dir) throws IOException { - Files.walkFileTree( - dir, - new SimpleFileVisitor() { - @Override - public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) - throws IOException { - Files.delete(file); - return FileVisitResult.CONTINUE; - } - - @Override - public FileVisitResult postVisitDirectory(Path dir, IOException exc) - throws IOException { - Files.delete(dir); - return FileVisitResult.CONTINUE; - } - }); - } - - private static Path unzipAndGetMainHtml(byte[] fileBytes) throws IOException { - Path tempDirectory = Files.createTempDirectory("unzipped_"); - try (ZipInputStream zipIn = - ZipSecurity.createHardenedInputStream(new ByteArrayInputStream(fileBytes))) { - ZipEntry entry = zipIn.getNextEntry(); - while (entry != null) { - Path filePath = tempDirectory.resolve(sanitizeZipFilename(entry.getName())); - if (entry.isDirectory()) { - Files.createDirectories(filePath); // Explicitly create the directory structure - } else { - Files.createDirectories( - filePath.getParent()); // Create parent directories if they don't exist - Files.copy(zipIn, filePath); - } - zipIn.closeEntry(); - entry = zipIn.getNextEntry(); - } - } - - // Search for the main HTML file. - try (Stream walk = Files.walk(tempDirectory)) { - List htmlFiles = walk.filter(file -> file.toString().endsWith(".html")).toList(); - - if (htmlFiles.isEmpty()) { - throw new IOException("No HTML files found in the unzipped directory."); - } - - // Prioritize 'index.html' if it exists, otherwise use the first .html file - for (Path htmlFile : htmlFiles) { - if ("index.html".equals(htmlFile.getFileName().toString())) { - return htmlFile; - } - } - - return htmlFiles.get(0); - } - } - static String sanitizeZipFilename(String entryName) { if (entryName == null || entryName.trim().isEmpty()) { return ""; diff --git a/app/common/src/main/java/stirling/software/common/util/GeneralUtils.java b/app/common/src/main/java/stirling/software/common/util/GeneralUtils.java index 874417990..1379b7459 100644 --- a/app/common/src/main/java/stirling/software/common/util/GeneralUtils.java +++ b/app/common/src/main/java/stirling/software/common/util/GeneralUtils.java @@ -94,32 +94,6 @@ public class GeneralUtils { return tempFile; } - /* - * Gets the configured temporary directory, creating it if necessary. - * - * @return Path to the temporary directory - * @throws IOException if directory creation fails - */ - private Path getTempDirectory() throws IOException { - String customTempDir = System.getenv("STIRLING_TEMPFILES_DIRECTORY"); - if (customTempDir == null || customTempDir.isEmpty()) { - customTempDir = System.getProperty("stirling.tempfiles.directory"); - } - - Path tempDir; - if (customTempDir != null && !customTempDir.isEmpty()) { - tempDir = Path.of(customTempDir); - } else { - tempDir = Path.of(System.getProperty("java.io.tmpdir"), "stirling-pdf"); - } - - if (!Files.exists(tempDir)) { - Files.createDirectories(tempDir); - } - - return tempDir; - } - /* * Remove file extension *