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 f11acda3..7d413947 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 @@ -55,6 +55,7 @@ public class DatabaseService implements DatabaseInterface { */ @Override public boolean hasBackup() { + createBackupDirectory(); Path filePath = Paths.get(BACKUP_DIR); if (Files.exists(filePath)) { @@ -74,6 +75,8 @@ public class DatabaseService implements DatabaseInterface { List backupFiles = new ArrayList<>(); if (isH2Database()) { + createBackupDirectory(); + Path backupPath = Paths.get(BACKUP_DIR); try (DirectoryStream stream = @@ -110,6 +113,18 @@ public class DatabaseService implements DatabaseInterface { return backupFiles; } + private void createBackupDirectory() { + Path backupPath = Paths.get(BACKUP_DIR); + if (!Files.exists(backupPath)) { + try { + Files.createDirectories(backupPath); + log.debug("create backup directory: {}", BACKUP_DIR); + } catch (IOException e) { + log.error("Error create backup directory: {}", e.getMessage(), e); + } + } + } + @Override public void importDatabase() { if (!hasBackup()) throw new BackupNotFoundException("No backup scripts were found."); @@ -255,6 +270,7 @@ public class DatabaseService implements DatabaseInterface { * @return the Path object for the given file name */ public Path getBackupFilePath(String fileName) { + createBackupDirectory(); Path filePath = Paths.get(BACKUP_DIR, fileName).normalize(); if (!filePath.startsWith(BACKUP_DIR)) { throw new SecurityException("Path traversal detected");