From 1bd7e420e53f752ed2192c58619db3c1388748ad Mon Sep 17 00:00:00 2001 From: Anthony Stirling <77850077+Frooodle@users.noreply.github.com> Date: Sun, 12 Jan 2025 00:28:05 +0000 Subject: [PATCH] install paths dynmaic (#2668) # Description Please provide a summary of the changes, including relevant motivation and context. Closes #(issue_number) ## Checklist - [ ] I have read the [Contribution Guidelines](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/CONTRIBUTING.md) - [ ] I have performed a self-review of my own code - [ ] I have attached images of the change if it is UI based - [ ] I have commented my code, particularly in hard-to-understand areas - [ ] If my code has heavily changed functionality I have updated relevant docs on [Stirling-PDFs doc repo](https://github.com/Stirling-Tools/Stirling-Tools.github.io/blob/main/docs/) - [ ] My changes generate no new warnings - [ ] 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) --- .../SPDF/config/InstallationPathConfig.java | 12 +++++++++--- .../security/database/DatabaseConfig.java | 8 ++++++-- .../security/database/DatabaseService.java | 19 +++++++++---------- .../controller/web/GeneralWebController.java | 4 ++-- 4 files changed, 26 insertions(+), 17 deletions(-) diff --git a/src/main/java/stirling/software/SPDF/config/InstallationPathConfig.java b/src/main/java/stirling/software/SPDF/config/InstallationPathConfig.java index e8e1aa1b..a1807375 100644 --- a/src/main/java/stirling/software/SPDF/config/InstallationPathConfig.java +++ b/src/main/java/stirling/software/SPDF/config/InstallationPathConfig.java @@ -22,12 +22,13 @@ public class InstallationPathConfig { // Pipeline paths private static final String PIPELINE_WATCHED_FOLDERS_PATH; private static final String PIPELINE_FINISHED_FOLDERS_PATH; - + private static final String PIPELINE_DEFAULT_WEB_UI_CONFIGS; + // Custom file paths private static final String STATIC_PATH; private static final String TEMPLATES_PATH; private static final String SIGNATURES_PATH; - + static { BASE_PATH = initializeBasePath(); @@ -45,7 +46,8 @@ public class InstallationPathConfig { // Initialize pipeline paths PIPELINE_WATCHED_FOLDERS_PATH = PIPELINE_PATH + "watchedFolders" + File.separator; PIPELINE_FINISHED_FOLDERS_PATH = PIPELINE_PATH + "finishedFolders" + File.separator; - + PIPELINE_DEFAULT_WEB_UI_CONFIGS = PIPELINE_PATH + "defaultWebUIConfigs" + File.separator; + // Initialize custom file paths STATIC_PATH = CUSTOM_FILES_PATH + "static" + File.separator; TEMPLATES_PATH = CUSTOM_FILES_PATH + "templates" + File.separator; @@ -118,6 +120,10 @@ public class InstallationPathConfig { return PIPELINE_FINISHED_FOLDERS_PATH; } + public static String getPipelineDefaultWebUIConfigsDir() { + return PIPELINE_DEFAULT_WEB_UI_CONFIGS; + } + public static String getStaticPath() { return STATIC_PATH; } diff --git a/src/main/java/stirling/software/SPDF/config/security/database/DatabaseConfig.java b/src/main/java/stirling/software/SPDF/config/security/database/DatabaseConfig.java index 124aee5c..81a4b220 100644 --- a/src/main/java/stirling/software/SPDF/config/security/database/DatabaseConfig.java +++ b/src/main/java/stirling/software/SPDF/config/security/database/DatabaseConfig.java @@ -1,5 +1,7 @@ package stirling.software.SPDF.config.security.database; +import java.io.File; + import javax.sql.DataSource; import org.springframework.beans.factory.annotation.Qualifier; @@ -9,6 +11,7 @@ import org.springframework.context.annotation.Configuration; import lombok.Getter; import lombok.extern.slf4j.Slf4j; +import stirling.software.SPDF.config.InstallationPathConfig; import stirling.software.SPDF.model.ApplicationProperties; import stirling.software.SPDF.model.provider.UnsupportedProviderException; @@ -17,8 +20,8 @@ import stirling.software.SPDF.model.provider.UnsupportedProviderException; @Configuration public class DatabaseConfig { - public static final String DATASOURCE_DEFAULT_URL = - "jdbc:h2:file:./configs/stirling-pdf-DB-2.3.232;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE"; + public final String DATASOURCE_DEFAULT_URL; + public static final String DATASOURCE_URL_TEMPLATE = "jdbc:%s://%s:%4d/%s"; public static final String DEFAULT_DRIVER = "org.h2.Driver"; public static final String DEFAULT_USERNAME = "sa"; @@ -30,6 +33,7 @@ public class DatabaseConfig { public DatabaseConfig( ApplicationProperties applicationProperties, @Qualifier("runningEE") boolean runningEE) { + DATASOURCE_DEFAULT_URL = "jdbc:h2:file:" + InstallationPathConfig.getConfigPath() + File.separator + "stirling-pdf-DB-2.3.232;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE"; this.applicationProperties = applicationProperties; this.runningEE = runningEE; } diff --git a/src/main/java/stirling/software/SPDF/config/security/database/DatabaseService.java b/src/main/java/stirling/software/SPDF/config/security/database/DatabaseService.java index 7d413947..adece75d 100644 --- a/src/main/java/stirling/software/SPDF/config/security/database/DatabaseService.java +++ b/src/main/java/stirling/software/SPDF/config/security/database/DatabaseService.java @@ -26,6 +26,7 @@ import org.springframework.jdbc.datasource.init.ScriptException; import org.springframework.stereotype.Service; import lombok.extern.slf4j.Slf4j; +import stirling.software.SPDF.config.InstallationPathConfig; import stirling.software.SPDF.config.interfaces.DatabaseInterface; import stirling.software.SPDF.model.ApplicationProperties; import stirling.software.SPDF.model.exception.BackupNotFoundException; @@ -37,12 +38,14 @@ public class DatabaseService implements DatabaseInterface { public static final String BACKUP_PREFIX = "backup_"; public static final String SQL_SUFFIX = ".sql"; - private static final String BACKUP_DIR = "configs/db/backup/"; + private final Path BACKUP_DIR; private final ApplicationProperties applicationProperties; private final DataSource dataSource; public DatabaseService(ApplicationProperties applicationProperties, DataSource dataSource) { + this.BACKUP_DIR = + Paths.get(InstallationPathConfig.getConfigPath(), "db", "backup").normalize(); this.applicationProperties = applicationProperties; this.dataSource = dataSource; } @@ -56,9 +59,8 @@ public class DatabaseService implements DatabaseInterface { @Override public boolean hasBackup() { createBackupDirectory(); - Path filePath = Paths.get(BACKUP_DIR); - if (Files.exists(filePath)) { + if (Files.exists(BACKUP_DIR)) { return !getBackupList().isEmpty(); } @@ -77,11 +79,9 @@ public class DatabaseService implements DatabaseInterface { if (isH2Database()) { createBackupDirectory(); - Path backupPath = Paths.get(BACKUP_DIR); - try (DirectoryStream stream = Files.newDirectoryStream( - backupPath, + BACKUP_DIR, path -> path.getFileName().toString().startsWith(BACKUP_PREFIX) && path.getFileName() @@ -114,10 +114,9 @@ public class DatabaseService implements DatabaseInterface { } private void createBackupDirectory() { - Path backupPath = Paths.get(BACKUP_DIR); - if (!Files.exists(backupPath)) { + if (!Files.exists(BACKUP_DIR)) { try { - Files.createDirectories(backupPath); + Files.createDirectories(BACKUP_DIR); log.debug("create backup directory: {}", BACKUP_DIR); } catch (IOException e) { log.error("Error create backup directory: {}", e.getMessage(), e); @@ -271,7 +270,7 @@ public class DatabaseService implements DatabaseInterface { */ public Path getBackupFilePath(String fileName) { createBackupDirectory(); - Path filePath = Paths.get(BACKUP_DIR, fileName).normalize(); + Path filePath = BACKUP_DIR.resolve(fileName).normalize(); if (!filePath.startsWith(BACKUP_DIR)) { throw new SecurityException("Path traversal detected"); } diff --git a/src/main/java/stirling/software/SPDF/controller/web/GeneralWebController.java b/src/main/java/stirling/software/SPDF/controller/web/GeneralWebController.java index 3823e088..97d7517a 100644 --- a/src/main/java/stirling/software/SPDF/controller/web/GeneralWebController.java +++ b/src/main/java/stirling/software/SPDF/controller/web/GeneralWebController.java @@ -54,8 +54,8 @@ public class GeneralWebController { model.addAttribute("currentPage", "pipeline"); List pipelineConfigs = new ArrayList<>(); List> pipelineConfigsWithNames = new ArrayList<>(); - if (new File("./pipeline/defaultWebUIConfigs/").exists()) { - try (Stream paths = Files.walk(Paths.get("./pipeline/defaultWebUIConfigs/"))) { + if (new File(InstallationPathConfig.getPipelineDefaultWebUIConfigsDir()).exists()) { + try (Stream paths = Files.walk(Paths.get(InstallationPathConfig.getPipelineDefaultWebUIConfigsDir()))) { List jsonFiles = paths.filter(Files::isRegularFile) .filter(p -> p.toString().endsWith(".json"))