From 73df0ae1a8fe0ea52f2c7d4345d239ed94e65293 Mon Sep 17 00:00:00 2001 From: Ludy Date: Sun, 24 Aug 2025 22:47:09 +0200 Subject: [PATCH] fix(config): recreate settings.yml if missing or below minimal size threshold (#4166) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit # Description of Changes - Added logic to verify the `settings.yml` file’s existence **and** ensure it has at least 31 lines (minimum valid config since `v0.13.0`). - If the file exists but is too small, it is moved to a timestamped `.bak` backup before creating a new one from the template. - Added logging to show current line count and backup location for better traceability. --- ## 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. --- .../configuration/ConfigInitializer.java | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/app/common/src/main/java/stirling/software/common/configuration/ConfigInitializer.java b/app/common/src/main/java/stirling/software/common/configuration/ConfigInitializer.java index 50090ee51..54e42504c 100644 --- a/app/common/src/main/java/stirling/software/common/configuration/ConfigInitializer.java +++ b/app/common/src/main/java/stirling/software/common/configuration/ConfigInitializer.java @@ -23,10 +23,30 @@ import stirling.software.common.util.YamlHelper; @Slf4j public class ConfigInitializer { + private static final int MIN_SETTINGS_FILE_LINES = 31; + public void ensureConfigExists() throws IOException, URISyntaxException { // 1) If settings file doesn't exist, create from template Path destPath = Paths.get(InstallationPathConfig.getSettingsPath()); - if (Files.notExists(destPath)) { + + boolean settingsFileExists = Files.exists(destPath); + + long lineCount = settingsFileExists ? Files.readAllLines(destPath).size() : 0; + + log.info("Current settings file line count: {}", lineCount); + + if (!settingsFileExists || lineCount < MIN_SETTINGS_FILE_LINES) { + if (settingsFileExists) { + // move settings.yml to settings.yml.{timestamp}.bak + Path backupPath = + Paths.get( + InstallationPathConfig.getSettingsPath() + + "." + + System.currentTimeMillis() + + ".bak"); + Files.move(destPath, backupPath, StandardCopyOption.REPLACE_EXISTING); + log.info("Moved existing settings file to backup: {}", backupPath); + } Files.createDirectories(destPath.getParent()); try (InputStream in = getClass().getClassLoader().getResourceAsStream("settings.yml.template")) {