package fbhttp import ( "compress/gzip" "encoding/json" "errors" "fmt" "html/template" "io" "io/fs" "log" "net/http" "os" "path" "path/filepath" "strings" "sync" "github.com/filebrowser/filebrowser/v2/settings" "github.com/filebrowser/filebrowser/v2/storage" "github.com/filebrowser/filebrowser/v2/version" ) var indexTemplates sync.Map func handleWithStaticData(w http.ResponseWriter, _ *http.Request, d *data, fSys fs.FS, file, contentType string) (int, error) { w.Header().Set("Content-Type", contentType) data := map[string]interface{}{ "Name": d.settings.Branding.Name, "DisableExternal": d.settings.Branding.DisableExternal, "DisableUsedPercentage": d.settings.Branding.DisableUsedPercentage, "Color": d.settings.Branding.Color, "BaseURL": d.server.BaseURL, "Version": version.Version, "StaticURL": path.Join(d.server.BaseURL, "/static"), "Signup": false, "NoAuth": false, "AuthMethod": "sso", "LogoutPage": "/auth/logout", "LoginPage": true, "CSS": false, "ReCaptcha": false, "Theme": d.settings.Branding.Theme, "EnableThumbs": d.server.EnableThumbnails, "ResizePreview": d.server.ResizePreview, "EnableExec": false, "TusSettings": d.settings.Tus, "HideLoginButton": d.settings.HideLoginButton, "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 } data["Json"] = template.JS(strings.ReplaceAll(string(b), `'`, `\'`)) fileContents, err := fs.ReadFile(fSys, file) if err != nil { if errors.Is(err, os.ErrNotExist) { return http.StatusNotFound, err } return http.StatusInternalServerError, err } // Build templates once. A per-response nonce permits the bootstrap without // enabling arbitrary inline scripts; the configuration JSON stays escaped. contents := strings.ReplaceAll(string(fileContents), "