feat: improve ArtCraft integration and configurable file opening

This commit is contained in:
SantaSpeen committed 2026-11-02 00:37:44 +03:00
1 parent 3bc62d2b69
commit 80bc2c458d
60 files changed
+1738 -232

No files matched your search

+35
View File
@@ -0,0 +1,35 @@
package users
import (
"regexp"
"strings"
"github.com/filebrowser/filebrowser/v2/internal/addons"
)
var autoOpenExtension = regexp.MustCompile(`^\.[a-z0-9][a-z0-9+-]{0,15}$`)
// Preferences select an opener; they never grant access to an addon or file.
func normalizeAutoOpen(values map[string]string) (map[string]string, bool) {
if len(values) > 512 {
return nil, false
}
result := make(map[string]string, len(values))
for extension, handler := range values {
extension = strings.ToLower(strings.TrimSpace(extension))
if !autoOpenExtension.MatchString(extension) {
return nil, false
}
if _, exists := result[extension]; exists {
return nil, false
}
if handler != "preview" && handler != "download" {
tool, ok := addons.Find(handler)
if !ok || !tool.CanOpen(extension) {
return nil, false
}
}
result[extension] = handler
}
return result, true
}
+20
View File
@@ -0,0 +1,20 @@
package users
import "testing"
func TestAutoOpenPreferences(t *testing.T) {
valid, ok := normalizeAutoOpen(map[string]string{".JPG": "photocraft", ".txt": "preview", ".mp3": "download"})
if !ok || valid[".jpg"] != "photocraft" {
t.Fatal("valid mappings rejected")
}
for _, values := range []map[string]string{
{"jpg": "photocraft"}, {"../secret": "download"}, {".jpg": "https://example.com"}, {".mp3": "soundcraft"}, {".jpg": "wordcraft"},
} {
if _, ok := normalizeAutoOpen(values); ok {
t.Fatalf("invalid mapping accepted: %v", values)
}
}
if _, ok := normalizeAutoOpen(nil); !ok {
t.Fatal("legacy users must retain default opening behavior")
}
}
+30 -22
View File
@@ -19,28 +19,29 @@ const (
// User describes a user.
type User struct {
ID uint `storm:"id,increment" json:"id"`
Username string `storm:"unique" json:"username"`
DisplayName string `json:"displayName,omitempty"`
ProfileName string `json:"profileName,omitempty"`
Email string `json:"email,omitempty"`
Password string `json:"password"`
Scope string `json:"scope"`
Locale string `json:"locale"`
LockPassword bool `json:"lockPassword"`
ViewMode ViewMode `json:"viewMode"`
SingleClick bool `json:"singleClick"`
RedirectAfterCopyMove bool `json:"redirectAfterCopyMove"`
Perm Permissions `json:"perm"`
Commands []string `json:"commands"`
Sorting files.Sorting `json:"sorting"`
Fs afero.Fs `json:"-" yaml:"-"`
Rules []rules.Rule `json:"rules"`
HideDotfiles bool `json:"hideDotfiles"`
HidePreviews bool `json:"hidePreviews"`
Dicom3D Dicom3DSettings `json:"dicom3D"`
DateFormat bool `json:"dateFormat"`
AceEditorTheme string `json:"aceEditorTheme"`
ID uint `storm:"id,increment" json:"id"`
Username string `storm:"unique" json:"username"`
DisplayName string `json:"displayName,omitempty"`
ProfileName string `json:"profileName,omitempty"`
Email string `json:"email,omitempty"`
Password string `json:"password"`
Scope string `json:"scope"`
Locale string `json:"locale"`
LockPassword bool `json:"lockPassword"`
ViewMode ViewMode `json:"viewMode"`
SingleClick bool `json:"singleClick"`
AutoOpen map[string]string `json:"autoOpen"`
RedirectAfterCopyMove bool `json:"redirectAfterCopyMove"`
Perm Permissions `json:"perm"`
Commands []string `json:"commands"`
Sorting files.Sorting `json:"sorting"`
Fs afero.Fs `json:"-" yaml:"-"`
Rules []rules.Rule `json:"rules"`
HideDotfiles bool `json:"hideDotfiles"`
HidePreviews bool `json:"hidePreviews"`
Dicom3D Dicom3DSettings `json:"dicom3D"`
DateFormat bool `json:"dateFormat"`
AceEditorTheme string `json:"aceEditorTheme"`
}
// GetRules implements rules.Provider.
@@ -57,6 +58,7 @@ var checkableFields = []string{
"Sorting",
"Rules",
"Dicom3D",
"AutoOpen",
}
// Clean cleans up a user and verifies if all its fields
@@ -68,6 +70,12 @@ func (u *User) Clean(baseScope string, followExternalSymlinks bool, fields ...st
for _, field := range fields {
switch field {
case "AutoOpen":
var valid bool
u.AutoOpen, valid = normalizeAutoOpen(u.AutoOpen)
if !valid {
return fberrors.ErrInvalidRequestParams
}
case "Dicom3D":
if !u.Dicom3D.Valid() {
return fberrors.ErrInvalidRequestParams