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"
|
2019-12-02 00:30:58 +01:00
|
|
|
gc "github.com/patrickmn/go-cache"
|
2019-12-11 16:55:18 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/entity"
|
2019-12-02 00:30:58 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/event"
|
2019-05-03 18:57:28 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/tidb"
|
2019-05-06 23:18:10 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/util"
|
2019-12-02 00:30:58 +01:00
|
|
|
"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-12-02 00:30:58 +01:00
|
|
|
var log = event.Log
|
|
|
|
|
2019-05-06 23:18:10 +02:00
|
|
|
type Config struct {
|
2019-05-03 18:57:28 +02:00
|
|
|
db *gorm.DB
|
2019-12-02 00:30:58 +01:00
|
|
|
cache *gc.Cache
|
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-12-02 00:30:58 +01:00
|
|
|
log.SetFormatter(&logrus.TextFormatter{
|
2019-05-03 18:57:28 +02:00
|
|
|
DisableColors: false,
|
|
|
|
FullTimestamp: true,
|
|
|
|
})
|
|
|
|
|
2019-05-04 05:25:00 +02:00
|
|
|
if debug {
|
2019-12-02 00:30:58 +01:00
|
|
|
log.SetLevel(logrus.DebugLevel)
|
2019-05-04 05:25:00 +02:00
|
|
|
} else {
|
2019-12-02 00:30:58 +01:00
|
|
|
log.SetLevel(logrus.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
|
|
|
|
}
|
|
|
|
|
2019-12-11 14:10:20 +01:00
|
|
|
// Url returns the public server URL (default is "http://localhost:2342/").
|
|
|
|
func (c *Config) Url() string {
|
|
|
|
if c.config.Url == "" {
|
|
|
|
return "http://localhost:2342/"
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.config.Url
|
|
|
|
}
|
|
|
|
|
|
|
|
// Title returns the site title (default is application name).
|
|
|
|
func (c *Config) Title() string {
|
|
|
|
if c.config.Title == "" {
|
|
|
|
return c.Name()
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.config.Title
|
|
|
|
}
|
|
|
|
|
|
|
|
// Subtitle returns the site title.
|
|
|
|
func (c *Config) Subtitle() string {
|
|
|
|
return c.config.Subtitle
|
|
|
|
}
|
|
|
|
|
|
|
|
// Description returns the site title.
|
|
|
|
func (c *Config) Description() string {
|
|
|
|
return c.config.Description
|
|
|
|
}
|
|
|
|
|
|
|
|
// Author returns the site author / copyright.
|
|
|
|
func (c *Config) Author() string {
|
|
|
|
return c.config.Author
|
|
|
|
}
|
|
|
|
|
|
|
|
// Description returns the twitter handle for sharing.
|
|
|
|
func (c *Config) Twitter() string {
|
|
|
|
return c.config.Twitter
|
|
|
|
}
|
|
|
|
|
2019-05-03 18:57:28 +02:00
|
|
|
// 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-11-11 21:10:41 +01:00
|
|
|
// Public returns true if app requires no authentication.
|
|
|
|
func (c *Config) Public() bool {
|
|
|
|
return c.config.Public
|
|
|
|
}
|
|
|
|
|
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-12-15 17:19:16 +01:00
|
|
|
// HideNSFW returns true if NSFW photos are hidden by default.
|
|
|
|
func (c *Config) HideNSFW() bool {
|
|
|
|
return c.config.HideNSFW
|
|
|
|
}
|
|
|
|
|
|
|
|
// UploadNSFW returns true if NSFW photos can be uploaded.
|
|
|
|
func (c *Config) UploadNSFW() bool {
|
|
|
|
return c.config.UploadNSFW
|
|
|
|
}
|
|
|
|
|
2019-11-08 06:53:40 +01:00
|
|
|
// AdminPassword returns the admin password.
|
|
|
|
func (c *Config) AdminPassword() string {
|
|
|
|
if c.config.AdminPassword == "" {
|
|
|
|
return "photoprism"
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.config.AdminPassword
|
|
|
|
}
|
|
|
|
|
2019-05-03 18:57:28 +02:00
|
|
|
// LogLevel returns the logrus log level.
|
2019-12-02 00:30:58 +01:00
|
|
|
func (c *Config) LogLevel() logrus.Level {
|
2019-05-03 18:57:28 +02:00
|
|
|
if c.Debug() {
|
|
|
|
c.config.LogLevel = "debug"
|
|
|
|
}
|
|
|
|
|
2019-12-02 00:30:58 +01:00
|
|
|
if logLevel, err := logrus.ParseLevel(c.config.LogLevel); err == nil {
|
2019-05-03 18:57:28 +02:00
|
|
|
return logLevel
|
|
|
|
} else {
|
2019-12-02 00:30:58 +01:00
|
|
|
return logrus.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-11-17 03:08:13 +01:00
|
|
|
// SettingsFile returns the user settings file name.
|
|
|
|
func (c *Config) SettingsFile() string {
|
|
|
|
return c.ConfigPath() + "/settings.yml"
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-12-14 20:35:14 +01:00
|
|
|
// NSFWModelPath returns the NSFW tensorflow model path.
|
|
|
|
func (c *Config) NSFWModelPath() string {
|
|
|
|
return c.ResourcesPath() + "/nsfw"
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-11-08 06:53:40 +01:00
|
|
|
// Cache returns the in-memory cache.
|
|
|
|
func (c *Config) Cache() *gc.Cache {
|
|
|
|
if c.cache == nil {
|
|
|
|
c.cache = gc.New(336*time.Hour, 30*time.Minute)
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.cache
|
|
|
|
}
|
|
|
|
|
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(
|
2019-12-11 16:55:18 +01:00
|
|
|
&entity.File{},
|
|
|
|
&entity.Photo{},
|
|
|
|
&entity.Event{},
|
|
|
|
&entity.Location{},
|
|
|
|
&entity.Camera{},
|
|
|
|
&entity.Lens{},
|
|
|
|
&entity.Country{},
|
|
|
|
&entity.Share{},
|
|
|
|
|
|
|
|
&entity.Album{},
|
|
|
|
&entity.PhotoAlbum{},
|
|
|
|
&entity.Label{},
|
|
|
|
&entity.Category{},
|
|
|
|
&entity.PhotoLabel{},
|
|
|
|
&entity.Keyword{},
|
|
|
|
&entity.PhotoKeyword{},
|
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()
|
|
|
|
|
2019-12-11 16:55:18 +01:00
|
|
|
var cameras []*entity.Camera
|
|
|
|
var albums []*entity.Album
|
2019-05-03 18:57:28 +02:00
|
|
|
|
|
|
|
type country struct {
|
|
|
|
LocCountry string
|
|
|
|
LocCountryCode string
|
|
|
|
}
|
|
|
|
|
2019-12-17 15:28:54 +01:00
|
|
|
var position struct {
|
|
|
|
PhotoUUID string `json:"uuid"`
|
|
|
|
PhotoLat float64 `json:"lat"`
|
|
|
|
PhotoLong float64 `json:"long"`
|
|
|
|
TakenAt time.Time `json:"utc"`
|
|
|
|
TakenAtLocal time.Time `json:"time"`
|
|
|
|
}
|
|
|
|
|
|
|
|
db.Table("photos").
|
|
|
|
Select("photo_uuid, photo_lat, photo_long, taken_at, taken_at_local").
|
|
|
|
Where("deleted_at IS NULL AND photo_lat != 0 AND photo_long != 0").
|
|
|
|
Order("taken_at DESC").
|
|
|
|
Limit(1).Offset(0).
|
|
|
|
Take(&position)
|
|
|
|
|
2019-05-03 18:57:28 +02:00
|
|
|
var countries []country
|
2019-12-11 04:12:54 +01:00
|
|
|
var count = struct {
|
|
|
|
Photos uint `json:"photos"`
|
|
|
|
Favorites uint `json:"favorites"`
|
|
|
|
Private uint `json:"private"`
|
|
|
|
Stories uint `json:"stories"`
|
|
|
|
Labels uint `json:"labels"`
|
|
|
|
Albums uint `json:"albums"`
|
2019-12-11 14:10:20 +01:00
|
|
|
Countries uint `json:"countries"`
|
2019-12-11 04:12:54 +01:00
|
|
|
}{}
|
|
|
|
|
|
|
|
db.Table("photos").
|
|
|
|
Select("COUNT(*) AS photos, SUM(photo_favorite) AS favorites, SUM(photo_private) AS private, SUM(photo_story) AS stories").
|
|
|
|
Where("deleted_at IS NULL").
|
|
|
|
Take(&count)
|
|
|
|
|
|
|
|
db.Table("labels").
|
|
|
|
Select("COUNT(*) AS labels").
|
2019-12-16 23:33:52 +01:00
|
|
|
Where("(label_priority >= 0 || label_favorite = 1) && deleted_at IS NULL").
|
2019-12-11 04:12:54 +01:00
|
|
|
Take(&count)
|
|
|
|
|
|
|
|
db.Table("albums").
|
|
|
|
Select("COUNT(*) AS albums").
|
|
|
|
Where("deleted_at IS NULL").
|
|
|
|
Take(&count)
|
|
|
|
|
|
|
|
db.Table("countries").
|
|
|
|
Select("COUNT(*) AS countries").
|
|
|
|
Take(&count)
|
|
|
|
|
2019-12-11 16:55:18 +01:00
|
|
|
db.Model(&entity.Location{}).
|
2019-12-11 04:12:54 +01:00
|
|
|
Select("DISTINCT loc_country_code, loc_country").
|
|
|
|
Scan(&countries)
|
|
|
|
|
|
|
|
db.Where("deleted_at IS NULL").
|
|
|
|
Limit(1000).Order("camera_model").
|
|
|
|
Find(&cameras)
|
|
|
|
|
|
|
|
db.Where("deleted_at IS NULL AND album_favorite = 1").
|
|
|
|
Limit(20).Order("album_name").
|
|
|
|
Find(&albums)
|
2019-05-03 18:57:28 +02:00
|
|
|
|
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-12-11 14:10:20 +01:00
|
|
|
"name": c.Name(),
|
|
|
|
"url": c.Url(),
|
|
|
|
"title": c.Title(),
|
|
|
|
"subtitle": c.Subtitle(),
|
|
|
|
"description": c.Description(),
|
|
|
|
"author": c.Author(),
|
|
|
|
"twitter": c.Twitter(),
|
|
|
|
"version": c.Version(),
|
|
|
|
"copyright": c.Copyright(),
|
|
|
|
"debug": c.Debug(),
|
|
|
|
"readonly": c.ReadOnly(),
|
2019-12-15 17:19:16 +01:00
|
|
|
"uploadNSFW": c.UploadNSFW(),
|
2019-12-11 14:10:20 +01:00
|
|
|
"public": c.Public(),
|
|
|
|
"albums": albums,
|
|
|
|
"cameras": cameras,
|
|
|
|
"countries": countries,
|
|
|
|
"thumbnails": Thumbnails,
|
|
|
|
"jsHash": jsHash,
|
|
|
|
"cssHash": cssHash,
|
|
|
|
"settings": c.Settings(),
|
|
|
|
"count": count,
|
2019-12-17 15:28:54 +01:00
|
|
|
"pos": position,
|
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-11-12 04:34:37 +01:00
|
|
|
// Shutdown closes open database connections.
|
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")
|
|
|
|
}
|
|
|
|
}
|
2019-11-12 04:34:37 +01:00
|
|
|
|
|
|
|
// Settings returns the current user settings.
|
|
|
|
func (c *Config) Settings() *Settings {
|
2019-11-17 03:08:13 +01:00
|
|
|
s := NewSettings()
|
|
|
|
p := c.SettingsFile()
|
|
|
|
|
|
|
|
if err := s.SetValuesFromFile(p); err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return s
|
2019-11-12 04:34:37 +01:00
|
|
|
}
|