fix(config): recreate settings.yml if missing or below minimal size threshold (#4166)

# 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.
This commit is contained in:
Ludy 2025-08-24 22:47:09 +02:00 committed by GitHub
parent f0cfd87a5a
commit 73df0ae1a8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -23,10 +23,30 @@ import stirling.software.common.util.YamlHelper;
@Slf4j @Slf4j
public class ConfigInitializer { public class ConfigInitializer {
private static final int MIN_SETTINGS_FILE_LINES = 31;
public void ensureConfigExists() throws IOException, URISyntaxException { public void ensureConfigExists() throws IOException, URISyntaxException {
// 1) If settings file doesn't exist, create from template // 1) If settings file doesn't exist, create from template
Path destPath = Paths.get(InstallationPathConfig.getSettingsPath()); 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()); Files.createDirectories(destPath.getParent());
try (InputStream in = try (InputStream in =
getClass().getClassLoader().getResourceAsStream("settings.yml.template")) { getClass().getClassLoader().getResourceAsStream("settings.yml.template")) {