2020-01-02 00:03:07 +01:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
2020-01-21 13:59:41 +01:00
|
|
|
"fmt"
|
2022-07-19 20:41:36 +02:00
|
|
|
"io/ioutil"
|
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"
|
2022-07-21 20:23:00 +02:00
|
|
|
"runtime"
|
2022-10-12 18:39:30 +02:00
|
|
|
"sync"
|
2020-01-02 00:03:07 +01:00
|
|
|
|
2022-04-15 09:42:07 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/clean"
|
2020-01-12 14:00:56 +01:00
|
|
|
"github.com/photoprism/photoprism/pkg/fs"
|
2023-01-30 14:56:31 +01:00
|
|
|
"github.com/photoprism/photoprism/pkg/rnd"
|
2020-01-02 00:03:07 +01:00
|
|
|
)
|
|
|
|
|
2022-04-06 17:46:41 +02:00
|
|
|
// binPaths stores known executable paths.
|
2022-10-12 18:39:30 +02:00
|
|
|
var (
|
|
|
|
binPaths = make(map[string]string, 8)
|
|
|
|
binMu = sync.RWMutex{}
|
|
|
|
tempPath = ""
|
|
|
|
)
|
2022-04-06 17:46:41 +02:00
|
|
|
|
2022-10-12 18:39:30 +02:00
|
|
|
// findBin resolves the absolute file path of external binaries.
|
|
|
|
func findBin(configBin, defaultBin string) (binPath string) {
|
2022-04-06 17:46:41 +02:00
|
|
|
cacheKey := defaultBin + configBin
|
2022-10-12 18:39:30 +02:00
|
|
|
binMu.RLock()
|
|
|
|
cached, found := binPaths[cacheKey]
|
|
|
|
binMu.RUnlock()
|
|
|
|
|
|
|
|
// Already found?
|
|
|
|
if found {
|
2022-04-06 17:46:41 +02:00
|
|
|
return cached
|
|
|
|
}
|
|
|
|
|
2022-10-12 18:39:30 +02:00
|
|
|
// Default binary name?
|
2020-01-02 00:03:07 +01:00
|
|
|
if configBin == "" {
|
2022-04-06 17:46:41 +02:00
|
|
|
binPath = defaultBin
|
2020-01-02 00:03:07 +01:00
|
|
|
} else {
|
2022-04-06 17:46:41 +02:00
|
|
|
binPath = configBin
|
2020-01-02 00:03:07 +01:00
|
|
|
}
|
|
|
|
|
2022-10-12 18:39:30 +02:00
|
|
|
// Search for binary.
|
2022-04-06 17:46:41 +02:00
|
|
|
if path, err := exec.LookPath(binPath); err == nil {
|
|
|
|
binPath = path
|
2020-01-02 00:03:07 +01:00
|
|
|
}
|
|
|
|
|
2022-10-12 18:39:30 +02:00
|
|
|
// Found?
|
2022-04-06 17:46:41 +02:00
|
|
|
if !fs.FileExists(binPath) {
|
|
|
|
binPath = ""
|
|
|
|
} else {
|
2022-10-12 18:39:30 +02:00
|
|
|
binMu.Lock()
|
2022-04-06 17:46:41 +02:00
|
|
|
binPaths[cacheKey] = binPath
|
2022-10-12 18:39:30 +02:00
|
|
|
binMu.Unlock()
|
2020-01-02 00:03:07 +01:00
|
|
|
}
|
|
|
|
|
2022-04-06 17:46:41 +02:00
|
|
|
return binPath
|
2020-01-02 00:03:07 +01:00
|
|
|
}
|
|
|
|
|
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-08-18 19:09:46 +02:00
|
|
|
if c.AssetsPath() == "" {
|
|
|
|
return notFoundError("assets")
|
2022-10-31 15:01:48 +01:00
|
|
|
} else if err := os.MkdirAll(c.AssetsPath(), fs.ModeDir); 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")
|
2022-10-31 15:01:48 +01:00
|
|
|
} else if err := os.MkdirAll(c.StoragePath(), fs.ModeDir); err != nil {
|
2020-05-31 02:09:52 +02:00
|
|
|
return createError(c.StoragePath(), err)
|
|
|
|
}
|
|
|
|
|
2022-10-17 19:07:38 +02:00
|
|
|
if c.UsersPath() == "" {
|
|
|
|
return notFoundError("users")
|
2023-03-14 21:47:14 +01:00
|
|
|
} else if err := os.MkdirAll(c.UsersStoragePath(), fs.ModeDir); err != nil {
|
|
|
|
return createError(c.UsersStoragePath(), err)
|
2022-10-17 19:07:38 +02:00
|
|
|
}
|
|
|
|
|
2022-04-18 13:55:17 +02:00
|
|
|
if c.CmdCachePath() == "" {
|
|
|
|
return notFoundError("cmd cache")
|
2022-10-31 15:01:48 +01:00
|
|
|
} else if err := os.MkdirAll(c.CmdCachePath(), fs.ModeDir); err != nil {
|
2022-04-18 13:55:17 +02:00
|
|
|
return createError(c.CmdCachePath(), err)
|
|
|
|
}
|
|
|
|
|
2020-12-04 11:02:19 +01:00
|
|
|
if c.BackupPath() == "" {
|
|
|
|
return notFoundError("backup")
|
2022-10-31 15:01:48 +01:00
|
|
|
} else if err := os.MkdirAll(c.BackupPath(), fs.ModeDir); err != nil {
|
2020-12-04 11:02:19 +01:00
|
|
|
return createError(c.BackupPath(), err)
|
|
|
|
}
|
|
|
|
|
2020-08-18 19:09:46 +02:00
|
|
|
if c.OriginalsPath() == "" {
|
|
|
|
return notFoundError("originals")
|
2022-10-31 15:01:48 +01:00
|
|
|
} else if err := os.MkdirAll(c.OriginalsPath(), fs.ModeDir); 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")
|
2022-10-31 15:01:48 +01:00
|
|
|
} else if err := os.MkdirAll(c.ImportPath(), fs.ModeDir); 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()) {
|
2022-10-31 15:01:48 +01:00
|
|
|
if err := os.MkdirAll(c.SidecarPath(), fs.ModeDir); err != nil {
|
2020-06-07 10:09:35 +02:00
|
|
|
return createError(c.SidecarPath(), err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-18 19:09:46 +02:00
|
|
|
if c.CachePath() == "" {
|
|
|
|
return notFoundError("cache")
|
2022-10-31 15:01:48 +01:00
|
|
|
} else if err := os.MkdirAll(c.CachePath(), fs.ModeDir); err != nil {
|
2020-05-31 02:09:52 +02:00
|
|
|
return createError(c.CachePath(), err)
|
2020-04-06 16:34:29 +02:00
|
|
|
}
|
|
|
|
|
2023-02-21 13:31:30 +01:00
|
|
|
if c.MediaCachePath() == "" {
|
|
|
|
return notFoundError("media")
|
|
|
|
} else if err := os.MkdirAll(c.MediaCachePath(), fs.ModeDir); err != nil {
|
|
|
|
return createError(c.MediaCachePath(), err)
|
|
|
|
}
|
|
|
|
|
2022-04-18 13:55:17 +02:00
|
|
|
if c.ThumbCachePath() == "" {
|
2020-08-18 19:09:46 +02:00
|
|
|
return notFoundError("thumbs")
|
2022-10-31 15:01:48 +01:00
|
|
|
} else if err := os.MkdirAll(c.ThumbCachePath(), fs.ModeDir); err != nil {
|
2022-04-18 13:55:17 +02:00
|
|
|
return createError(c.ThumbCachePath(), err)
|
2020-01-02 00:03:07 +01:00
|
|
|
}
|
|
|
|
|
2020-12-17 18:24:55 +01:00
|
|
|
if c.ConfigPath() == "" {
|
|
|
|
return notFoundError("config")
|
2022-10-31 15:01:48 +01:00
|
|
|
} else if err := os.MkdirAll(c.ConfigPath(), fs.ModeDir); err != nil {
|
2020-12-17 18:24:55 +01:00
|
|
|
return createError(c.ConfigPath(), err)
|
2020-05-31 02:09:52 +02:00
|
|
|
}
|
|
|
|
|
2022-10-19 05:09:09 +02:00
|
|
|
if c.CertificatesPath() == "" {
|
|
|
|
return notFoundError("certificates")
|
2022-10-31 15:01:48 +01:00
|
|
|
} else if err := os.MkdirAll(c.CertificatesPath(), fs.ModeDir); err != nil {
|
2022-10-19 05:09:09 +02:00
|
|
|
return createError(c.CertificatesPath(), err)
|
2022-10-11 22:44:11 +02:00
|
|
|
}
|
|
|
|
|
2020-08-18 19:09:46 +02:00
|
|
|
if c.TempPath() == "" {
|
|
|
|
return notFoundError("temp")
|
2022-10-31 15:01:48 +01:00
|
|
|
} else if err := os.MkdirAll(c.TempPath(), fs.ModeDir); err != nil {
|
2020-05-31 02:09:52 +02:00
|
|
|
return createError(c.TempPath(), err)
|
2020-01-02 00:03:07 +01:00
|
|
|
}
|
|
|
|
|
2020-12-17 18:24:55 +01:00
|
|
|
if c.AlbumsPath() == "" {
|
|
|
|
return notFoundError("albums")
|
2022-10-31 15:01:48 +01:00
|
|
|
} else if err := os.MkdirAll(c.AlbumsPath(), fs.ModeDir); err != nil {
|
2020-12-17 18:24:55 +01:00
|
|
|
return createError(c.AlbumsPath(), err)
|
|
|
|
}
|
|
|
|
|
2020-08-18 19:09:46 +02:00
|
|
|
if c.TensorFlowModelPath() == "" {
|
|
|
|
return notFoundError("tensorflow model")
|
2022-10-31 15:01:48 +01:00
|
|
|
} else if err := os.MkdirAll(c.TensorFlowModelPath(), fs.ModeDir); 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")
|
2022-10-31 15:01:48 +01:00
|
|
|
} else if err := os.MkdirAll(c.BuildPath(), fs.ModeDir); 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")
|
2022-10-31 15:01:48 +01:00
|
|
|
} else if err := os.MkdirAll(filepath.Dir(c.PIDFilename()), fs.ModeDir); 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")
|
2022-10-31 15:01:48 +01:00
|
|
|
} else if err := os.MkdirAll(filepath.Dir(c.LogFilename()), fs.ModeDir); 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
|
|
|
}
|
|
|
|
|
2022-04-06 17:46:41 +02:00
|
|
|
if c.DarktableEnabled() {
|
2022-04-18 13:55:17 +02:00
|
|
|
if dir, err := c.CreateDarktableCachePath(); err != nil {
|
|
|
|
return fmt.Errorf("could not create darktable cache path %s", clean.Log(dir))
|
2022-04-06 17:46:41 +02:00
|
|
|
}
|
|
|
|
|
2022-04-18 13:55:17 +02:00
|
|
|
if dir, err := c.CreateDarktableConfigPath(); err != nil {
|
|
|
|
return fmt.Errorf("could not create darktable cache path %s", clean.Log(dir))
|
2022-04-06 17:46:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-02 00:03:07 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-12-17 18:24:55 +01:00
|
|
|
// ConfigPath returns the config path.
|
|
|
|
func (c *Config) ConfigPath() string {
|
2020-12-18 20:42:12 +01:00
|
|
|
if c.options.ConfigPath == "" {
|
2020-12-17 18:24:55 +01:00
|
|
|
if fs.PathExists(filepath.Join(c.StoragePath(), "settings")) {
|
|
|
|
return filepath.Join(c.StoragePath(), "settings")
|
|
|
|
}
|
|
|
|
|
|
|
|
return filepath.Join(c.StoragePath(), "config")
|
|
|
|
}
|
|
|
|
|
2020-12-18 20:42:12 +01:00
|
|
|
return fs.Abs(c.options.ConfigPath)
|
2020-12-17 18:24:55 +01:00
|
|
|
}
|
|
|
|
|
2022-04-13 22:43:49 +02:00
|
|
|
// OptionsYaml returns the config options YAML filename.
|
|
|
|
func (c *Config) OptionsYaml() string {
|
|
|
|
return filepath.Join(c.ConfigPath(), "options.yml")
|
|
|
|
}
|
|
|
|
|
|
|
|
// DefaultsYaml returns the default options YAML filename.
|
|
|
|
func (c *Config) DefaultsYaml() string {
|
2023-01-30 14:56:31 +01:00
|
|
|
return fs.Abs(c.options.DefaultsYaml)
|
2022-04-13 22:43:49 +02:00
|
|
|
}
|
|
|
|
|
2020-12-04 13:10:32 +01:00
|
|
|
// HubConfigFile returns the backend api config file name.
|
|
|
|
func (c *Config) HubConfigFile() string {
|
2020-12-17 18:24:55 +01:00
|
|
|
return filepath.Join(c.ConfigPath(), "hub.yml")
|
2020-09-06 14:18:40 +02:00
|
|
|
}
|
|
|
|
|
2022-04-13 22:43:49 +02:00
|
|
|
// SettingsYaml returns the settings YAML filename.
|
|
|
|
func (c *Config) SettingsYaml() string {
|
2020-12-17 18:24:55 +01:00
|
|
|
return filepath.Join(c.ConfigPath(), "settings.yml")
|
2020-01-02 00:03:07 +01:00
|
|
|
}
|
|
|
|
|
2023-01-30 14:56:31 +01:00
|
|
|
// SettingsYamlDefaults returns the default settings YAML filename.
|
|
|
|
func (c *Config) SettingsYamlDefaults(settingsYml string) string {
|
|
|
|
if settingsYml != "" && fs.FileExists(settingsYml) {
|
|
|
|
// Use regular settings YAML file.
|
|
|
|
} else if defaultsYml := c.DefaultsYaml(); defaultsYml == "" {
|
|
|
|
// Use regular settings YAML file.
|
|
|
|
} else if dir := filepath.Dir(defaultsYml); dir == "" || dir == "." {
|
|
|
|
// Use regular settings YAML file.
|
|
|
|
} else if fileName := filepath.Join(dir, "settings.yml"); settingsYml == "" || fs.FileExistsNotEmpty(fileName) {
|
|
|
|
// Use default settings YAML file.
|
|
|
|
return fileName
|
|
|
|
}
|
|
|
|
|
|
|
|
return settingsYml
|
|
|
|
}
|
|
|
|
|
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-12-18 20:42:12 +01:00
|
|
|
if c.options.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-12-18 20:42:12 +01:00
|
|
|
return fs.Abs(c.options.PIDFilename)
|
2020-01-02 00:03:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// LogFilename returns the filename for storing server logs.
|
|
|
|
func (c *Config) LogFilename() string {
|
2020-12-18 20:42:12 +01:00
|
|
|
if c.options.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-12-18 20:42:12 +01:00
|
|
|
return fs.Abs(c.options.LogFilename)
|
2020-01-02 00:03:07 +01:00
|
|
|
}
|
|
|
|
|
2022-04-06 17:46:41 +02:00
|
|
|
// CaseInsensitive checks if the storage path is case-insensitive.
|
2020-12-26 18:06:54 +01:00
|
|
|
func (c *Config) CaseInsensitive() (result bool, err error) {
|
|
|
|
storagePath := c.StoragePath()
|
|
|
|
return fs.CaseInsensitive(storagePath)
|
|
|
|
}
|
|
|
|
|
2020-01-02 00:03:07 +01:00
|
|
|
// OriginalsPath returns the originals.
|
|
|
|
func (c *Config) OriginalsPath() string {
|
2020-12-18 20:42:12 +01:00
|
|
|
if c.options.OriginalsPath == "" {
|
2020-05-31 12:20:32 +02:00
|
|
|
// Try to find the right directory by iterating through a list.
|
2020-12-18 20:42:12 +01:00
|
|
|
c.options.OriginalsPath = fs.FindDir(fs.OriginalPaths)
|
2020-05-31 12:20:32 +02:00
|
|
|
}
|
|
|
|
|
2020-12-18 20:42:12 +01:00
|
|
|
return fs.Abs(c.options.OriginalsPath)
|
2020-01-02 00:03:07 +01:00
|
|
|
}
|
|
|
|
|
2022-07-21 20:23:00 +02:00
|
|
|
// OriginalsDeletable checks if originals can be deleted.
|
|
|
|
func (c *Config) OriginalsDeletable() bool {
|
|
|
|
return !c.ReadOnly() && fs.Writable(c.OriginalsPath()) && c.Settings().Features.Delete
|
|
|
|
}
|
|
|
|
|
2020-01-02 00:03:07 +01:00
|
|
|
// ImportPath returns the import directory.
|
|
|
|
func (c *Config) ImportPath() string {
|
2020-12-18 20:42:12 +01:00
|
|
|
if c.options.ImportPath == "" {
|
2020-05-31 12:20:32 +02:00
|
|
|
// Try to find the right directory by iterating through a list.
|
2020-12-18 20:42:12 +01:00
|
|
|
c.options.ImportPath = fs.FindDir(fs.ImportPaths)
|
2020-05-31 12:20:32 +02:00
|
|
|
}
|
|
|
|
|
2020-12-18 20:42:12 +01:00
|
|
|
return fs.Abs(c.options.ImportPath)
|
2020-01-02 00:03:07 +01:00
|
|
|
}
|
|
|
|
|
2022-09-30 00:42:19 +02:00
|
|
|
// ImportDest returns the relative originals path to which the files should be imported by default.
|
|
|
|
func (c *Config) ImportDest() string {
|
|
|
|
return clean.UserPath(c.options.ImportDest)
|
|
|
|
}
|
|
|
|
|
2020-09-06 14:18:40 +02:00
|
|
|
// SidecarPath returns the storage path for generated sidecar files (relative or absolute).
|
2020-06-07 10:09:35 +02:00
|
|
|
func (c *Config) SidecarPath() string {
|
2020-12-18 20:42:12 +01:00
|
|
|
if c.options.SidecarPath == "" {
|
|
|
|
c.options.SidecarPath = filepath.Join(c.StoragePath(), "sidecar")
|
2020-06-07 10:09:35 +02:00
|
|
|
}
|
|
|
|
|
2020-12-18 20:42:12 +01:00
|
|
|
return c.options.SidecarPath
|
2020-05-19 11:00:17 +02:00
|
|
|
}
|
|
|
|
|
2022-04-06 17:46:41 +02:00
|
|
|
// SidecarPathIsAbs checks if sidecar path is absolute.
|
2020-06-07 14:33:07 +02:00
|
|
|
func (c *Config) SidecarPathIsAbs() bool {
|
|
|
|
return filepath.IsAbs(c.SidecarPath())
|
|
|
|
}
|
|
|
|
|
2022-04-06 17:46:41 +02:00
|
|
|
// SidecarWritable checks if sidecar files can be created.
|
2020-06-07 14:33:07 +02:00
|
|
|
func (c *Config) SidecarWritable() bool {
|
|
|
|
return !c.ReadOnly() || c.SidecarPathIsAbs()
|
|
|
|
}
|
|
|
|
|
2023-03-14 21:47:14 +01:00
|
|
|
// UsersPath returns the relative base path for user assets.
|
2022-10-17 19:07:38 +02:00
|
|
|
func (c *Config) UsersPath() string {
|
|
|
|
// Set default.
|
|
|
|
if c.options.UsersPath == "" {
|
2023-03-14 21:47:14 +01:00
|
|
|
return "users"
|
2022-10-17 19:07:38 +02:00
|
|
|
}
|
|
|
|
|
2023-03-14 21:47:14 +01:00
|
|
|
return clean.UserPath(c.options.UsersPath)
|
2022-10-17 19:07:38 +02:00
|
|
|
}
|
|
|
|
|
2023-03-14 21:47:14 +01:00
|
|
|
// UsersOriginalsPath returns the users originals base path.
|
|
|
|
func (c *Config) UsersOriginalsPath() string {
|
|
|
|
return filepath.Join(c.OriginalsPath(), c.UsersPath())
|
|
|
|
}
|
|
|
|
|
2023-03-14 23:17:12 +01:00
|
|
|
// UsersStoragePath returns the users storage base path.
|
|
|
|
func (c *Config) UsersStoragePath() string {
|
|
|
|
return filepath.Join(c.StoragePath(), "users")
|
|
|
|
}
|
|
|
|
|
2023-03-14 21:47:14 +01:00
|
|
|
// UserStoragePath returns the storage path for user assets.
|
|
|
|
func (c *Config) UserStoragePath(userUid string) string {
|
2022-10-17 19:07:38 +02:00
|
|
|
if !rnd.IsUID(userUid, 0) {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2023-03-14 22:19:16 +01:00
|
|
|
dir := filepath.Join(c.UsersStoragePath(), userUid)
|
2022-10-17 19:07:38 +02:00
|
|
|
|
2022-10-31 15:01:48 +01:00
|
|
|
if err := os.MkdirAll(dir, fs.ModeDir); err != nil {
|
2022-10-17 19:07:38 +02:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return dir
|
|
|
|
}
|
|
|
|
|
|
|
|
// UserUploadPath returns the upload path for the specified user.
|
|
|
|
func (c *Config) UserUploadPath(userUid, token string) (string, error) {
|
|
|
|
if !rnd.IsUID(userUid, 0) {
|
|
|
|
return "", fmt.Errorf("invalid uid")
|
|
|
|
}
|
|
|
|
|
2023-03-14 21:47:14 +01:00
|
|
|
dir := filepath.Join(c.UserStoragePath(userUid), "upload", clean.Token(token))
|
2022-10-17 19:07:38 +02:00
|
|
|
|
2022-10-31 15:01:48 +01:00
|
|
|
if err := os.MkdirAll(dir, fs.ModeDir); err != nil {
|
2022-10-17 19:07:38 +02:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return dir, nil
|
|
|
|
}
|
|
|
|
|
2022-07-21 20:23:00 +02:00
|
|
|
// TempPath returns the cached temporary directory name e.g. for uploads and downloads.
|
2020-04-06 16:34:29 +02:00
|
|
|
func (c *Config) TempPath() string {
|
2022-07-19 20:41:36 +02:00
|
|
|
// Return cached value?
|
|
|
|
if tempPath == "" {
|
|
|
|
tempPath = c.tempPath()
|
|
|
|
}
|
|
|
|
|
|
|
|
return tempPath
|
|
|
|
}
|
|
|
|
|
2022-07-21 20:23:00 +02:00
|
|
|
// tempPath determines the temporary directory name e.g. for uploads and downloads.
|
2022-07-19 20:41:36 +02:00
|
|
|
func (c *Config) tempPath() string {
|
2022-07-21 20:23:00 +02:00
|
|
|
osTempDir := os.TempDir()
|
|
|
|
|
|
|
|
// Empty default?
|
|
|
|
if osTempDir == "" {
|
|
|
|
switch runtime.GOOS {
|
|
|
|
case "android":
|
|
|
|
osTempDir = "/data/local/tmp"
|
|
|
|
case "windows":
|
|
|
|
osTempDir = "C:/Windows/Temp"
|
|
|
|
default:
|
|
|
|
osTempDir = "/tmp"
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("config: empty default temp folder path, using %s", clean.Log(osTempDir))
|
|
|
|
}
|
|
|
|
|
2022-07-19 20:41:36 +02:00
|
|
|
// Check configured temp path first.
|
2022-04-18 13:55:17 +02:00
|
|
|
if c.options.TempPath != "" {
|
2022-07-19 20:41:36 +02:00
|
|
|
if dir := fs.Abs(c.options.TempPath); dir == "" {
|
|
|
|
// Ignore.
|
2022-10-31 15:01:48 +01:00
|
|
|
} else if err := os.MkdirAll(dir, fs.ModeDir); err != nil {
|
2022-07-19 20:41:36 +02:00
|
|
|
// Ignore.
|
|
|
|
} else if fs.PathWritable(dir) {
|
|
|
|
return dir
|
2022-04-18 13:55:17 +02:00
|
|
|
}
|
2020-04-06 16:34:29 +02:00
|
|
|
}
|
|
|
|
|
2022-07-19 20:41:36 +02:00
|
|
|
// Find alternative temp path based on storage serial checksum.
|
2022-07-21 20:23:00 +02:00
|
|
|
if dir := filepath.Join(osTempDir, "photoprism_"+c.SerialChecksum()); dir == "" {
|
2022-07-19 20:41:36 +02:00
|
|
|
// Ignore.
|
2022-10-31 15:01:48 +01:00
|
|
|
} else if err := os.MkdirAll(dir, fs.ModeDir); err != nil {
|
2022-07-19 20:41:36 +02:00
|
|
|
// Ignore.
|
|
|
|
} else if fs.PathWritable(dir) {
|
|
|
|
return dir
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find alternative temp path based on built-in TempDir() function.
|
2022-07-21 20:23:00 +02:00
|
|
|
if dir, err := ioutil.TempDir(osTempDir, "photoprism_"); err != nil || dir == "" {
|
2022-07-19 20:41:36 +02:00
|
|
|
// Ignore.
|
2022-10-31 15:01:48 +01:00
|
|
|
} else if err = os.MkdirAll(dir, fs.ModeDir); err != nil {
|
2022-07-19 20:41:36 +02:00
|
|
|
// Ignore.
|
|
|
|
} else if fs.PathWritable(dir) {
|
|
|
|
return dir
|
|
|
|
}
|
|
|
|
|
2022-07-21 20:23:00 +02:00
|
|
|
return osTempDir
|
2020-04-06 16:34:29 +02:00
|
|
|
}
|
|
|
|
|
2020-12-18 09:11:42 +01:00
|
|
|
// CachePath returns the path for cache files.
|
2020-01-02 00:03:07 +01:00
|
|
|
func (c *Config) CachePath() string {
|
2020-12-18 20:42:12 +01:00
|
|
|
if c.options.CachePath == "" {
|
2020-05-31 02:09:52 +02:00
|
|
|
return filepath.Join(c.StoragePath(), "cache")
|
|
|
|
}
|
|
|
|
|
2020-12-18 20:42:12 +01:00
|
|
|
return fs.Abs(c.options.CachePath)
|
2020-01-02 00:03:07 +01:00
|
|
|
}
|
|
|
|
|
2022-10-26 18:51:28 +02:00
|
|
|
// CmdCachePath returns a path that external CLI tools can use as cache directory.
|
2022-04-18 13:55:17 +02:00
|
|
|
func (c *Config) CmdCachePath() string {
|
|
|
|
return filepath.Join(c.CachePath(), "cmd")
|
|
|
|
}
|
|
|
|
|
2022-10-26 18:51:28 +02:00
|
|
|
// CmdLibPath returns the dynamic loader path that external CLI tools should use.
|
|
|
|
func (c *Config) CmdLibPath() string {
|
|
|
|
if dir := os.Getenv("LD_LIBRARY_PATH"); dir != "" {
|
|
|
|
return dir
|
|
|
|
}
|
|
|
|
|
|
|
|
return "/usr/local/lib:/usr/lib"
|
|
|
|
}
|
|
|
|
|
2023-02-21 13:31:30 +01:00
|
|
|
// MediaCachePath returns the media cache path.
|
|
|
|
func (c *Config) MediaCachePath() string {
|
|
|
|
return filepath.Join(c.CachePath(), "media")
|
|
|
|
}
|
|
|
|
|
|
|
|
// ThumbCachePath returns the thumbnail storage path.
|
2022-04-18 13:55:17 +02:00
|
|
|
func (c *Config) ThumbCachePath() string {
|
2023-02-21 13:31:30 +01:00
|
|
|
return filepath.Join(c.CachePath(), "thumbnails")
|
2022-04-18 13:55:17 +02: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 {
|
2020-12-18 20:42:12 +01:00
|
|
|
if c.options.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.
|
2020-12-04 11:37:31 +01:00
|
|
|
if fs.PathWritable(originalsDir) && !c.ReadOnly() {
|
2020-05-31 11:28:28 +02:00
|
|
|
return originalsDir
|
2020-12-04 11:37:31 +01:00
|
|
|
} else if fs.PathWritable(storageDir) && c.ReadOnly() {
|
2020-05-31 11:28:28 +02:00
|
|
|
return storageDir
|
|
|
|
}
|
|
|
|
|
2020-12-04 11:37:31 +01:00
|
|
|
// Fallback to backup storage path.
|
2020-12-18 20:42:12 +01:00
|
|
|
if fs.PathWritable(c.options.BackupPath) {
|
|
|
|
return fs.Abs(filepath.Join(c.options.BackupPath, dirName))
|
2020-12-04 11:37:31 +01:00
|
|
|
}
|
|
|
|
|
2020-05-31 11:28:28 +02:00
|
|
|
// 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-12-04 11:37:31 +01:00
|
|
|
if fs.PathWritable(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-12-18 20:42:12 +01:00
|
|
|
return fs.Abs(c.options.StoragePath)
|
2020-01-02 00:03:07 +01:00
|
|
|
}
|
|
|
|
|
2020-12-04 11:02:19 +01:00
|
|
|
// BackupPath returns the backup storage path.
|
|
|
|
func (c *Config) BackupPath() string {
|
2020-12-18 20:42:12 +01:00
|
|
|
if fs.PathWritable(c.options.BackupPath) {
|
|
|
|
return fs.Abs(c.options.BackupPath)
|
2020-12-04 11:02:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return filepath.Join(c.StoragePath(), "backup")
|
|
|
|
}
|
|
|
|
|
2020-12-18 09:11:42 +01:00
|
|
|
// AssetsPath returns the path to static assets for models and templates.
|
2020-05-31 02:09:52 +02:00
|
|
|
func (c *Config) AssetsPath() string {
|
2020-12-18 20:42:12 +01:00
|
|
|
if c.options.AssetsPath == "" {
|
2020-08-18 19:09:46 +02:00
|
|
|
// Try to find the right directory by iterating through a list.
|
2020-12-18 20:42:12 +01:00
|
|
|
c.options.AssetsPath = fs.FindDir(fs.AssetPaths)
|
2020-08-18 19:09:46 +02:00
|
|
|
}
|
|
|
|
|
2020-12-18 20:42:12 +01:00
|
|
|
return fs.Abs(c.options.AssetsPath)
|
2020-01-02 00:03:07 +01:00
|
|
|
}
|
|
|
|
|
2022-07-05 23:13:34 +02:00
|
|
|
// CustomAssetsPath returns the path to custom assets such as icons, models and translations.
|
|
|
|
func (c *Config) CustomAssetsPath() string {
|
|
|
|
if c.options.CustomAssetsPath != "" {
|
|
|
|
return fs.Abs(c.options.CustomAssetsPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2022-09-02 21:30:50 +02:00
|
|
|
// CustomStaticPath returns the custom static assets' path.
|
|
|
|
func (c *Config) CustomStaticPath() string {
|
|
|
|
if dir := c.CustomAssetsPath(); dir == "" {
|
|
|
|
return ""
|
|
|
|
} else if dir = filepath.Join(dir, "static"); !fs.PathExists(dir) {
|
|
|
|
return ""
|
|
|
|
} else {
|
|
|
|
return dir
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// CustomStaticFile returns the path to a custom static file.
|
|
|
|
func (c *Config) CustomStaticFile(fileName string) string {
|
|
|
|
if dir := c.CustomStaticPath(); dir == "" {
|
|
|
|
return ""
|
|
|
|
} else {
|
|
|
|
return filepath.Join(dir, fileName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// CustomStaticUri returns the URI to a custom static resource.
|
|
|
|
func (c *Config) CustomStaticUri() string {
|
|
|
|
if dir := c.CustomAssetsPath(); dir == "" {
|
|
|
|
return ""
|
|
|
|
} else {
|
|
|
|
return c.CdnUrl(c.BaseUri(CustomStaticUri))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-20 10:28:01 +01:00
|
|
|
// CustomStaticAssetUri returns the resource URI of the custom static file asset.
|
|
|
|
func (c *Config) CustomStaticAssetUri(res string) string {
|
|
|
|
if dir := c.CustomAssetsPath(); dir == "" {
|
|
|
|
return ""
|
|
|
|
} else {
|
|
|
|
return c.CdnUrl(c.BaseUri(CustomStaticUri)) + "/" + res
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2020-12-11 12:46:28 +01:00
|
|
|
|
2023-06-02 17:07:10 +02:00
|
|
|
// MariadbBin returns the mariadb executable file name.
|
|
|
|
func (c *Config) MariadbBin() string {
|
|
|
|
return findBin("", "mariadb")
|
2020-12-11 13:52:34 +01:00
|
|
|
}
|
|
|
|
|
2023-06-02 17:07:10 +02:00
|
|
|
// MariadbDumpBin returns the mariadb-dump executable file name.
|
|
|
|
func (c *Config) MariadbDumpBin() string {
|
|
|
|
return findBin("", "mariadb-dump")
|
2020-12-11 12:46:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// SqliteBin returns the sqlite executable file name.
|
|
|
|
func (c *Config) SqliteBin() string {
|
2022-10-12 18:39:30 +02:00
|
|
|
return findBin("", "sqlite3")
|
2020-12-11 13:52:34 +01:00
|
|
|
}
|
2020-12-17 18:24:55 +01:00
|
|
|
|
|
|
|
// AlbumsPath returns the storage path for album YAML files.
|
|
|
|
func (c *Config) AlbumsPath() string {
|
|
|
|
return filepath.Join(c.StoragePath(), "albums")
|
|
|
|
}
|
2021-02-21 15:48:46 +01:00
|
|
|
|
2022-01-06 13:21:42 +01:00
|
|
|
// OriginalsAlbumsPath returns the optional album YAML file path inside originals.
|
2021-02-21 15:48:46 +01:00
|
|
|
func (c *Config) OriginalsAlbumsPath() string {
|
|
|
|
return filepath.Join(c.OriginalsPath(), "albums")
|
|
|
|
}
|