fix: align settings, modernize notifications and remove branding overrides
This commit is contained in:
1 parent
d3d164910b
commit
9110c8a9d5
55 files changed
+384
-304
No files matched your search
@@ -8,11 +8,9 @@ import (
|
||||
"html/template"
|
||||
"io"
|
||||
"io/fs"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
@@ -39,7 +37,6 @@ func handleWithStaticData(w http.ResponseWriter, _ *http.Request, d *data, fSys
|
||||
"AuthMethod": "sso",
|
||||
"LogoutPage": "/auth/logout",
|
||||
"LoginPage": true,
|
||||
"CSS": false,
|
||||
"ReCaptcha": false,
|
||||
"Theme": d.settings.Branding.Theme,
|
||||
"EnableThumbs": d.server.EnableThumbnails,
|
||||
@@ -50,19 +47,6 @@ func handleWithStaticData(w http.ResponseWriter, _ *http.Request, d *data, fSys
|
||||
"Nonce": randomSecret(),
|
||||
}
|
||||
|
||||
if d.settings.Branding.Files != "" {
|
||||
fPath := filepath.Join(d.settings.Branding.Files, "custom.css")
|
||||
_, err := os.Stat(fPath)
|
||||
|
||||
if err != nil && !os.IsNotExist(err) {
|
||||
log.Printf("couldn't load custom styles: %v", err)
|
||||
}
|
||||
|
||||
if err == nil {
|
||||
data["CSS"] = true
|
||||
}
|
||||
}
|
||||
|
||||
b, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
return http.StatusInternalServerError, err
|
||||
@@ -126,22 +110,6 @@ func getStaticHandlers(store *storage.Storage, server *settings.Server, assetsFs
|
||||
w.Header().Add("Vary", "Accept-Encoding")
|
||||
w.Header().Set("X-Content-Type-Options", "nosniff")
|
||||
|
||||
if d.settings.Branding.Files != "" {
|
||||
if strings.HasPrefix(r.URL.Path, "img/") {
|
||||
fPath := filepath.Join(d.settings.Branding.Files, r.URL.Path)
|
||||
_, err := os.Stat(fPath)
|
||||
if err != nil && !os.IsNotExist(err) {
|
||||
log.Printf("could not load branding file override: %v", err)
|
||||
} else if err == nil {
|
||||
http.ServeFile(w, r, fPath)
|
||||
return 0, nil
|
||||
}
|
||||
} else if r.URL.Path == "custom.css" && d.settings.Branding.Files != "" {
|
||||
http.ServeFile(w, r, filepath.Join(d.settings.Branding.Files, "custom.css"))
|
||||
return 0, nil
|
||||
}
|
||||
}
|
||||
|
||||
if !strings.HasSuffix(r.URL.Path, ".js") {
|
||||
http.FileServer(http.FS(assetsFs)).ServeHTTP(w, r)
|
||||
return 0, nil
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
package fbhttp
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"testing/fstest"
|
||||
|
||||
"github.com/filebrowser/filebrowser/v2/settings"
|
||||
"github.com/filebrowser/filebrowser/v2/users"
|
||||
)
|
||||
|
||||
func TestLegacyBrandingDirectoryCannotOverrideAssets(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
if err := os.Mkdir(filepath.Join(root, "img"), 0700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for name, content := range map[string]string{"custom.css": "legacy css", "img/logo.svg": "legacy logo"} {
|
||||
if err := os.WriteFile(filepath.Join(root, name), []byte(content), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
raw, err := json.Marshal(map[string]any{"branding": map[string]any{"name": "Preserved", "files": root}})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var legacy settings.Settings
|
||||
if err := json.Unmarshal(raw, &legacy); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
stored, err := json.Marshal(legacy.Branding)
|
||||
if err != nil || bytes.Contains(stored, []byte(`"files"`)) || legacy.Branding.Name != "Preserved" {
|
||||
t.Fatal("legacy branding setting was not discarded")
|
||||
}
|
||||
st := scopedUserStorage(t, root, users.Permissions{}, []byte("key"))
|
||||
legacy.Key = []byte("key")
|
||||
if err := st.Settings.Save(&legacy); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, static := getStaticHandlers(st, &settings.Server{}, fstest.MapFS{"img/logo.svg": &fstest.MapFile{Data: []byte("embedded logo")}})
|
||||
for _, test := range []struct {
|
||||
path string
|
||||
status int
|
||||
body string
|
||||
}{{"/custom.css", 404, ""}, {"/img/logo.svg", 200, "embedded logo"}} {
|
||||
w := httptest.NewRecorder()
|
||||
static.ServeHTTP(w, httptest.NewRequest("GET", test.path, nil))
|
||||
if w.Code != test.status || (test.body != "" && w.Body.String() != test.body) {
|
||||
t.Fatalf("%s: %d %s", test.path, w.Code, w.Body.String())
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user