Skip to content

Commit

Permalink
install paths dynmaic (#2668)
Browse files Browse the repository at this point in the history
# 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)
  • Loading branch information
Frooodle authored Jan 12, 2025
1 parent 76cbf94 commit 1bd7e42
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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;
Expand Down Expand Up @@ -118,6 +120,10 @@ public static String getPipelineFinishedFoldersDir() {
return PIPELINE_FINISHED_FOLDERS_PATH;
}

public static String getPipelineDefaultWebUIConfigsDir() {
return PIPELINE_DEFAULT_WEB_UI_CONFIGS;
}

public static String getStaticPath() {
return STATIC_PATH;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -9,6 +11,7 @@

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;

Expand All @@ -17,8 +20,8 @@
@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";
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
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;
Expand All @@ -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;
}
Expand All @@ -56,9 +59,8 @@ public DatabaseService(ApplicationProperties applicationProperties, DataSource d
@Override
public boolean hasBackup() {
createBackupDirectory();
Path filePath = Paths.get(BACKUP_DIR);

if (Files.exists(filePath)) {
if (Files.exists(BACKUP_DIR)) {
return !getBackupList().isEmpty();
}

Expand All @@ -77,11 +79,9 @@ public List<FileInfo> getBackupList() {
if (isH2Database()) {
createBackupDirectory();

Path backupPath = Paths.get(BACKUP_DIR);

try (DirectoryStream<Path> stream =
Files.newDirectoryStream(
backupPath,
BACKUP_DIR,
path ->
path.getFileName().toString().startsWith(BACKUP_PREFIX)
&& path.getFileName()
Expand Down Expand Up @@ -114,10 +114,9 @@ public List<FileInfo> getBackupList() {
}

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);
Expand Down Expand Up @@ -271,7 +270,7 @@ public boolean deleteBackupFile(String fileName) throws IOException {
*/
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");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ public String pipelineForm(Model model) {
model.addAttribute("currentPage", "pipeline");
List<String> pipelineConfigs = new ArrayList<>();
List<Map<String, String>> pipelineConfigsWithNames = new ArrayList<>();
if (new File("./pipeline/defaultWebUIConfigs/").exists()) {
try (Stream<Path> paths = Files.walk(Paths.get("./pipeline/defaultWebUIConfigs/"))) {
if (new File(InstallationPathConfig.getPipelineDefaultWebUIConfigsDir()).exists()) {
try (Stream<Path> paths = Files.walk(Paths.get(InstallationPathConfig.getPipelineDefaultWebUIConfigsDir()))) {
List<Path> jsonFiles =
paths.filter(Files::isRegularFile)
.filter(p -> p.toString().endsWith(".json"))
Expand Down

0 comments on commit 1bd7e42

Please sign in to comment.