-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
config_storage.go
654 lines (540 loc) Β· 18.4 KB
/
config_storage.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
package config
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"os/user"
"path/filepath"
"runtime"
"sync"
"github.com/photoprism/photoprism/pkg/clean"
"github.com/photoprism/photoprism/pkg/fs"
"github.com/photoprism/photoprism/pkg/rnd"
)
// binPaths stores known executable paths.
var (
binPaths = make(map[string]string, 8)
binMu = sync.RWMutex{}
tempPath = ""
)
// findBin resolves the absolute file path of external binaries.
func findBin(configBin, defaultBin string) (binPath string) {
cacheKey := defaultBin + configBin
binMu.RLock()
cached, found := binPaths[cacheKey]
binMu.RUnlock()
// Already found?
if found {
return cached
}
// Default binary name?
if configBin == "" {
binPath = defaultBin
} else {
binPath = configBin
}
// Search for binary.
if path, err := exec.LookPath(binPath); err == nil {
binPath = path
}
// Found?
if !fs.FileExists(binPath) {
binPath = ""
} else {
binMu.Lock()
binPaths[cacheKey] = binPath
binMu.Unlock()
}
return binPath
}
// CreateDirectories creates directories for storing photos, metadata and cache files.
func (c *Config) CreateDirectories() error {
// Error if the originals and storage path are identical.
if c.OriginalsPath() == c.StoragePath() {
return fmt.Errorf("originals and storage folder must be different directories")
}
// Make sure that the configured storage path exists and initialize it with
// ".ppstorage" and ".ppignore files" so that it is not accidentally indexed.
if dir := c.StoragePath(); dir == "" {
return notFoundError("storage")
} else if err := fs.MkdirAll(dir); err != nil {
return createError(dir, err)
} else if _, err = fs.WriteUnixTime(filepath.Join(dir, fs.PPStorageFilename)); err != nil {
return fmt.Errorf("%s file in %s could not be created", fs.PPStorageFilename, clean.Log(dir))
} else if err = fs.WriteString(filepath.Join(dir, fs.PPIgnoreFilename), fs.PPIgnoreAll); err != nil {
return fmt.Errorf("%s file in %s could not be created", fs.PPIgnoreFilename, clean.Log(dir))
}
// Create originals path if it does not exist yet and return an error if it could be a storage folder.
if dir := c.OriginalsPath(); dir == "" {
return notFoundError("originals")
} else if err := fs.MkdirAll(dir); err != nil {
return createError(dir, err)
} else if fs.FileExists(filepath.Join(dir, fs.PPStorageFilename)) {
return fmt.Errorf("found a %s file in the originals path", fs.PPStorageFilename)
}
// Create import path if it does not exist yet and return an error if it could be a storage folder.
if dir := c.ImportPath(); dir == "" {
return notFoundError("import")
} else if err := fs.MkdirAll(dir); err != nil {
return createError(dir, err)
} else if fs.FileExists(filepath.Join(dir, fs.PPStorageFilename)) {
return fmt.Errorf("found a %s file in the import path", fs.PPStorageFilename)
}
// Create storage path if it doesn't exist yet.
if dir := c.UsersStoragePath(); dir == "" {
return notFoundError("users storage")
} else if err := fs.MkdirAll(dir); err != nil {
return createError(dir, err)
}
// Create assets path if it doesn't exist yet.
if dir := c.AssetsPath(); dir == "" {
return notFoundError("assets")
} else if err := fs.MkdirAll(dir); err != nil {
return createError(dir, err)
}
// Create command cache storage path if it doesn't exist yet.
if dir := c.CmdCachePath(); dir == "" {
return notFoundError("cmd cache")
} else if err := fs.MkdirAll(dir); err != nil {
return createError(dir, err)
}
// Create backup base path if it doesn't exist yet.
if dir := c.BackupBasePath(); dir == "" {
return notFoundError("backup")
} else if err := fs.MkdirAll(dir); err != nil {
return createError(dir, err)
}
// Create sidecar storage path if it doesn't exist yet.
if dir := c.SidecarPath(); filepath.IsAbs(dir) {
if err := fs.MkdirAll(dir); err != nil {
return createError(dir, err)
}
}
// Create and initialize cache storage directory.
if dir := c.CachePath(); dir == "" {
return notFoundError("cache")
} else if err := fs.MkdirAll(dir); err != nil {
return createError(c.CachePath(), err)
} else if err = fs.WriteString(filepath.Join(dir, fs.PPIgnoreFilename), fs.PPIgnoreAll); err != nil {
return fmt.Errorf("%s file in %s could not be created", fs.PPIgnoreFilename, clean.Log(dir))
}
// Create media cache storage path if it doesn't exist yet.
if dir := c.MediaCachePath(); dir == "" {
return notFoundError("media")
} else if err := fs.MkdirAll(dir); err != nil {
return createError(dir, err)
}
// Create thumbnail cache storage path if it doesn't exist yet.
if dir := c.ThumbCachePath(); dir == "" {
return notFoundError("thumbs")
} else if err := fs.MkdirAll(dir); err != nil {
return createError(dir, err)
}
// Create and initialize config directory.
if dir := c.ConfigPath(); dir == "" {
return notFoundError("config")
} else if err := fs.MkdirAll(dir); err != nil {
return createError(dir, err)
} else if err = fs.WriteString(filepath.Join(dir, fs.PPIgnoreFilename), fs.PPIgnoreAll); err != nil {
return fmt.Errorf("%s file in %s could not be created", fs.PPIgnoreFilename, clean.Log(dir))
}
// Create certificates config path if it doesn't exist yet.
if dir := c.CertificatesPath(); dir == "" {
return notFoundError("certificates")
} else if err := fs.MkdirAll(dir); err != nil {
return createError(dir, err)
}
// Create temporary file path if it doesn't exist yet.
if dir := c.TempPath(); dir == "" {
return notFoundError("temp")
} else if err := fs.MkdirAll(dir); err != nil {
return createError(dir, err)
}
// Create albums backup path if it doesn't exist yet.
if dir := c.BackupAlbumsPath(); dir == "" {
return notFoundError("albums")
} else if err := fs.MkdirAll(dir); err != nil {
return createError(dir, err)
}
// Create TensorFlow model path if it doesn't exist yet.
if dir := c.TensorFlowModelPath(); dir == "" {
return notFoundError("tensorflow model")
} else if err := fs.MkdirAll(dir); err != nil {
return createError(dir, err)
}
// Create frontend build path if it doesn't exist yet.
if dir := c.BuildPath(); dir == "" {
return notFoundError("build")
} else if err := fs.MkdirAll(dir); err != nil {
return createError(dir, err)
}
if dir := filepath.Dir(c.PIDFilename()); dir == "" {
return notFoundError("pid file")
} else if err := fs.MkdirAll(dir); err != nil {
return createError(dir, err)
}
if dir := filepath.Dir(c.LogFilename()); dir == "" {
return notFoundError("log file")
} else if err := fs.MkdirAll(dir); err != nil {
return createError(dir, err)
}
if c.DarktableEnabled() {
if dir, err := c.CreateDarktableCachePath(); err != nil {
return fmt.Errorf("could not create darktable cache path %s", clean.Log(dir))
}
if dir, err := c.CreateDarktableConfigPath(); err != nil {
return fmt.Errorf("could not create darktable cache path %s", clean.Log(dir))
}
}
return nil
}
// ConfigPath returns the config path.
func (c *Config) ConfigPath() string {
if c.options.ConfigPath == "" {
if fs.PathExists(filepath.Join(c.StoragePath(), "settings")) {
return filepath.Join(c.StoragePath(), "settings")
}
return filepath.Join(c.StoragePath(), "config")
}
return fs.Abs(c.options.ConfigPath)
}
// 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 {
return fs.Abs(c.options.DefaultsYaml)
}
// HubConfigFile returns the backend api config file name.
func (c *Config) HubConfigFile() string {
return filepath.Join(c.ConfigPath(), "hub.yml")
}
// SettingsYaml returns the settings YAML filename.
func (c *Config) SettingsYaml() string {
return filepath.Join(c.ConfigPath(), "settings.yml")
}
// 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
}
// PIDFilename returns the filename for storing the server process id (pid).
func (c *Config) PIDFilename() string {
if c.options.PIDFilename == "" {
return filepath.Join(c.StoragePath(), "photoprism.pid")
}
return fs.Abs(c.options.PIDFilename)
}
// LogFilename returns the filename for storing server logs.
func (c *Config) LogFilename() string {
if c.options.LogFilename == "" {
return filepath.Join(c.StoragePath(), "photoprism.log")
}
return fs.Abs(c.options.LogFilename)
}
// CaseInsensitive checks if the storage path is case-insensitive.
func (c *Config) CaseInsensitive() (result bool, err error) {
storagePath := c.StoragePath()
return fs.CaseInsensitive(storagePath)
}
// OriginalsPath returns the originals.
func (c *Config) OriginalsPath() string {
if c.options.OriginalsPath == "" {
// Try to find the right directory by iterating through a list.
c.options.OriginalsPath = fs.FindDir(fs.OriginalPaths)
}
return fs.Abs(c.options.OriginalsPath)
}
// OriginalsDeletable checks if originals can be deleted.
func (c *Config) OriginalsDeletable() bool {
return !c.ReadOnly() && fs.Writable(c.OriginalsPath()) && c.Settings().Features.Delete
}
// ImportPath returns the import directory.
func (c *Config) ImportPath() string {
if c.options.ImportPath == "" {
// Try to find the right directory by iterating through a list.
c.options.ImportPath = fs.FindDir(fs.ImportPaths)
}
return fs.Abs(c.options.ImportPath)
}
// 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)
}
// SidecarPath returns the storage path for generated sidecar files (relative or absolute).
func (c *Config) SidecarPath() string {
if c.options.SidecarPath == "" {
c.options.SidecarPath = filepath.Join(c.StoragePath(), "sidecar")
}
return c.options.SidecarPath
}
// SidecarPathIsAbs checks if sidecar path is absolute.
func (c *Config) SidecarPathIsAbs() bool {
return filepath.IsAbs(c.SidecarPath())
}
// SidecarWritable checks if sidecar files can be created.
func (c *Config) SidecarWritable() bool {
return !c.ReadOnly() || c.SidecarPathIsAbs()
}
// SidecarYaml checks if sidecar YAML files should be created and updated.
func (c *Config) SidecarYaml() bool {
if !c.SidecarWritable() || c.options.DisableBackups {
return false
}
return c.options.SidecarYaml
}
// UsersPath returns the relative base path for user assets.
func (c *Config) UsersPath() string {
// Set default.
if c.options.UsersPath == "" {
return "users"
}
return clean.UserPath(c.options.UsersPath)
}
// UsersOriginalsPath returns the users originals base path.
func (c *Config) UsersOriginalsPath() string {
return filepath.Join(c.OriginalsPath(), c.UsersPath())
}
// UsersStoragePath returns the users storage base path.
func (c *Config) UsersStoragePath() string {
return filepath.Join(c.StoragePath(), "users")
}
// UserStoragePath returns the storage path for user assets.
func (c *Config) UserStoragePath(userUid string) string {
if !rnd.IsUID(userUid, 0) {
return ""
}
dir := filepath.Join(c.UsersStoragePath(), userUid)
if err := fs.MkdirAll(dir); err != nil {
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")
}
dir := filepath.Join(c.UserStoragePath(userUid), "upload", clean.Token(token))
if err := fs.MkdirAll(dir); err != nil {
return "", err
}
return dir, nil
}
// TempPath returns the cached temporary directory name e.g. for uploads and downloads.
func (c *Config) TempPath() string {
// Return cached value?
if tempPath == "" {
tempPath = c.tempPath()
}
return tempPath
}
// tempPath determines the temporary directory name e.g. for uploads and downloads.
func (c *Config) tempPath() string {
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))
}
// Check configured temp path first.
if c.options.TempPath != "" {
if dir := fs.Abs(c.options.TempPath); dir == "" {
// Ignore.
} else if err := fs.MkdirAll(dir); err != nil {
// Ignore.
} else if fs.PathWritable(dir) {
return dir
}
}
// Find alternative temp path based on storage serial checksum.
if dir := filepath.Join(osTempDir, "photoprism_"+c.SerialChecksum()); dir == "" {
// Ignore.
} else if err := fs.MkdirAll(dir); err != nil {
// Ignore.
} else if fs.PathWritable(dir) {
return dir
}
// Find alternative temp path based on built-in TempDir() function.
if dir, err := ioutil.TempDir(osTempDir, "photoprism_"); err != nil || dir == "" {
// Ignore.
} else if err = fs.MkdirAll(dir); err != nil {
// Ignore.
} else if fs.PathWritable(dir) {
return dir
}
return osTempDir
}
// CachePath returns the path for cache files.
func (c *Config) CachePath() string {
if c.options.CachePath == "" {
return filepath.Join(c.StoragePath(), "cache")
}
return fs.Abs(c.options.CachePath)
}
// CmdCachePath returns a path that external CLI tools can use as cache directory.
func (c *Config) CmdCachePath() string {
return filepath.Join(c.CachePath(), "cmd")
}
// 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"
}
// MediaCachePath returns the main media cache path.
func (c *Config) MediaCachePath() string {
return filepath.Join(c.CachePath(), "media")
}
// MediaFileCachePath returns the cache subdirectory path for a given file hash.
func (c *Config) MediaFileCachePath(hash string) string {
dir := c.MediaCachePath()
switch len(hash) {
case 0:
return dir
case 1:
dir = filepath.Join(dir, hash[0:1])
case 2:
dir = filepath.Join(dir, hash[0:1], hash[1:2])
default:
dir = filepath.Join(dir, hash[0:1], hash[1:2], hash[2:3])
}
// Ensure the subdirectory exists, or log an error otherwise.
if err := fs.MkdirAll(dir); err != nil {
log.Errorf("cache: failed to create subdirectory for media file")
}
return dir
}
// ThumbCachePath returns the thumbnail storage path.
func (c *Config) ThumbCachePath() string {
return filepath.Join(c.CachePath(), "thumbnails")
}
// StoragePath returns the path for generated files like cache and index.
func (c *Config) StoragePath() string {
if c.options.StoragePath == "" {
const dirName = "storage"
// Default directories.
originalsDir := fs.Abs(filepath.Join(c.OriginalsPath(), fs.PPHiddenPathname, dirName))
storageDir := fs.Abs(dirName)
// Find existing directories.
if fs.PathWritable(originalsDir) && !c.ReadOnly() {
return originalsDir
} else if fs.PathWritable(storageDir) && c.ReadOnly() {
return storageDir
}
// Fallback to backup storage path.
if fs.PathWritable(c.options.BackupPath) {
return fs.Abs(filepath.Join(c.options.BackupPath, dirName))
}
// Use .photoprism in home directory?
if usr, _ := user.Current(); usr.HomeDir != "" {
p := fs.Abs(filepath.Join(usr.HomeDir, fs.PPHiddenPathname, dirName))
if fs.PathWritable(p) || c.ReadOnly() {
return p
}
}
// Fallback directory in case nothing else works.
if c.ReadOnly() {
return fs.Abs(filepath.Join(fs.PPHiddenPathname, dirName))
}
// Store cache and index in "originals/.photoprism/storage".
return originalsDir
}
return fs.Abs(c.options.StoragePath)
}
// AssetsPath returns the path to static assets for models and templates.
func (c *Config) AssetsPath() string {
if c.options.AssetsPath == "" {
// Try to find the right directory by iterating through a list.
c.options.AssetsPath = fs.FindDir(fs.AssetPaths)
}
return fs.Abs(c.options.AssetsPath)
}
// 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 ""
}
// 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))
}
}
// 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
}
}
// LocalesPath returns the translation locales path.
func (c *Config) LocalesPath() string {
return filepath.Join(c.AssetsPath(), "locales")
}
// ExamplesPath returns the example files path.
func (c *Config) ExamplesPath() string {
return filepath.Join(c.AssetsPath(), "examples")
}
// TestdataPath returns the test files path.
func (c *Config) TestdataPath() string {
return filepath.Join(c.StoragePath(), "testdata")
}
// MariadbBin returns the mariadb executable file name.
func (c *Config) MariadbBin() string {
return findBin("", "mariadb")
}
// MariadbDumpBin returns the mariadb-dump executable file name.
func (c *Config) MariadbDumpBin() string {
return findBin("", "mariadb-dump")
}
// SqliteBin returns the sqlite executable file name.
func (c *Config) SqliteBin() string {
return findBin("", "sqlite3")
}
// OriginalsAlbumsPath returns the optional album YAML file path inside originals.
func (c *Config) OriginalsAlbumsPath() string {
return filepath.Join(c.OriginalsPath(), "albums")
}