filebrowser/backend/settings/config.go

96 lines
2.2 KiB
Go
Raw Normal View History

2023-09-01 14:00:02 +00:00
package settings
import (
"log"
"os"
"github.com/goccy/go-yaml"
2023-09-03 22:03:00 +00:00
"github.com/gtsteffaniak/filebrowser/users"
2023-09-01 14:00:02 +00:00
)
var GlobalConfiguration Settings
2023-09-03 22:03:00 +00:00
func Initialize(configFile string) {
yamlData := loadConfigFile(configFile)
2023-09-03 17:28:00 +00:00
GlobalConfiguration = setDefaults()
err := yaml.Unmarshal(yamlData, &GlobalConfiguration)
2023-09-01 14:00:02 +00:00
if err != nil {
2023-09-03 17:28:00 +00:00
log.Fatalf("Error unmarshaling YAML data: %v", err)
}
2023-09-03 22:16:49 +00:00
GlobalConfiguration.UserDefaults.Perm = GlobalConfiguration.UserDefaults.Permissions
2023-09-03 17:28:00 +00:00
}
2023-09-03 00:16:19 +00:00
2023-09-03 22:03:00 +00:00
func loadConfigFile(configFile string) []byte {
2023-09-03 17:28:00 +00:00
// Open and read the YAML file
2023-09-03 22:03:00 +00:00
yamlFile, err := os.Open(configFile)
2023-09-03 17:28:00 +00:00
if err != nil {
2023-09-03 22:03:00 +00:00
log.Printf("ERROR: opening config file\n %v\n WARNING: Using default config only\n If this was a mistake, please make sure the file exists and is accessible by the filebrowser binary.\n\n", err)
2023-09-25 01:03:09 +00:00
GlobalConfiguration = setDefaults()
2023-09-03 17:28:00 +00:00
return []byte{}
2023-09-01 14:00:02 +00:00
}
defer yamlFile.Close()
2023-09-02 02:03:45 +00:00
stat, err := yamlFile.Stat()
if err != nil {
2023-09-03 00:16:19 +00:00
log.Fatalf("Error getting file information: %s", err.Error())
2023-09-02 02:03:45 +00:00
}
yamlData := make([]byte, stat.Size())
_, err = yamlFile.Read(yamlData)
2023-09-01 14:00:02 +00:00
if err != nil {
log.Fatalf("Error reading YAML data: %v", err)
}
2023-09-03 17:28:00 +00:00
return yamlData
2023-09-01 14:00:02 +00:00
}
2023-09-03 17:28:00 +00:00
func setDefaults() Settings {
return Settings{
2023-09-03 22:03:00 +00:00
Signup: true,
AdminUsername: "admin",
AdminPassword: "admin",
2023-09-02 00:51:13 +00:00
Server: Server{
2023-09-04 02:21:25 +00:00
EnableThumbnails: true,
2023-09-03 22:03:00 +00:00
EnableExec: false,
2023-09-02 00:51:13 +00:00
IndexingInterval: 5,
Port: 8080,
2023-09-03 17:28:00 +00:00
NumImageProcessors: 4,
2023-09-02 02:03:45 +00:00
BaseURL: "",
2023-09-03 22:03:00 +00:00
Database: "database.db",
Log: "stdout",
Root: "/srv",
2023-09-02 00:51:13 +00:00
},
2023-09-01 14:00:02 +00:00
Auth: Auth{
2023-09-02 00:51:13 +00:00
Method: "password",
2023-09-01 14:00:02 +00:00
Recaptcha: Recaptcha{
Host: "",
},
},
2023-09-03 17:28:00 +00:00
UserDefaults: UserDefaults{
2023-09-03 22:16:49 +00:00
Scope: ".",
2023-09-03 22:03:00 +00:00
LockPassword: false,
2023-09-03 17:28:00 +00:00
HideDotfiles: true,
2023-09-03 22:16:49 +00:00
Permissions: users.Permissions{
2023-09-03 22:03:00 +00:00
Create: true,
Rename: true,
Modify: true,
Delete: true,
Share: true,
Download: true,
},
2023-09-03 17:28:00 +00:00
},
2023-09-02 00:51:13 +00:00
}
}
2023-09-30 17:50:20 +00:00
// Apply applies the default options to a user.
func (d *UserDefaults) Apply(u *users.User) {
u.Scope = d.Scope
u.Locale = d.Locale
u.ViewMode = d.ViewMode
u.SingleClick = d.SingleClick
u.Perm = d.Perm
u.Sorting = d.Sorting
u.Commands = d.Commands
u.HideDotfiles = d.HideDotfiles
u.DateFormat = d.DateFormat
}