/
settings.go
164 lines (142 loc) Β· 3.54 KB
/
settings.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
package customize
import (
"fmt"
"os"
"gopkg.in/yaml.v2"
"github.com/photoprism/photoprism/pkg/clean"
"github.com/photoprism/photoprism/pkg/fs"
"github.com/photoprism/photoprism/pkg/i18n"
)
const (
RootPath = "/"
)
// Settings represents user settings for Web UI, indexing, and import.
type Settings struct {
UI UISettings `json:"ui" yaml:"UI"`
Search SearchSettings `json:"search" yaml:"Search"`
Maps MapsSettings `json:"maps" yaml:"Maps"`
Features FeatureSettings `json:"features" yaml:"Features"`
Import ImportSettings `json:"import" yaml:"Import"`
Index IndexSettings `json:"index" yaml:"Index"`
Stack StackSettings `json:"stack" yaml:"Stack"`
Share ShareSettings `json:"share" yaml:"Share"`
Download DownloadSettings `json:"download" yaml:"Download"`
Templates TemplateSettings `json:"templates" yaml:"Templates"`
}
// NewDefaultSettings creates a new default Settings instance.
func NewDefaultSettings() *Settings {
return NewSettings(DefaultTheme, DefaultLocale)
}
// NewSettings creates a new Settings instance.
func NewSettings(theme, lang string) *Settings {
return &Settings{
UI: UISettings{
Scrollbar: true,
Zoom: false,
Theme: theme,
Language: lang,
},
Search: SearchSettings{
BatchSize: 0,
},
Maps: MapsSettings{
Animate: 0,
Style: "",
},
Features: FeatureSettings{
Favorites: true,
Reactions: true,
Ratings: true,
Upload: true,
Download: true,
Private: true,
Files: true,
Videos: true,
Folders: true,
Albums: true,
Moments: true,
Estimates: true,
People: true,
Labels: true,
Places: true,
Edit: true,
Archive: true,
Review: true,
Share: true,
Library: true,
Import: true,
Logs: true,
Search: true,
Settings: true,
Services: true,
Account: true,
Delete: true,
},
Import: ImportSettings{
Path: RootPath,
Move: false,
},
Index: IndexSettings{
Path: RootPath,
Rescan: false,
Convert: true,
},
Stack: StackSettings{
UUID: true,
Meta: true,
Name: false,
},
Share: ShareSettings{
Title: "",
},
Download: NewDownloadSettings(),
Templates: TemplateSettings{
Default: "index.gohtml",
},
}
}
// Propagate updates settings in other packages as needed.
func (s *Settings) Propagate() {
i18n.SetLocale(s.UI.Language)
}
// StackSequences checks if files should be stacked based on their file name prefix (sequential names).
func (s Settings) StackSequences() bool {
return s.Stack.Name
}
// StackUUID checks if files should be stacked based on unique image or instance id.
func (s Settings) StackUUID() bool {
return s.Stack.UUID
}
// StackMeta checks if files should be stacked based on their place and time metadata.
func (s Settings) StackMeta() bool {
return s.Stack.Meta
}
// Load user settings from file.
func (s *Settings) Load(fileName string) error {
if fileName == "" {
return fmt.Errorf("no settings filename provided")
} else if !fs.FileExists(fileName) {
return fmt.Errorf("settings file not found: %s", clean.Log(fileName))
}
yamlConfig, err := os.ReadFile(fileName)
if err != nil {
return err
}
if err = yaml.Unmarshal(yamlConfig, s); err != nil {
return err
}
s.Propagate()
return nil
}
// Save user settings to a file.
func (s *Settings) Save(fileName string) error {
data, err := yaml.Marshal(s)
if err != nil {
return err
}
s.Propagate()
if err = os.WriteFile(fileName, data, fs.ModeFile); err != nil {
return err
}
return nil
}