Config: Refactor directory initialization and improve inline docs

Signed-off-by: Michael Mayer <michael@photoprism.app>
This commit is contained in:
Michael Mayer 2023-06-14 16:44:45 +02:00
parent 4430c7953e
commit a7c8f0102f
5 changed files with 48 additions and 21 deletions

View file

@ -219,14 +219,17 @@ func (c *Config) Propagate() {
func (c *Config) Init() error { func (c *Config) Init() error {
start := time.Now() start := time.Now()
// Create configured directory paths.
if err := c.CreateDirectories(); err != nil { if err := c.CreateDirectories(); err != nil {
return err return err
} }
if err := c.initSerial(); err != nil { // Init storage directories with a random serial.
if err := c.InitSerial(); err != nil {
return err return err
} }
// Detect case-insensitive file system.
if insensitive, err := c.CaseInsensitive(); err != nil { if insensitive, err := c.CaseInsensitive(); err != nil {
return err return err
} else if insensitive { } else if insensitive {
@ -234,6 +237,7 @@ func (c *Config) Init() error {
fs.IgnoreCase() fs.IgnoreCase()
} }
// Detect CPU.
if cpuName := cpuid.CPU.BrandName; cpuName != "" { if cpuName := cpuid.CPU.BrandName; cpuName != "" {
log.Debugf("config: running on %s, %s memory detected", clean.Log(cpuid.CPU.BrandName), humanize.Bytes(TotalMem)) 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.initSettings()
c.initHub() c.initHub()
// Propagate configuration.
c.Propagate() c.Propagate()
// Connect to database.
if err := c.connectDb(); err != nil { if err := c.connectDb(); err != nil {
return err return err
} else if !c.Sponsor() { } else if !c.Sponsor() {
@ -284,6 +290,7 @@ func (c *Config) Init() error {
log.Info(MsgSignUp) log.Info(MsgSignUp)
} }
// Show log message.
log.Debugf("config: successfully initialized [%s]", time.Since(start)) log.Debugf("config: successfully initialized [%s]", time.Since(start))
return nil return nil
@ -313,8 +320,8 @@ func (c *Config) readSerial() string {
return "" return ""
} }
// initSerial initializes storage directories with a random serial. // InitSerial initializes storage directories with a random serial.
func (c *Config) initSerial() (err error) { func (c *Config) InitSerial() (err error) {
if c.Serial() != "" { if c.Serial() != "" {
return nil return nil
} }

View file

@ -60,22 +60,6 @@ func findBin(configBin, defaultBin string) (binPath string) {
// CreateDirectories creates directories for storing photos, metadata and cache files. // CreateDirectories creates directories for storing photos, metadata and cache files.
func (c *Config) CreateDirectories() error { 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() == "" { if c.AssetsPath() == "" {
return notFoundError("assets") return notFoundError("assets")
} else if err := os.MkdirAll(c.AssetsPath(), fs.ModeDir); err != nil { } else if err := os.MkdirAll(c.AssetsPath(), fs.ModeDir); err != nil {

26
internal/config/error.go Normal file
View file

@ -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))
}

View file

@ -1,10 +1,14 @@
package config package config
import ( import (
"os"
"github.com/photoprism/photoprism/internal/acl" "github.com/photoprism/photoprism/internal/acl"
"github.com/photoprism/photoprism/internal/customize" "github.com/photoprism/photoprism/internal/customize"
"github.com/photoprism/photoprism/internal/entity" "github.com/photoprism/photoprism/internal/entity"
"github.com/photoprism/photoprism/internal/i18n" "github.com/photoprism/photoprism/internal/i18n"
"github.com/photoprism/photoprism/pkg/fs"
) )
// initSettings initializes user settings from a config file. // initSettings initializes user settings from a config file.
@ -17,13 +21,19 @@ func (c *Config) initSettings() {
c.settings = customize.NewSettings(c.DefaultTheme(), c.DefaultLocale()) c.settings = customize.NewSettings(c.DefaultTheme(), c.DefaultLocale())
// Get filenames to load the settings from. // Get filenames to load the settings from.
configPath := c.ConfigPath()
settingsFile := c.SettingsYaml() settingsFile := c.SettingsYaml()
defaultsFile := c.SettingsYamlDefaults(settingsFile) 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. // Load values from an existing YAML file or create it otherwise.
if err := c.settings.Load(defaultsFile); err == nil { if err := c.settings.Load(defaultsFile); err == nil {
log.Debugf("settings: loaded from %s", defaultsFile) 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) log.Errorf("settings: could not create %s (%s)", settingsFile, err)
} else { } else {
log.Debugf("settings: saved to %s ", settingsFile) log.Debugf("settings: saved to %s ", settingsFile)

View file

@ -284,7 +284,7 @@ func (c *Config) ReSync(token string) (err error) {
// Load backend api credentials from a YAML file. // Load backend api credentials from a YAML file.
func (c *Config) Load() error { func (c *Config) Load() error {
if !fs.FileExists(c.FileName) { 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() mutex.Lock()