package fbhttp import ( "compress/gzip" "encoding/json" "errors" "fmt" "html/template" "io" "io/fs" "net/http" "net/url" "os" "path" "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": d.server.NoSSO, "AuthMethod": "sso", "SSOIssuer": d.server.SSOIssuer, "SSOClientID": d.server.SSOClientID, "LogoutPage": "/auth/logout", "LoginPage": true, "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(), } b, err := json.Marshal(data) if err != nil { return http.StatusInternalServerError, err } frameSource := "" if d.server.SSOIssuer != "" && !d.server.NoSSO { if issuer, e := url.Parse(d.server.SSOIssuer); e == nil { frameSource = " " + issuer.Scheme + "://" + issuer.Host } } data["FrameSource"] = frameSource 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), "