diff --git a/internal/config/config.go b/internal/config/config.go index 7b998abbf..b0110d7ab 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -219,14 +219,17 @@ func (c *Config) Propagate() { func (c *Config) Init() error { start := time.Now() + // Create configured directory paths. if err := c.CreateDirectories(); err != nil { return err } - if err := c.initSerial(); err != nil { + // Init storage directories with a random serial. + if err := c.InitSerial(); err != nil { return err } + // Detect case-insensitive file system. if insensitive, err := c.CaseInsensitive(); err != nil { return err } else if insensitive { @@ -234,6 +237,7 @@ func (c *Config) Init() error { fs.IgnoreCase() } + // Detect CPU. if cpuName := cpuid.CPU.BrandName; cpuName != "" { log.Debugf("config: running on %s, %s memory detected", clean.Log(cpuid.CPU.BrandName), humanize.Bytes(TotalMem)) } @@ -275,8 +279,10 @@ func (c *Config) Init() error { c.initSettings() c.initHub() + // Propagate configuration. c.Propagate() + // Connect to database. if err := c.connectDb(); err != nil { return err } else if !c.Sponsor() { @@ -284,6 +290,7 @@ func (c *Config) Init() error { log.Info(MsgSignUp) } + // Show log message. log.Debugf("config: successfully initialized [%s]", time.Since(start)) return nil @@ -313,8 +320,8 @@ func (c *Config) readSerial() string { return "" } -// initSerial initializes storage directories with a random serial. -func (c *Config) initSerial() (err error) { +// InitSerial initializes storage directories with a random serial. +func (c *Config) InitSerial() (err error) { if c.Serial() != "" { return nil } diff --git a/internal/config/config_filepaths.go b/internal/config/config_filepaths.go index 2e455ac47..881bfc42d 100644 --- a/internal/config/config_filepaths.go +++ b/internal/config/config_filepaths.go @@ -60,22 +60,6 @@ func findBin(configBin, defaultBin string) (binPath string) { // CreateDirectories creates directories for storing photos, metadata and cache files. func (c *Config) CreateDirectories() error { - createError := func(path string, err error) (result error) { - if fs.FileExists(path) { - result = fmt.Errorf("directory path %s is a file, please check your configuration", clean.Log(path)) - } else { - result = fmt.Errorf("failed to create the directory %s, check configuration and permissions", clean.Log(path)) - } - - log.Debug(err) - - return result - } - - notFoundError := func(name string) error { - return fmt.Errorf("invalid %s path, check configuration and permissions", clean.Log(name)) - } - if c.AssetsPath() == "" { return notFoundError("assets") } else if err := os.MkdirAll(c.AssetsPath(), fs.ModeDir); err != nil { diff --git a/internal/config/error.go b/internal/config/error.go new file mode 100644 index 000000000..3b52c1941 --- /dev/null +++ b/internal/config/error.go @@ -0,0 +1,26 @@ +package config + +import ( + "fmt" + + "github.com/photoprism/photoprism/pkg/clean" + "github.com/photoprism/photoprism/pkg/fs" +) + +// createError returns a new directory create error. +func createError(path string, err error) (result error) { + if fs.FileExists(path) { + result = fmt.Errorf("directory path %s is a file, please check your configuration", clean.Log(path)) + } else { + result = fmt.Errorf("failed to create the directory %s, check configuration and permissions", clean.Log(path)) + } + + log.Debug(err) + + return result +} + +// notFoundError returns a new directory not found error. +func notFoundError(name string) error { + return fmt.Errorf("invalid %s path, check configuration and permissions", clean.Log(name)) +} diff --git a/internal/config/settings.go b/internal/config/settings.go index bd211c9ca..8ae78e141 100644 --- a/internal/config/settings.go +++ b/internal/config/settings.go @@ -1,10 +1,14 @@ package config import ( + "os" + "github.com/photoprism/photoprism/internal/acl" "github.com/photoprism/photoprism/internal/customize" "github.com/photoprism/photoprism/internal/entity" "github.com/photoprism/photoprism/internal/i18n" + + "github.com/photoprism/photoprism/pkg/fs" ) // initSettings initializes user settings from a config file. @@ -17,13 +21,19 @@ func (c *Config) initSettings() { c.settings = customize.NewSettings(c.DefaultTheme(), c.DefaultLocale()) // Get filenames to load the settings from. + configPath := c.ConfigPath() settingsFile := c.SettingsYaml() defaultsFile := c.SettingsYamlDefaults(settingsFile) + // Make sure that the config path exists. + if err := os.MkdirAll(configPath, fs.ModeDir); err != nil { + log.Errorf("settings: %s", createError(configPath, err)) + } + // Load values from an existing YAML file or create it otherwise. if err := c.settings.Load(defaultsFile); err == nil { log.Debugf("settings: loaded from %s", defaultsFile) - } else if err := c.settings.Save(settingsFile); err != nil { + } else if err = c.settings.Save(settingsFile); err != nil { log.Errorf("settings: could not create %s (%s)", settingsFile, err) } else { log.Debugf("settings: saved to %s ", settingsFile) diff --git a/internal/hub/config.go b/internal/hub/config.go index 75a59150c..804d1ed85 100644 --- a/internal/hub/config.go +++ b/internal/hub/config.go @@ -284,7 +284,7 @@ func (c *Config) ReSync(token string) (err error) { // Load backend api credentials from a YAML file. func (c *Config) Load() error { if !fs.FileExists(c.FileName) { - return fmt.Errorf("settings file not found: %s", clean.Log(c.FileName)) + return fmt.Errorf("%s not found", clean.Log(c.FileName)) } mutex.Lock()