mirror of
https://github.com/gtsteffaniak/filebrowser.git
synced 2026-11-27 00:49:41 -05:00
+11



![coderabbitai[bot]](/avatar/4a08c485fbe908b864e4cf902fddaff20a4dd8a58faf8f65f051648bd2ef3153?size=40)




45a14bf2f2
Signed-off-by: MichaIng <micha@dietpi.com> Co-authored-by: Kevin B. <kurami32_kbm@proton.me> Co-authored-by: Cursor <cursoragent@cursor.com> Co-authored-by: Mattijs Bliek <mattijs@mattijsbliek.com> Co-authored-by: GammelSami <39372285+GammelSami@users.noreply.github.com> Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> Co-authored-by: yzxcj797 <54314860+yzxcj797@users.noreply.github.com> Co-authored-by: yzxcj797 <yzxcj797@users.noreply.github.com> Co-authored-by: DireMunchkin <dire.munchkin@proton.me> Co-authored-by: Chris <39135018+Chri6s@users.noreply.github.com> Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> Co-authored-by: Aditya Raj Singh <github@adityaraj.com> Co-authored-by: tbsagdic <86909795+tbsagdic@users.noreply.github.com> Co-authored-by: Claude Opus 5 <noreply@anthropic.com> Co-authored-by: Ethan Lee Qi Yang <pluscompanyhqDEPT@gmail.com> Co-authored-by: Aditya Raj Singh <aditya@bncw.in> Co-authored-by: MichaIng <micha@dietpi.com> Co-authored-by: FandacqkaCorp <david@davidyloreto.com> Co-authored-by: FandacqkaCorp <275647464+FandacqkaCorp@users.noreply.github.com> Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: connorjfarrell <connorjfarrell@users.noreply.github.com>
143 lines
3.8 KiB
Go
143 lines
3.8 KiB
Go
package sharedefaults
|
|
|
|
import (
|
|
"encoding/json"
|
|
|
|
"github.com/gtsteffaniak/filebrowser/backend/internal/database/share"
|
|
"github.com/gtsteffaniak/filebrowser/backend/pkg/settings"
|
|
)
|
|
|
|
// NormalizeUploadShareEditable ensures upload shares always request create permission.
|
|
func NormalizeUploadShareEditable(editable *share.ShareEditable) {
|
|
if editable != nil && editable.ShareType == "upload" {
|
|
editable.AllowCreate = true
|
|
}
|
|
}
|
|
|
|
// ApplyDefaultsToEditable merges default template values into editable for new shares.
|
|
func ApplyDefaultsToEditable(editable *share.ShareEditable, defaults settings.ShareDefaults) {
|
|
if editable == nil {
|
|
return
|
|
}
|
|
current := EditableToDefaults(*editable)
|
|
currentBytes, err := json.Marshal(current)
|
|
if err != nil {
|
|
return
|
|
}
|
|
defaultBytes, err := json.Marshal(defaults)
|
|
if err != nil {
|
|
return
|
|
}
|
|
mergedBytes, err := mergeJSON(defaultBytes, currentBytes)
|
|
if err != nil {
|
|
return
|
|
}
|
|
var merged settings.ShareDefaults
|
|
if err := json.Unmarshal(mergedBytes, &merged); err != nil {
|
|
return
|
|
}
|
|
*editable = DefaultsToEditable(merged)
|
|
}
|
|
|
|
// ApplyEnforcedDefaults overwrites enforced fields on editable from the defaults template.
|
|
func ApplyEnforcedDefaults(editable *share.ShareEditable, defaults settings.ShareDefaults, enforced settings.ShareDefaultsEnforcement) {
|
|
if editable == nil {
|
|
return
|
|
}
|
|
paths := settings.ShareEnforcedPathSet(enforced)
|
|
if len(paths) == 0 {
|
|
return
|
|
}
|
|
currentBytes, err := json.Marshal(EditableToDefaults(*editable))
|
|
if err != nil {
|
|
return
|
|
}
|
|
patchBytes, err := patchJSONForPaths(defaults, paths)
|
|
if err != nil {
|
|
return
|
|
}
|
|
mergedBytes, err := mergeJSON(currentBytes, patchBytes)
|
|
if err != nil {
|
|
return
|
|
}
|
|
var merged settings.ShareDefaults
|
|
if err := json.Unmarshal(mergedBytes, &merged); err != nil {
|
|
return
|
|
}
|
|
*editable = DefaultsToEditable(merged)
|
|
}
|
|
|
|
// ValidateEditableNotEnforced rejects requests where enforced fields differ from the defaults template.
|
|
func ValidateEditableNotEnforced(after *share.ShareEditable, enforced settings.ShareDefaultsEnforcement, defaults settings.ShareDefaults) error {
|
|
if after == nil {
|
|
return nil
|
|
}
|
|
paths := settings.ShareEnforcedPathSet(enforced)
|
|
if len(paths) == 0 {
|
|
return nil
|
|
}
|
|
afterDefaults := EditableToDefaults(*after)
|
|
for path := range paths {
|
|
expected, ok := settings.ShareDefaultsValueAtPath(defaults, path)
|
|
if !ok {
|
|
continue
|
|
}
|
|
actual, ok := settings.ShareDefaultsValueAtPath(afterDefaults, path)
|
|
if !ok || !jsonValuesEqual(expected, actual) {
|
|
return settings.ErrEnforcedShareValueMismatch{Path: path}
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func patchJSONForPaths(source settings.ShareDefaults, paths map[string]struct{}) ([]byte, error) {
|
|
patchMap := make(map[string]interface{}, len(paths))
|
|
for path := range paths {
|
|
val, ok := settings.ShareDefaultsValueAtPath(source, path)
|
|
if !ok {
|
|
continue
|
|
}
|
|
patchMap[path] = val
|
|
}
|
|
return json.Marshal(patchMap)
|
|
}
|
|
|
|
func mergeJSON(baseJSON, patchJSON []byte) ([]byte, error) {
|
|
var base map[string]interface{}
|
|
if err := json.Unmarshal(baseJSON, &base); err != nil {
|
|
return nil, err
|
|
}
|
|
var patch map[string]interface{}
|
|
if err := json.Unmarshal(patchJSON, &patch); err != nil {
|
|
return nil, err
|
|
}
|
|
deepMergeMaps(base, patch)
|
|
return json.Marshal(base)
|
|
}
|
|
|
|
func deepMergeMaps(base, patch map[string]interface{}) {
|
|
for key, patchVal := range patch {
|
|
baseVal, exists := base[key]
|
|
if !exists {
|
|
base[key] = patchVal
|
|
continue
|
|
}
|
|
baseMap, baseOK := baseVal.(map[string]interface{})
|
|
patchMap, patchOK := patchVal.(map[string]interface{})
|
|
if baseOK && patchOK {
|
|
deepMergeMaps(baseMap, patchMap)
|
|
continue
|
|
}
|
|
base[key] = patchVal
|
|
}
|
|
}
|
|
|
|
func jsonValuesEqual(a, b interface{}) bool {
|
|
ab, errA := json.Marshal(a)
|
|
bb, errB := json.Marshal(b)
|
|
if errA != nil || errB != nil {
|
|
return false
|
|
}
|
|
return string(ab) == string(bb)
|
|
}
|