photoprism/config.go

138 lines
3 KiB
Go
Raw Normal View History

2018-02-04 17:34:07 +01:00
package photoprism
import (
2018-07-18 15:17:56 +02:00
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/mssql"
_ "github.com/jinzhu/gorm/dialects/mysql"
_ "github.com/jinzhu/gorm/dialects/postgres"
_ "github.com/jinzhu/gorm/dialects/sqlite"
2018-02-04 17:34:07 +01:00
"github.com/kylelemons/go-gypsy/yaml"
"github.com/urfave/cli"
2018-07-18 15:17:56 +02:00
"log"
2018-06-17 12:56:02 +02:00
"os"
"path"
2018-02-04 17:34:07 +01:00
)
type Config struct {
ConfigFile string
DarktableCli string
OriginalsPath string
ThumbnailsPath string
ImportPath string
ExportPath string
2018-07-18 15:17:56 +02:00
DatabaseDriver string
DatabaseDsn string
db *gorm.DB
2018-02-04 17:34:07 +01:00
}
func NewConfig() *Config {
return &Config{}
}
func (c *Config) SetValuesFromFile(fileName string) error {
2018-02-04 17:34:07 +01:00
yamlConfig, err := yaml.ReadFile(fileName)
if err != nil {
return err
}
c.ConfigFile = fileName
2018-02-04 17:34:07 +01:00
if OriginalsPath, err := yamlConfig.Get("originals-path"); err == nil {
2018-02-27 19:51:52 +01:00
c.OriginalsPath = GetExpandedFilename(OriginalsPath)
2018-02-04 17:34:07 +01:00
}
if ThumbnailsPath, err := yamlConfig.Get("thumbnails-path"); err == nil {
2018-02-27 19:51:52 +01:00
c.ThumbnailsPath = GetExpandedFilename(ThumbnailsPath)
2018-02-04 17:34:07 +01:00
}
if ImportPath, err := yamlConfig.Get("import-path"); err == nil {
2018-02-27 19:51:52 +01:00
c.ImportPath = GetExpandedFilename(ImportPath)
2018-02-04 17:34:07 +01:00
}
if ExportPath, err := yamlConfig.Get("export-path"); err == nil {
2018-02-27 19:51:52 +01:00
c.ExportPath = GetExpandedFilename(ExportPath)
2018-02-04 17:34:07 +01:00
}
if DarktableCli, err := yamlConfig.Get("darktable-cli"); err == nil {
2018-02-27 19:51:52 +01:00
c.DarktableCli = GetExpandedFilename(DarktableCli)
2018-02-04 17:34:07 +01:00
}
2018-07-18 15:17:56 +02:00
if DatabaseDriver, err := yamlConfig.Get("database-driver"); err == nil {
c.DatabaseDriver = DatabaseDriver
}
if DatabaseDsn, err := yamlConfig.Get("database-dsn"); err == nil {
c.DatabaseDsn = DatabaseDsn
}
2018-02-04 17:34:07 +01:00
return nil
}
func (c *Config) SetValuesFromCliContext(context *cli.Context) error {
if context.IsSet("originals-path") {
2018-02-27 19:51:52 +01:00
c.OriginalsPath = GetExpandedFilename(context.String("originals-path"))
2018-02-04 17:34:07 +01:00
}
if context.IsSet("thumbnails-path") {
2018-02-27 19:51:52 +01:00
c.ThumbnailsPath = GetExpandedFilename(context.String("thumbnails-path"))
2018-02-04 17:34:07 +01:00
}
if context.IsSet("import-path") {
2018-02-27 19:51:52 +01:00
c.ImportPath = GetExpandedFilename(context.String("import-path"))
2018-02-04 17:34:07 +01:00
}
if context.IsSet("export-path") {
2018-02-27 19:51:52 +01:00
c.ExportPath = GetExpandedFilename(context.String("export-path"))
2018-02-04 17:34:07 +01:00
}
if context.IsSet("darktable-cli") {
2018-02-27 19:51:52 +01:00
c.DarktableCli = GetExpandedFilename(context.String("darktable-cli"))
2018-02-04 17:34:07 +01:00
}
2018-07-18 15:17:56 +02:00
if context.IsSet("database-driver") {
c.DatabaseDriver = context.String("database-driver")
}
if context.IsSet("database-dsn") {
c.DatabaseDsn = context.String("database-dsn")
}
2018-02-04 17:34:07 +01:00
return nil
}
2018-06-17 12:56:02 +02:00
func (c *Config) CreateDirectories() {
os.MkdirAll(path.Dir(c.OriginalsPath), os.ModePerm)
os.MkdirAll(path.Dir(c.ThumbnailsPath), os.ModePerm)
os.MkdirAll(path.Dir(c.ImportPath), os.ModePerm)
os.MkdirAll(path.Dir(c.ExportPath), os.ModePerm)
}
2018-07-18 15:17:56 +02:00
func (c *Config) ConnectToDatabase() error {
db, err := gorm.Open(c.DatabaseDriver, c.DatabaseDsn)
if err != nil || db == nil {
log.Fatal(err)
}
c.db = db
c.MigrateDb()
return err
}
func (c *Config) GetDb() *gorm.DB {
if c.db == nil {
c.ConnectToDatabase()
}
return c.db
}
func (c *Config) MigrateDb() {
db := c.GetDb()
db.AutoMigrate(&File{}, &Photo{}, &Tag{}, &Album{})
}