2020-01-02 00:03:07 +01:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
2020-01-21 13:59:41 +01:00
|
|
|
"fmt"
|
2020-01-02 00:03:07 +01:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
2020-05-31 02:09:52 +02:00
|
|
|
"os/user"
|
2020-01-02 00:03:07 +01:00
|
|
|
"path/filepath"
|
|
|
|
|
2020-01-12 14:00:56 +01:00
|
|
|
"github.com/photoprism/photoprism/pkg/fs"
|
2020-05-03 18:00:50 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/txt"
|
2020-01-02 00:03:07 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func findExecutable(configBin, defaultBin string) (result string) {
|
|
|
|
if configBin == "" {
|
|
|
|
result = defaultBin
|
|
|
|
} else {
|
|
|
|
result = configBin
|
|
|
|
}
|
|
|
|
|
|
|
|
if path, err := exec.LookPath(result); err == nil {
|
|
|
|
result = path
|
|
|
|
}
|
|
|
|
|
2020-01-12 14:00:56 +01:00
|
|
|
if !fs.FileExists(result) {
|
2020-01-02 00:03:07 +01:00
|
|
|
result = ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2020-01-21 13:59:41 +01:00
|
|
|
// CreateDirectories creates directories for storing photos, metadata and cache files.
|
2020-01-02 00:03:07 +01:00
|
|
|
func (c *Config) CreateDirectories() error {
|
2020-01-21 13:59:41 +01:00
|
|
|
createError := func(path string, err error) (result error) {
|
|
|
|
if fs.FileExists(path) {
|
2020-05-07 12:33:09 +02:00
|
|
|
result = fmt.Errorf("%s is a file, not a folder: please check your configuration", txt.Quote(path))
|
2020-01-21 13:59:41 +01:00
|
|
|
} else {
|
2020-05-03 18:00:50 +02:00
|
|
|
result = fmt.Errorf("can't create %s: please check configuration and permissions", txt.Quote(path))
|
2020-01-21 13:59:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Debug(err)
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2020-08-18 19:09:46 +02:00
|
|
|
notFoundError := func(name string) error {
|
|
|
|
return fmt.Errorf("%s path not found, run 'photoprism config' to check configuration values", name)
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.AssetsPath() == "" {
|
|
|
|
return notFoundError("assets")
|
|
|
|
} else if err := os.MkdirAll(c.AssetsPath(), os.ModePerm); err != nil {
|
2020-05-31 02:09:52 +02:00
|
|
|
return createError(c.AssetsPath(), err)
|
|
|
|
}
|
|
|
|
|
2020-08-18 19:09:46 +02:00
|
|
|
if c.StoragePath() == "" {
|
|
|
|
return notFoundError("storage")
|
|
|
|
} else if err := os.MkdirAll(c.StoragePath(), os.ModePerm); err != nil {
|
2020-05-31 02:09:52 +02:00
|
|
|
return createError(c.StoragePath(), err)
|
|
|
|
}
|
|
|
|
|
2020-08-18 19:09:46 +02:00
|
|
|
if c.OriginalsPath() == "" {
|
|
|
|
return notFoundError("originals")
|
|
|
|
} else if err := os.MkdirAll(c.OriginalsPath(), os.ModePerm); err != nil {
|
2020-01-21 13:59:41 +01:00
|
|
|
return createError(c.OriginalsPath(), err)
|
2020-01-02 00:03:07 +01:00
|
|
|
}
|
|
|
|
|
2020-08-18 19:09:46 +02:00
|
|
|
if c.ImportPath() == "" {
|
|
|
|
return notFoundError("import")
|
|
|
|
} else if err := os.MkdirAll(c.ImportPath(), os.ModePerm); err != nil {
|
2020-01-21 13:59:41 +01:00
|
|
|
return createError(c.ImportPath(), err)
|
2020-01-02 00:03:07 +01:00
|
|
|
}
|
|
|
|
|
2020-06-07 10:09:35 +02:00
|
|
|
if filepath.IsAbs(c.SidecarPath()) {
|
|
|
|
if err := os.MkdirAll(c.SidecarPath(), os.ModePerm); err != nil {
|
|
|
|
return createError(c.SidecarPath(), err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-18 19:09:46 +02:00
|
|
|
if c.CachePath() == "" {
|
|
|
|
return notFoundError("cache")
|
|
|
|
} else if err := os.MkdirAll(c.CachePath(), os.ModePerm); err != nil {
|
2020-05-31 02:09:52 +02:00
|
|
|
return createError(c.CachePath(), err)
|
2020-04-06 16:34:29 +02:00
|
|
|
}
|
|
|
|
|
2020-08-18 19:09:46 +02:00
|
|
|
if c.ThumbPath() == "" {
|
|
|
|
return notFoundError("thumbs")
|
|
|
|
} else if err := os.MkdirAll(c.ThumbPath(), os.ModePerm); err != nil {
|
2020-05-05 17:04:13 +02:00
|
|
|
return createError(c.ThumbPath(), err)
|
2020-01-02 00:03:07 +01:00
|
|
|
}
|
|
|
|
|
2020-08-18 19:09:46 +02:00
|
|
|
if c.SettingsPath() == "" {
|
|
|
|
return notFoundError("settings")
|
|
|
|
} else if err := os.MkdirAll(c.SettingsPath(), os.ModePerm); err != nil {
|
2020-05-31 02:09:52 +02:00
|
|
|
return createError(c.SettingsPath(), err)
|
|
|
|
}
|
|
|
|
|
2020-08-18 19:09:46 +02:00
|
|
|
if c.TempPath() == "" {
|
|
|
|
return notFoundError("temp")
|
|
|
|
} else if err := os.MkdirAll(c.TempPath(), os.ModePerm); err != nil {
|
2020-05-31 02:09:52 +02:00
|
|
|
return createError(c.TempPath(), err)
|
2020-01-02 00:03:07 +01:00
|
|
|
}
|
|
|
|
|
2020-08-18 19:09:46 +02:00
|
|
|
if c.TensorFlowModelPath() == "" {
|
|
|
|
return notFoundError("tensorflow model")
|
|
|
|
} else if err := os.MkdirAll(c.TensorFlowModelPath(), os.ModePerm); err != nil {
|
2020-01-21 13:59:41 +01:00
|
|
|
return createError(c.TensorFlowModelPath(), err)
|
2020-01-02 00:03:07 +01:00
|
|
|
}
|
|
|
|
|
2020-08-18 19:09:46 +02:00
|
|
|
if c.BuildPath() == "" {
|
|
|
|
return notFoundError("build")
|
|
|
|
} else if err := os.MkdirAll(c.BuildPath(), os.ModePerm); err != nil {
|
2020-06-26 16:11:56 +02:00
|
|
|
return createError(c.BuildPath(), err)
|
2020-01-02 00:03:07 +01:00
|
|
|
}
|
|
|
|
|
2020-08-18 19:09:46 +02:00
|
|
|
if filepath.Dir(c.PIDFilename()) == "" {
|
|
|
|
return notFoundError("pid file")
|
|
|
|
} else if err := os.MkdirAll(filepath.Dir(c.PIDFilename()), os.ModePerm); err != nil {
|
2020-01-21 13:59:41 +01:00
|
|
|
return createError(filepath.Dir(c.PIDFilename()), err)
|
2020-01-02 00:03:07 +01:00
|
|
|
}
|
|
|
|
|
2020-08-18 19:09:46 +02:00
|
|
|
if filepath.Dir(c.LogFilename()) == "" {
|
|
|
|
return notFoundError("log file")
|
|
|
|
} else if err := os.MkdirAll(filepath.Dir(c.LogFilename()), os.ModePerm); err != nil {
|
2020-01-21 13:59:41 +01:00
|
|
|
return createError(filepath.Dir(c.LogFilename()), err)
|
2020-01-02 00:03:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ConfigFile returns the config file name.
|
|
|
|
func (c *Config) ConfigFile() string {
|
2020-05-31 02:09:52 +02:00
|
|
|
if c.params.ConfigFile == "" || !fs.FileExists(c.params.ConfigFile) {
|
|
|
|
return filepath.Join(c.SettingsPath(), "photoprism.yml")
|
|
|
|
}
|
|
|
|
|
2020-04-13 18:08:21 +02:00
|
|
|
return c.params.ConfigFile
|
2020-01-02 00:03:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// SettingsFile returns the user settings file name.
|
|
|
|
func (c *Config) SettingsFile() string {
|
2020-05-31 02:09:52 +02:00
|
|
|
return filepath.Join(c.SettingsPath(), "settings.yml")
|
2020-01-02 00:03:07 +01:00
|
|
|
}
|
|
|
|
|
2020-05-31 02:09:52 +02:00
|
|
|
// SettingsPath returns the config path.
|
|
|
|
func (c *Config) SettingsPath() string {
|
|
|
|
if c.params.SettingsPath == "" {
|
|
|
|
return filepath.Join(c.StoragePath(), "settings")
|
2020-01-02 00:03:07 +01:00
|
|
|
}
|
|
|
|
|
2020-05-31 02:09:52 +02:00
|
|
|
return fs.Abs(c.params.SettingsPath)
|
2020-01-02 00:03:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// PIDFilename returns the filename for storing the server process id (pid).
|
|
|
|
func (c *Config) PIDFilename() string {
|
2020-04-13 18:08:21 +02:00
|
|
|
if c.params.PIDFilename == "" {
|
2020-05-31 02:09:52 +02:00
|
|
|
return filepath.Join(c.StoragePath(), "photoprism.pid")
|
2020-01-02 00:03:07 +01:00
|
|
|
}
|
|
|
|
|
2020-04-13 18:08:21 +02:00
|
|
|
return fs.Abs(c.params.PIDFilename)
|
2020-01-02 00:03:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// LogFilename returns the filename for storing server logs.
|
|
|
|
func (c *Config) LogFilename() string {
|
2020-04-13 18:08:21 +02:00
|
|
|
if c.params.LogFilename == "" {
|
2020-05-31 02:09:52 +02:00
|
|
|
return filepath.Join(c.StoragePath(), "photoprism.log")
|
2020-01-02 00:03:07 +01:00
|
|
|
}
|
|
|
|
|
2020-04-13 18:08:21 +02:00
|
|
|
return fs.Abs(c.params.LogFilename)
|
2020-01-02 00:03:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// OriginalsPath returns the originals.
|
|
|
|
func (c *Config) OriginalsPath() string {
|
2020-05-31 12:20:32 +02:00
|
|
|
if c.params.OriginalsPath == "" {
|
|
|
|
// Try to find the right directory by iterating through a list.
|
|
|
|
c.params.OriginalsPath = fs.FindDir(fs.OriginalPaths)
|
|
|
|
}
|
|
|
|
|
2020-04-13 18:08:21 +02:00
|
|
|
return fs.Abs(c.params.OriginalsPath)
|
2020-01-02 00:03:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ImportPath returns the import directory.
|
|
|
|
func (c *Config) ImportPath() string {
|
2020-05-31 12:20:32 +02:00
|
|
|
if c.params.ImportPath == "" {
|
|
|
|
// Try to find the right directory by iterating through a list.
|
|
|
|
c.params.ImportPath = fs.FindDir(fs.ImportPaths)
|
|
|
|
}
|
|
|
|
|
2020-04-13 18:08:21 +02:00
|
|
|
return fs.Abs(c.params.ImportPath)
|
2020-01-02 00:03:07 +01:00
|
|
|
}
|
|
|
|
|
2020-05-11 18:29:17 +02:00
|
|
|
// ExifToolBin returns the exiftool executable file name.
|
|
|
|
func (c *Config) ExifToolBin() string {
|
|
|
|
return findExecutable(c.params.ExifToolBin, "exiftool")
|
|
|
|
}
|
|
|
|
|
2020-05-13 20:53:15 +02:00
|
|
|
// SidecarJson returns true if metadata should be synced with json sidecar files as used by exiftool.
|
|
|
|
func (c *Config) SidecarJson() bool {
|
2020-06-07 14:33:07 +02:00
|
|
|
if !c.SidecarWritable() || c.ExifToolBin() == "" {
|
2020-05-11 18:29:17 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-05-13 20:53:15 +02:00
|
|
|
return c.params.SidecarJson
|
2020-05-11 18:29:17 +02:00
|
|
|
}
|
|
|
|
|
2020-05-18 22:18:58 +02:00
|
|
|
// SidecarYaml returns true if metadata should be synced with PhotoPrism YAML sidecar files.
|
|
|
|
func (c *Config) SidecarYaml() bool {
|
2020-06-07 14:33:07 +02:00
|
|
|
if !c.SidecarWritable() {
|
2020-05-18 22:18:58 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.params.SidecarYaml
|
|
|
|
}
|
|
|
|
|
2020-06-07 10:09:35 +02:00
|
|
|
// SidecarPath returns the storage path for automatically created sidecar files.
|
|
|
|
func (c *Config) SidecarPath() string {
|
|
|
|
if c.params.SidecarPath == "" {
|
|
|
|
c.params.SidecarPath = filepath.Join(c.StoragePath(), "sidecar")
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.params.SidecarPath
|
2020-05-19 11:00:17 +02:00
|
|
|
}
|
|
|
|
|
2020-06-07 14:33:07 +02:00
|
|
|
// SidecarPathIsAbs returns true if sidecar path is absolute.
|
|
|
|
func (c *Config) SidecarPathIsAbs() bool {
|
|
|
|
return filepath.IsAbs(c.SidecarPath())
|
|
|
|
}
|
|
|
|
|
|
|
|
// SidecarWritable returns true if sidecar files can be created.
|
|
|
|
func (c *Config) SidecarWritable() bool {
|
|
|
|
return !c.ReadOnly() || c.SidecarPathIsAbs()
|
|
|
|
}
|
|
|
|
|
2020-05-11 18:29:17 +02:00
|
|
|
// FFmpegBin returns the ffmpeg executable file name.
|
|
|
|
func (c *Config) FFmpegBin() string {
|
|
|
|
return findExecutable(c.params.FFmpegBin, "ffmpeg")
|
2020-01-02 00:03:07 +01:00
|
|
|
}
|
|
|
|
|
2020-04-06 16:34:29 +02:00
|
|
|
// TempPath returns a temporary directory name for uploads and downloads.
|
|
|
|
func (c *Config) TempPath() string {
|
2020-04-13 18:08:21 +02:00
|
|
|
if c.params.TempPath == "" {
|
2020-05-05 18:26:44 +02:00
|
|
|
return filepath.Join(os.TempDir(), "photoprism")
|
2020-04-06 16:34:29 +02:00
|
|
|
}
|
|
|
|
|
2020-04-13 18:08:21 +02:00
|
|
|
return fs.Abs(c.params.TempPath)
|
2020-04-06 16:34:29 +02:00
|
|
|
}
|
|
|
|
|
2020-01-02 00:03:07 +01:00
|
|
|
// CachePath returns the path to the cache.
|
|
|
|
func (c *Config) CachePath() string {
|
2020-05-31 02:09:52 +02:00
|
|
|
if c.params.CachePath == "" {
|
|
|
|
return filepath.Join(c.StoragePath(), "cache")
|
|
|
|
}
|
|
|
|
|
2020-04-13 18:08:21 +02:00
|
|
|
return fs.Abs(c.params.CachePath)
|
2020-01-02 00:03:07 +01:00
|
|
|
}
|
|
|
|
|
2020-05-31 11:28:28 +02:00
|
|
|
// StoragePath returns the path for generated files like cache and index.
|
2020-05-31 02:09:52 +02:00
|
|
|
func (c *Config) StoragePath() string {
|
|
|
|
if c.params.StoragePath == "" {
|
2020-05-31 11:28:28 +02:00
|
|
|
const dirName = "storage"
|
|
|
|
|
|
|
|
// Default directories.
|
|
|
|
originalsDir := fs.Abs(filepath.Join(c.OriginalsPath(), fs.HiddenPath, dirName))
|
|
|
|
storageDir := fs.Abs(dirName)
|
|
|
|
|
|
|
|
// Find existing directories.
|
|
|
|
if fs.PathExists(originalsDir) && !c.ReadOnly() {
|
|
|
|
return originalsDir
|
|
|
|
} else if fs.PathExists(storageDir) && c.ReadOnly() {
|
|
|
|
return storageDir
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use .photoprism in home directory?
|
2020-05-31 02:09:52 +02:00
|
|
|
if usr, _ := user.Current(); usr.HomeDir != "" {
|
2020-05-31 11:28:28 +02:00
|
|
|
p := fs.Abs(filepath.Join(usr.HomeDir, fs.HiddenPath, dirName))
|
2020-01-02 00:03:07 +01:00
|
|
|
|
2020-05-31 11:28:28 +02:00
|
|
|
if fs.PathExists(p) || c.ReadOnly() {
|
2020-05-31 02:09:52 +02:00
|
|
|
return p
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-31 11:28:28 +02:00
|
|
|
// Fallback directory in case nothing else works.
|
|
|
|
if c.ReadOnly() {
|
|
|
|
return fs.Abs(filepath.Join(fs.HiddenPath, dirName))
|
2020-05-31 02:09:52 +02:00
|
|
|
}
|
2020-05-31 11:28:28 +02:00
|
|
|
|
|
|
|
// Store cache and index in "originals/.photoprism/storage".
|
|
|
|
return originalsDir
|
2020-01-02 00:03:07 +01:00
|
|
|
}
|
|
|
|
|
2020-05-31 02:09:52 +02:00
|
|
|
return fs.Abs(c.params.StoragePath)
|
2020-01-02 00:03:07 +01:00
|
|
|
}
|
|
|
|
|
2020-05-31 02:09:52 +02:00
|
|
|
// AssetsPath returns the path to static assets.
|
|
|
|
func (c *Config) AssetsPath() string {
|
2020-08-18 19:09:46 +02:00
|
|
|
if c.params.AssetsPath == "" {
|
|
|
|
// Try to find the right directory by iterating through a list.
|
|
|
|
c.params.AssetsPath = fs.FindDir(fs.AssetPaths)
|
|
|
|
}
|
|
|
|
|
2020-05-31 02:09:52 +02:00
|
|
|
return fs.Abs(c.params.AssetsPath)
|
2020-01-02 00:03:07 +01:00
|
|
|
}
|
|
|
|
|
2020-07-15 01:26:54 +02:00
|
|
|
// LocalesPath returns the translation locales path.
|
|
|
|
func (c *Config) LocalesPath() string {
|
|
|
|
return filepath.Join(c.AssetsPath(), "locales")
|
|
|
|
}
|
|
|
|
|
2020-05-31 02:09:52 +02:00
|
|
|
// ExamplesPath returns the example files path.
|
|
|
|
func (c *Config) ExamplesPath() string {
|
|
|
|
return filepath.Join(c.AssetsPath(), "examples")
|
2020-01-02 00:03:07 +01:00
|
|
|
}
|
|
|
|
|
2020-05-31 02:09:52 +02:00
|
|
|
// TestdataPath returns the test files path.
|
|
|
|
func (c *Config) TestdataPath() string {
|
|
|
|
return filepath.Join(c.StoragePath(), "testdata")
|
2020-01-02 00:03:07 +01:00
|
|
|
}
|