Skip to content

Commit

Permalink
Fix: NoSuchFileException if configs\db\backup is not present on f…
Browse files Browse the repository at this point in the history
…irst start
  • Loading branch information
Ludy87 committed Jan 11, 2025
1 parent 2241e2c commit fbcdcb3
Showing 1 changed file with 16 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public DatabaseService(ApplicationProperties applicationProperties, DataSource d
*/
@Override
public boolean hasBackup() {
createBackupDirectory();
Path filePath = Paths.get(BACKUP_DIR);

if (Files.exists(filePath)) {
Expand All @@ -74,6 +75,8 @@ public List<FileInfo> getBackupList() {
List<FileInfo> backupFiles = new ArrayList<>();

if (isH2Database()) {
createBackupDirectory();

Path backupPath = Paths.get(BACKUP_DIR);

try (DirectoryStream<Path> stream =
Expand Down Expand Up @@ -110,6 +113,18 @@ public List<FileInfo> getBackupList() {
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.");
Expand Down Expand Up @@ -255,6 +270,7 @@ public boolean deleteBackupFile(String fileName) throws IOException {
* @return the <code>Path</code> 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");
Expand Down

0 comments on commit fbcdcb3

Please sign in to comment.