mirror of
https://github.com/filebrowser/filebrowser.git
synced 2024-06-07 23:00:43 +00:00
01929c72ea
Former-commit-id: a54bc7c8a0fb700d4f90f78c54d6cb028e8acea7 [formerly a0946a1d5b82ce5681684bc0f871bd892c4d6c3f] [formerly 6b23b0abbe5cde6a6348f982975d15a1161359de [formerly 0e7abaa7fb
]]
Former-commit-id: cf9ea1110bcb8b5eb30ecd25db1928659ecea922 [formerly 38cebeff1f9e6b03760f7b277a3941f7bd733fb5]
Former-commit-id: db44c1d5ff8fed361849b5bf1fe48e04c75d7ce7
100 lines
2.0 KiB
Go
100 lines
2.0 KiB
Go
package settings
|
|
|
|
import (
|
|
"strings"
|
|
|
|
"github.com/filebrowser/filebrowser/v2/errors"
|
|
"github.com/filebrowser/filebrowser/v2/rules"
|
|
"github.com/filebrowser/filebrowser/v2/users"
|
|
)
|
|
|
|
// StorageBackend is a settings storage backend.
|
|
type StorageBackend interface {
|
|
Get() (*Settings, error)
|
|
Save(*Settings) error
|
|
GetServer() (*Server, error)
|
|
SaveServer(*Server) error
|
|
}
|
|
|
|
// Storage is a settings storage.
|
|
type Storage struct {
|
|
back StorageBackend
|
|
}
|
|
|
|
// NewStorage creates a settings storage from a backend.
|
|
func NewStorage(back StorageBackend) *Storage {
|
|
return &Storage{back: back}
|
|
}
|
|
|
|
// Get returns the settings for the current instance.
|
|
func (s *Storage) Get() (*Settings, error) {
|
|
return s.back.Get()
|
|
}
|
|
|
|
var defaultEvents = []string{
|
|
"save",
|
|
"copy",
|
|
"rename",
|
|
"upload",
|
|
"delete",
|
|
}
|
|
|
|
// Save saves the settings for the current instance.
|
|
func (s *Storage) Save(set *Settings) error {
|
|
if len(set.Key) == 0 {
|
|
return errors.ErrEmptyKey
|
|
}
|
|
|
|
if set.Defaults.Locale == "" {
|
|
set.Defaults.Locale = "en"
|
|
}
|
|
|
|
if set.Defaults.Commands == nil {
|
|
set.Defaults.Commands = []string{}
|
|
}
|
|
|
|
if set.Defaults.ViewMode == "" {
|
|
set.Defaults.ViewMode = users.MosaicViewMode
|
|
}
|
|
|
|
if set.Rules == nil {
|
|
set.Rules = []rules.Rule{}
|
|
}
|
|
|
|
if set.Shell == nil {
|
|
set.Shell = []string{}
|
|
}
|
|
|
|
if set.Commands == nil {
|
|
set.Commands = map[string][]string{}
|
|
}
|
|
|
|
for _, event := range defaultEvents {
|
|
if _, ok := set.Commands["before_"+event]; !ok {
|
|
set.Commands["before_"+event] = []string{}
|
|
}
|
|
|
|
if _, ok := set.Commands["after_"+event]; !ok {
|
|
set.Commands["after_"+event] = []string{}
|
|
}
|
|
}
|
|
|
|
err := s.back.Save(set)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// GetServer wraps StorageBackend.GetServer.
|
|
func (s *Storage) GetServer() (*Server, error) {
|
|
return s.back.GetServer()
|
|
}
|
|
|
|
// SaveServer wraps StorageBackend.SaveServer and adds some verification.
|
|
func (s *Storage) SaveServer(ser *Server) error {
|
|
ser.BaseURL = strings.TrimSuffix(ser.BaseURL, "/")
|
|
return s.back.SaveServer(ser)
|
|
}
|