Improve storage path auto-configuration #66

Signed-off-by: Michael Mayer <michael@liquidbytes.net>
This commit is contained in:
Michael Mayer 2020-05-31 11:28:28 +02:00
parent 1d8e7f1ad3
commit 4684f66298
2 changed files with 26 additions and 8 deletions

View file

@ -211,20 +211,38 @@ func (c *Config) CachePath() string {
return fs.Abs(c.params.CachePath)
}
// StoragePath returns the path for generated files.
// StoragePath returns the path for generated files like cache and index.
func (c *Config) StoragePath() string {
if c.params.StoragePath == "" {
if usr, _ := user.Current(); usr.HomeDir != "" {
p := filepath.Join(usr.HomeDir, fs.HiddenPath, "storage")
const dirName = "storage"
if fs.PathExists(p) {
// 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?
if usr, _ := user.Current(); usr.HomeDir != "" {
p := fs.Abs(filepath.Join(usr.HomeDir, fs.HiddenPath, dirName))
if fs.PathExists(p) || c.ReadOnly() {
return p
}
}
if !c.ReadOnly() {
return filepath.Join(c.OriginalsPath(), fs.HiddenPath, "storage")
// Fallback directory in case nothing else works.
if c.ReadOnly() {
return fs.Abs(filepath.Join(fs.HiddenPath, dirName))
}
// Store cache and index in "originals/.photoprism/storage".
return originalsDir
}
return fs.Abs(c.params.StoragePath)

View file

@ -119,13 +119,13 @@ var GlobalFlags = []cli.Flag{
},
cli.StringFlag{
Name: "assets-path",
Usage: "assets `PATH` for static files",
Usage: "assets `PATH` for static files like templates and TensorFlow models",
Value: "",
EnvVar: "PHOTOPRISM_ASSETS_PATH",
},
cli.StringFlag{
Name: "storage-path",
Usage: "storage `PATH` for generated files",
Usage: "storage `PATH` for generated files like cache and index",
Value: "",
EnvVar: "PHOTOPRISM_STORAGE_PATH",
},