2019-05-06 23:18:10 +02:00
|
|
|
package config
|
2019-05-03 18:57:28 +02:00
|
|
|
|
|
|
|
import (
|
2019-06-03 22:58:15 +02:00
|
|
|
"context"
|
2019-05-04 05:25:00 +02:00
|
|
|
"errors"
|
2019-05-03 18:57:28 +02:00
|
|
|
"os"
|
2019-06-06 14:44:29 +02:00
|
|
|
"os/exec"
|
2019-06-30 05:38:39 +02:00
|
|
|
"path/filepath"
|
2019-05-03 18:57:28 +02:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/jinzhu/gorm"
|
|
|
|
_ "github.com/jinzhu/gorm/dialects/mysql"
|
|
|
|
_ "github.com/jinzhu/gorm/dialects/sqlite"
|
|
|
|
"github.com/photoprism/photoprism/internal/models"
|
|
|
|
"github.com/photoprism/photoprism/internal/tidb"
|
2019-05-06 23:18:10 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/util"
|
2019-05-03 18:57:28 +02:00
|
|
|
log "github.com/sirupsen/logrus"
|
2019-06-30 05:38:39 +02:00
|
|
|
tensorflow "github.com/tensorflow/tensorflow/tensorflow/go"
|
2019-05-03 18:57:28 +02:00
|
|
|
"github.com/urfave/cli"
|
|
|
|
)
|
|
|
|
|
2019-05-06 23:18:10 +02:00
|
|
|
type Config struct {
|
2019-05-03 18:57:28 +02:00
|
|
|
db *gorm.DB
|
2019-05-06 23:18:10 +02:00
|
|
|
config *Params
|
2019-05-03 18:57:28 +02:00
|
|
|
}
|
|
|
|
|
2019-05-04 09:11:33 +02:00
|
|
|
func initLogger(debug bool) {
|
2019-05-03 18:57:28 +02:00
|
|
|
log.SetFormatter(&log.TextFormatter{
|
|
|
|
DisableColors: false,
|
|
|
|
FullTimestamp: true,
|
|
|
|
})
|
|
|
|
|
2019-05-04 05:25:00 +02:00
|
|
|
if debug {
|
|
|
|
log.SetLevel(log.DebugLevel)
|
|
|
|
} else {
|
|
|
|
log.SetLevel(log.InfoLevel)
|
2019-05-03 18:57:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-09 05:22:53 +02:00
|
|
|
func findExecutable(configBin, defaultBin string) (result string) {
|
2019-06-06 14:44:29 +02:00
|
|
|
if configBin == "" {
|
|
|
|
result = defaultBin
|
|
|
|
} else {
|
|
|
|
result = configBin
|
|
|
|
}
|
|
|
|
|
|
|
|
if path, err := exec.LookPath(result); err == nil {
|
|
|
|
result = path
|
|
|
|
}
|
|
|
|
|
|
|
|
if !util.Exists(result) {
|
|
|
|
result = ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2019-05-06 23:18:10 +02:00
|
|
|
func NewConfig(ctx *cli.Context) *Config {
|
2019-05-04 05:25:00 +02:00
|
|
|
initLogger(ctx.GlobalBool("debug"))
|
2019-05-03 18:57:28 +02:00
|
|
|
|
2019-05-14 16:04:17 +02:00
|
|
|
c := &Config{
|
|
|
|
config: NewParams(ctx),
|
|
|
|
}
|
2019-05-03 18:57:28 +02:00
|
|
|
|
|
|
|
log.SetLevel(c.LogLevel())
|
|
|
|
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateDirectories creates all the folders that photoprism needs. These are:
|
|
|
|
// OriginalsPath
|
|
|
|
// ThumbnailsPath
|
|
|
|
// ImportPath
|
|
|
|
// ExportPath
|
2019-05-06 23:18:10 +02:00
|
|
|
func (c *Config) CreateDirectories() error {
|
2019-05-03 18:57:28 +02:00
|
|
|
if err := os.MkdirAll(c.OriginalsPath(), os.ModePerm); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.MkdirAll(c.ImportPath(), os.ModePerm); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.MkdirAll(c.ExportPath(), os.ModePerm); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.MkdirAll(c.ThumbnailsPath(), os.ModePerm); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-06-05 18:25:20 +02:00
|
|
|
if err := os.MkdirAll(c.ResourcesPath(), os.ModePerm); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-05-03 18:57:28 +02:00
|
|
|
if err := os.MkdirAll(c.SqlServerPath(), os.ModePerm); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.MkdirAll(c.TensorFlowModelPath(), os.ModePerm); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-05-22 13:55:11 +02:00
|
|
|
if err := os.MkdirAll(c.HttpStaticBuildPath(), os.ModePerm); err != nil {
|
2019-05-03 18:57:28 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-06-30 05:38:39 +02:00
|
|
|
if err := os.MkdirAll(filepath.Dir(c.PIDFilename()), os.ModePerm); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.MkdirAll(filepath.Dir(c.LogFilename()), os.ModePerm); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-05-03 18:57:28 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// connectToDatabase establishes a database connection.
|
|
|
|
// When used with the internal driver, it may create a new database server instance.
|
|
|
|
// It tries to do this 12 times with a 5 second sleep interval in between.
|
2019-06-03 22:58:15 +02:00
|
|
|
func (c *Config) connectToDatabase(ctx context.Context) error {
|
2019-05-03 18:57:28 +02:00
|
|
|
dbDriver := c.DatabaseDriver()
|
|
|
|
dbDsn := c.DatabaseDsn()
|
|
|
|
|
2019-05-04 05:25:00 +02:00
|
|
|
if dbDriver == "" {
|
|
|
|
return errors.New("can't connect: database driver not specified")
|
|
|
|
}
|
|
|
|
|
|
|
|
if dbDsn == "" {
|
|
|
|
return errors.New("can't connect: database DSN not specified")
|
|
|
|
}
|
|
|
|
|
2019-05-03 18:57:28 +02:00
|
|
|
isTiDB := false
|
|
|
|
initSuccess := false
|
|
|
|
|
|
|
|
if dbDriver == DbTiDB {
|
|
|
|
isTiDB = true
|
|
|
|
dbDriver = DbMySQL
|
|
|
|
}
|
|
|
|
|
|
|
|
db, err := gorm.Open(dbDriver, dbDsn)
|
|
|
|
if err != nil || db == nil {
|
|
|
|
if isTiDB {
|
|
|
|
log.Infof("starting database server at %s:%d\n", c.SqlServerHost(), c.SqlServerPort())
|
|
|
|
|
2019-06-03 22:58:15 +02:00
|
|
|
go tidb.Start(ctx, c.SqlServerPath(), c.SqlServerPort(), c.SqlServerHost(), c.Debug())
|
2019-05-03 18:57:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for i := 1; i <= 12; i++ {
|
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
|
|
|
|
db, err = gorm.Open(dbDriver, dbDsn)
|
|
|
|
|
|
|
|
if db != nil && err == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if isTiDB && !initSuccess {
|
|
|
|
err = tidb.InitDatabase(c.SqlServerPort(), c.SqlServerPassword())
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Debug(err)
|
|
|
|
} else {
|
|
|
|
initSuccess = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil || db == nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
c.db = db
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Name returns the application name.
|
2019-05-06 23:18:10 +02:00
|
|
|
func (c *Config) Name() string {
|
2019-05-03 18:57:28 +02:00
|
|
|
return c.config.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
// Version returns the application version.
|
2019-05-06 23:18:10 +02:00
|
|
|
func (c *Config) Version() string {
|
2019-05-03 18:57:28 +02:00
|
|
|
return c.config.Version
|
|
|
|
}
|
|
|
|
|
2019-06-30 05:38:39 +02:00
|
|
|
// TensorFlowVersion returns the TenorFlow framework version.
|
|
|
|
func (c *Config) TensorFlowVersion() string {
|
|
|
|
return tensorflow.Version()
|
|
|
|
}
|
|
|
|
|
2019-05-03 18:57:28 +02:00
|
|
|
// Copyright returns the application copyright.
|
2019-05-06 23:18:10 +02:00
|
|
|
func (c *Config) Copyright() string {
|
2019-05-03 18:57:28 +02:00
|
|
|
return c.config.Copyright
|
|
|
|
}
|
|
|
|
|
|
|
|
// Debug returns true if Debug mode is on.
|
2019-05-06 23:18:10 +02:00
|
|
|
func (c *Config) Debug() bool {
|
2019-05-03 18:57:28 +02:00
|
|
|
return c.config.Debug
|
|
|
|
}
|
|
|
|
|
2019-05-04 09:11:33 +02:00
|
|
|
// ReadOnly returns true if photo directories are write protected.
|
2019-05-06 23:18:10 +02:00
|
|
|
func (c *Config) ReadOnly() bool {
|
2019-05-04 09:11:33 +02:00
|
|
|
return c.config.ReadOnly
|
|
|
|
}
|
|
|
|
|
2019-05-03 18:57:28 +02:00
|
|
|
// LogLevel returns the logrus log level.
|
2019-05-06 23:18:10 +02:00
|
|
|
func (c *Config) LogLevel() log.Level {
|
2019-05-03 18:57:28 +02:00
|
|
|
if c.Debug() {
|
|
|
|
c.config.LogLevel = "debug"
|
|
|
|
}
|
|
|
|
|
|
|
|
if logLevel, err := log.ParseLevel(c.config.LogLevel); err == nil {
|
|
|
|
return logLevel
|
|
|
|
} else {
|
2019-05-04 13:29:32 +02:00
|
|
|
return log.InfoLevel
|
2019-05-03 18:57:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-05 18:25:20 +02:00
|
|
|
// ConfigFile returns the config file name.
|
2019-05-06 23:18:10 +02:00
|
|
|
func (c *Config) ConfigFile() string {
|
2019-05-03 18:57:28 +02:00
|
|
|
return c.config.ConfigFile
|
|
|
|
}
|
|
|
|
|
2019-06-05 18:25:20 +02:00
|
|
|
// ConfigPath returns the config path.
|
|
|
|
func (c *Config) ConfigPath() string {
|
|
|
|
if c.config.ConfigPath == "" {
|
|
|
|
return c.AssetsPath() + "/config"
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.config.ConfigPath
|
|
|
|
}
|
|
|
|
|
2019-06-30 05:38:39 +02:00
|
|
|
// PIDFilename returns the filename for storing the server process id (pid).
|
|
|
|
func (c *Config) PIDFilename() string {
|
|
|
|
if c.config.PIDFilename == "" {
|
2019-06-20 01:11:03 +02:00
|
|
|
return c.AssetsPath() + "/photoprism.pid"
|
|
|
|
}
|
|
|
|
|
2019-06-30 05:38:39 +02:00
|
|
|
return c.config.PIDFilename
|
2019-06-20 01:11:03 +02:00
|
|
|
}
|
|
|
|
|
2019-06-30 05:38:39 +02:00
|
|
|
// LogFilename returns the filename for storing server logs.
|
|
|
|
func (c *Config) LogFilename() string {
|
|
|
|
if c.config.LogFilename == "" {
|
2019-06-20 01:11:03 +02:00
|
|
|
return c.AssetsPath() + "/photoprism.log"
|
|
|
|
}
|
|
|
|
|
2019-06-30 05:38:39 +02:00
|
|
|
return c.config.LogFilename
|
2019-06-20 01:11:03 +02:00
|
|
|
}
|
|
|
|
|
2019-06-30 05:38:39 +02:00
|
|
|
// DetachServer returns true if server should detach from console (daemon mode).
|
|
|
|
func (c *Config) DetachServer() bool {
|
|
|
|
return c.config.DetachServer
|
2019-06-20 01:11:03 +02:00
|
|
|
}
|
|
|
|
|
2019-05-03 18:57:28 +02:00
|
|
|
// SqlServerHost returns the built-in SQL server host name or IP address (empty for all interfaces).
|
2019-05-06 23:18:10 +02:00
|
|
|
func (c *Config) SqlServerHost() string {
|
2019-06-30 05:38:39 +02:00
|
|
|
if c.config.SqlServerHost == "" {
|
|
|
|
return "127.0.0.1"
|
|
|
|
}
|
|
|
|
|
2019-05-03 18:57:28 +02:00
|
|
|
return c.config.SqlServerHost
|
|
|
|
}
|
|
|
|
|
|
|
|
// SqlServerPort returns the built-in SQL server port.
|
2019-05-06 23:18:10 +02:00
|
|
|
func (c *Config) SqlServerPort() uint {
|
2019-06-30 05:38:39 +02:00
|
|
|
if c.config.SqlServerPort == 0 {
|
|
|
|
return 4000
|
|
|
|
}
|
|
|
|
|
2019-05-03 18:57:28 +02:00
|
|
|
return c.config.SqlServerPort
|
|
|
|
}
|
|
|
|
|
|
|
|
// SqlServerPath returns the database storage path for TiDB.
|
2019-05-06 23:18:10 +02:00
|
|
|
func (c *Config) SqlServerPath() string {
|
2019-06-30 05:38:39 +02:00
|
|
|
if c.config.SqlServerPath == "" {
|
|
|
|
return c.ResourcesPath() + "/database"
|
2019-05-03 18:57:28 +02:00
|
|
|
}
|
|
|
|
|
2019-06-30 05:38:39 +02:00
|
|
|
return c.config.SqlServerPath
|
2019-05-03 18:57:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// SqlServerPassword returns the password for the built-in database server.
|
2019-05-06 23:18:10 +02:00
|
|
|
func (c *Config) SqlServerPassword() string {
|
2019-05-03 18:57:28 +02:00
|
|
|
return c.config.SqlServerPassword
|
|
|
|
}
|
|
|
|
|
|
|
|
// HttpServerHost returns the built-in HTTP server host name or IP address (empty for all interfaces).
|
2019-05-06 23:18:10 +02:00
|
|
|
func (c *Config) HttpServerHost() string {
|
2019-05-03 18:57:28 +02:00
|
|
|
if c.config.HttpServerHost == "" {
|
|
|
|
return "0.0.0.0"
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.config.HttpServerHost
|
|
|
|
}
|
|
|
|
|
|
|
|
// HttpServerPort returns the built-in HTTP server port.
|
2019-05-06 23:18:10 +02:00
|
|
|
func (c *Config) HttpServerPort() int {
|
2019-06-30 05:38:39 +02:00
|
|
|
if c.config.HttpServerPort == 0 {
|
|
|
|
return 2342
|
|
|
|
}
|
|
|
|
|
2019-05-03 18:57:28 +02:00
|
|
|
return c.config.HttpServerPort
|
|
|
|
}
|
|
|
|
|
|
|
|
// HttpServerMode returns the server mode.
|
2019-05-06 23:18:10 +02:00
|
|
|
func (c *Config) HttpServerMode() string {
|
2019-06-30 05:38:39 +02:00
|
|
|
if c.config.HttpServerMode == "" {
|
|
|
|
if c.Debug() {
|
|
|
|
return "debug"
|
|
|
|
}
|
|
|
|
|
|
|
|
return "release"
|
|
|
|
}
|
|
|
|
|
2019-05-03 18:57:28 +02:00
|
|
|
return c.config.HttpServerMode
|
|
|
|
}
|
|
|
|
|
|
|
|
// HttpServerPassword returns the password for the user interface (optional).
|
2019-05-06 23:18:10 +02:00
|
|
|
func (c *Config) HttpServerPassword() string {
|
2019-05-03 18:57:28 +02:00
|
|
|
return c.config.HttpServerPassword
|
|
|
|
}
|
|
|
|
|
|
|
|
// OriginalsPath returns the originals.
|
2019-05-06 23:18:10 +02:00
|
|
|
func (c *Config) OriginalsPath() string {
|
2019-05-03 18:57:28 +02:00
|
|
|
return c.config.OriginalsPath
|
|
|
|
}
|
|
|
|
|
|
|
|
// ImportPath returns the import directory.
|
2019-05-06 23:18:10 +02:00
|
|
|
func (c *Config) ImportPath() string {
|
2019-05-03 18:57:28 +02:00
|
|
|
return c.config.ImportPath
|
|
|
|
}
|
|
|
|
|
|
|
|
// ExportPath returns the export directory.
|
2019-05-06 23:18:10 +02:00
|
|
|
func (c *Config) ExportPath() string {
|
2019-05-03 18:57:28 +02:00
|
|
|
return c.config.ExportPath
|
|
|
|
}
|
|
|
|
|
2019-06-06 14:44:29 +02:00
|
|
|
// SipsBin returns the sips binary file name.
|
|
|
|
func (c *Config) SipsBin() string {
|
|
|
|
return findExecutable(c.config.SipsBin, "sips")
|
|
|
|
}
|
|
|
|
|
|
|
|
// DarktableBin returns the darktable-cli binary file name.
|
|
|
|
func (c *Config) DarktableBin() string {
|
|
|
|
return findExecutable(c.config.DarktableBin, "darktable-cli")
|
|
|
|
}
|
|
|
|
|
|
|
|
// HeifConvertBin returns the heif-convert binary file name.
|
|
|
|
func (c *Config) HeifConvertBin() string {
|
|
|
|
return findExecutable(c.config.HeifConvertBin, "heif-convert")
|
|
|
|
}
|
|
|
|
|
|
|
|
// ExifToolBin returns the exiftool binary file name.
|
|
|
|
func (c *Config) ExifToolBin() string {
|
|
|
|
return findExecutable(c.config.ExifToolBin, "exiftool")
|
2019-05-03 18:57:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// DatabaseDriver returns the database driver name.
|
2019-05-06 23:18:10 +02:00
|
|
|
func (c *Config) DatabaseDriver() string {
|
2019-05-04 05:25:00 +02:00
|
|
|
if c.config.DatabaseDriver == "" {
|
|
|
|
return DbTiDB
|
|
|
|
}
|
|
|
|
|
2019-05-03 18:57:28 +02:00
|
|
|
return c.config.DatabaseDriver
|
|
|
|
}
|
|
|
|
|
|
|
|
// DatabaseDsn returns the database data source name (DSN).
|
2019-05-06 23:18:10 +02:00
|
|
|
func (c *Config) DatabaseDsn() string {
|
2019-05-04 05:25:00 +02:00
|
|
|
if c.config.DatabaseDsn == "" {
|
|
|
|
return "root:photoprism@tcp(localhost:4000)/photoprism?parseTime=true"
|
|
|
|
}
|
|
|
|
|
2019-05-03 18:57:28 +02:00
|
|
|
return c.config.DatabaseDsn
|
|
|
|
}
|
|
|
|
|
|
|
|
// CachePath returns the path to the cache.
|
2019-05-06 23:18:10 +02:00
|
|
|
func (c *Config) CachePath() string {
|
2019-05-03 18:57:28 +02:00
|
|
|
return c.config.CachePath
|
|
|
|
}
|
|
|
|
|
|
|
|
// ThumbnailsPath returns the path to the cached thumbnails.
|
2019-05-06 23:18:10 +02:00
|
|
|
func (c *Config) ThumbnailsPath() string {
|
2019-05-03 18:57:28 +02:00
|
|
|
return c.CachePath() + "/thumbnails"
|
|
|
|
}
|
|
|
|
|
|
|
|
// AssetsPath returns the path to the assets.
|
2019-05-06 23:18:10 +02:00
|
|
|
func (c *Config) AssetsPath() string {
|
2019-05-03 18:57:28 +02:00
|
|
|
return c.config.AssetsPath
|
|
|
|
}
|
|
|
|
|
2019-06-05 18:25:20 +02:00
|
|
|
// ResourcesPath returns the path to the app resources like static files.
|
|
|
|
func (c *Config) ResourcesPath() string {
|
|
|
|
if c.config.ResourcesPath == "" {
|
|
|
|
return c.AssetsPath() + "/resources"
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.config.ResourcesPath
|
2019-05-03 18:57:28 +02:00
|
|
|
}
|
|
|
|
|
2019-06-05 18:25:20 +02:00
|
|
|
// ExamplesPath returns the example files path.
|
|
|
|
func (c *Config) ExamplesPath() string {
|
|
|
|
return c.ResourcesPath() + "/examples"
|
|
|
|
}
|
|
|
|
|
|
|
|
// TensorFlowModelPath returns the tensorflow model path.
|
|
|
|
func (c *Config) TensorFlowModelPath() string {
|
|
|
|
return c.ResourcesPath() + "/nasnet"
|
2019-05-03 18:57:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// HttpTemplatesPath returns the server templates path.
|
2019-05-06 23:18:10 +02:00
|
|
|
func (c *Config) HttpTemplatesPath() string {
|
2019-06-05 18:25:20 +02:00
|
|
|
return c.ResourcesPath() + "/templates"
|
2019-05-03 18:57:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// HttpFaviconsPath returns the favicons path.
|
2019-05-06 23:18:10 +02:00
|
|
|
func (c *Config) HttpFaviconsPath() string {
|
2019-05-22 13:55:11 +02:00
|
|
|
return c.HttpStaticPath() + "/favicons"
|
2019-05-03 18:57:28 +02:00
|
|
|
}
|
|
|
|
|
2019-05-22 13:55:11 +02:00
|
|
|
// HttpStaticPath returns the static server assets path (//server/static/*).
|
|
|
|
func (c *Config) HttpStaticPath() string {
|
2019-06-05 18:25:20 +02:00
|
|
|
return c.ResourcesPath() + "/static"
|
2019-05-03 18:57:28 +02:00
|
|
|
}
|
|
|
|
|
2019-05-22 13:55:11 +02:00
|
|
|
// HttpStaticBuildPath returns the static build path (//server/static/build/*).
|
|
|
|
func (c *Config) HttpStaticBuildPath() string {
|
|
|
|
return c.HttpStaticPath() + "/build"
|
2019-05-03 18:57:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Db returns the db connection.
|
2019-05-06 23:18:10 +02:00
|
|
|
func (c *Config) Db() *gorm.DB {
|
2019-05-03 18:57:28 +02:00
|
|
|
if c.db == nil {
|
2019-07-10 10:33:16 +02:00
|
|
|
log.Fatal("database not initialised.")
|
2019-05-03 18:57:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return c.db
|
|
|
|
}
|
|
|
|
|
2019-05-04 13:29:32 +02:00
|
|
|
// CloseDb closes the db connection (if any).
|
2019-05-06 23:18:10 +02:00
|
|
|
func (c *Config) CloseDb() error {
|
2019-05-04 13:29:32 +02:00
|
|
|
if c.db != nil {
|
|
|
|
if err := c.db.Close(); err == nil {
|
|
|
|
c.db = nil
|
|
|
|
} else {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-05-03 18:57:28 +02:00
|
|
|
// MigrateDb will start a migration process.
|
2019-05-06 23:18:10 +02:00
|
|
|
func (c *Config) MigrateDb() {
|
2019-05-03 18:57:28 +02:00
|
|
|
db := c.Db()
|
|
|
|
|
2019-06-04 18:26:35 +02:00
|
|
|
// db.LogMode(true)
|
|
|
|
|
2019-05-04 11:50:19 +02:00
|
|
|
db.AutoMigrate(
|
|
|
|
&models.File{},
|
2019-05-03 18:57:28 +02:00
|
|
|
&models.Photo{},
|
2019-06-04 18:26:35 +02:00
|
|
|
&models.Label{},
|
2019-06-05 14:05:21 +02:00
|
|
|
&models.Category{},
|
2019-06-04 18:26:35 +02:00
|
|
|
&models.PhotoLabel{},
|
2019-05-03 18:57:28 +02:00
|
|
|
&models.Album{},
|
|
|
|
&models.Location{},
|
|
|
|
&models.Camera{},
|
|
|
|
&models.Lens{},
|
2019-05-04 11:50:19 +02:00
|
|
|
&models.Country{},
|
2019-06-17 06:04:29 +02:00
|
|
|
&models.Share{},
|
2019-06-17 07:19:44 +02:00
|
|
|
&models.Event{},
|
2019-05-04 13:29:32 +02:00
|
|
|
)
|
2019-05-03 18:57:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// ClientConfig returns a loaded and set configuration entity.
|
2019-05-06 23:18:10 +02:00
|
|
|
func (c *Config) ClientConfig() ClientConfig {
|
2019-05-03 18:57:28 +02:00
|
|
|
db := c.Db()
|
|
|
|
|
|
|
|
var cameras []*models.Camera
|
|
|
|
|
|
|
|
type country struct {
|
|
|
|
LocCountry string
|
|
|
|
LocCountryCode string
|
|
|
|
}
|
|
|
|
|
|
|
|
var countries []country
|
|
|
|
|
|
|
|
db.Model(&models.Location{}).Select("DISTINCT loc_country_code, loc_country").Scan(&countries)
|
|
|
|
|
|
|
|
db.Where("deleted_at IS NULL").Limit(1000).Order("camera_model").Find(&cameras)
|
|
|
|
|
2019-05-22 13:55:11 +02:00
|
|
|
jsHash := util.Hash(c.HttpStaticBuildPath() + "/app.js")
|
|
|
|
cssHash := util.Hash(c.HttpStaticBuildPath() + "/app.css")
|
2019-05-03 18:57:28 +02:00
|
|
|
|
|
|
|
result := ClientConfig{
|
2019-05-14 16:04:17 +02:00
|
|
|
"name": c.Name(),
|
|
|
|
"version": c.Version(),
|
|
|
|
"copyright": c.Copyright(),
|
|
|
|
"debug": c.Debug(),
|
|
|
|
"readonly": c.ReadOnly(),
|
|
|
|
"cameras": cameras,
|
|
|
|
"countries": countries,
|
|
|
|
"thumbnails": Thumbnails,
|
|
|
|
"jsHash": jsHash,
|
|
|
|
"cssHash": cssHash,
|
2019-05-03 18:57:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
2019-05-04 17:34:51 +02:00
|
|
|
|
2019-06-03 22:58:15 +02:00
|
|
|
// Init initialises the Database.
|
|
|
|
func (c *Config) Init(ctx context.Context) error {
|
|
|
|
return c.connectToDatabase(ctx)
|
|
|
|
}
|
|
|
|
|
2019-05-06 23:18:10 +02:00
|
|
|
func (c *Config) Shutdown() {
|
2019-05-04 17:34:51 +02:00
|
|
|
if err := c.CloseDb(); err != nil {
|
|
|
|
log.Errorf("could not close database connection: %s", err)
|
|
|
|
} else {
|
|
|
|
log.Info("closed database connection")
|
|
|
|
}
|
|
|
|
}
|