mirror of
https://github.com/databasus/databasus.git
synced 2026-04-06 00:32:03 +02:00
FIX (backups): Validate data and temp directory exist on app start (not only for LocalStorage)
This commit is contained in:
@@ -50,6 +50,12 @@ func main() {
|
||||
|
||||
runMigrations(log)
|
||||
|
||||
// create directories that used for backups and restore
|
||||
files_utils.EnsureDirectories([]string{
|
||||
config.GetEnv().TempFolder,
|
||||
config.GetEnv().DataFolder,
|
||||
})
|
||||
|
||||
// Handle password reset if flag is provided
|
||||
newPassword := flag.String("new-password", "", "Set a new password for the user")
|
||||
flag.Parse()
|
||||
|
||||
@@ -25,11 +25,6 @@ func (l *LocalStorage) TableName() string {
|
||||
func (l *LocalStorage) SaveFile(logger *slog.Logger, fileID uuid.UUID, file io.Reader) error {
|
||||
logger.Info("Starting to save file to local storage", "fileId", fileID.String())
|
||||
|
||||
if err := l.ensureDirectories(); err != nil {
|
||||
logger.Error("Failed to ensure directories", "fileId", fileID.String(), "error", err)
|
||||
return err
|
||||
}
|
||||
|
||||
tempFilePath := filepath.Join(config.GetEnv().TempFolder, fileID.String())
|
||||
logger.Debug("Creating temp file", "fileId", fileID.String(), "tempPath", tempFilePath)
|
||||
|
||||
@@ -134,14 +129,11 @@ func (l *LocalStorage) DeleteFile(fileID uuid.UUID) error {
|
||||
}
|
||||
|
||||
func (l *LocalStorage) Validate() error {
|
||||
return l.ensureDirectories()
|
||||
// nothing to validate
|
||||
return nil
|
||||
}
|
||||
|
||||
func (l *LocalStorage) TestConnection() error {
|
||||
if err := l.ensureDirectories(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
testFile := filepath.Join(config.GetEnv().TempFolder, "test_connection")
|
||||
f, err := os.Create(testFile)
|
||||
if err != nil {
|
||||
@@ -157,19 +149,3 @@ func (l *LocalStorage) TestConnection() error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (l *LocalStorage) ensureDirectories() error {
|
||||
// Standard permissions for directories: owner
|
||||
// can read/write/execute, others can read/execute
|
||||
const directoryPermissions = 0755
|
||||
|
||||
if err := os.MkdirAll(config.GetEnv().DataFolder, directoryPermissions); err != nil {
|
||||
return fmt.Errorf("failed to create backups directory: %w", err)
|
||||
}
|
||||
|
||||
if err := os.MkdirAll(config.GetEnv().TempFolder, directoryPermissions); err != nil {
|
||||
return fmt.Errorf("failed to create temp directory: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
20
backend/internal/util/files/creator.go
Normal file
20
backend/internal/util/files/creator.go
Normal file
@@ -0,0 +1,20 @@
|
||||
package files_utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
func EnsureDirectories(directories []string) error {
|
||||
// Standard permissions for directories: owner
|
||||
// can read/write/execute, others can read/execute
|
||||
const directoryPermissions = 0755
|
||||
|
||||
for _, directory := range directories {
|
||||
if err := os.MkdirAll(directory, directoryPermissions); err != nil {
|
||||
return fmt.Errorf("failed to create directory %s: %w", directory, err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user