diff --git a/web/static.go b/web/static.go index 5e856e267e..3b9943af75 100644 --- a/web/static.go +++ b/web/static.go @@ -4,7 +4,11 @@ package web import ( + "bytes" + "fmt" + "html" "net/http" + "os" "path" "path/filepath" "strings" @@ -73,7 +77,21 @@ func root(c *Context, w http.ResponseWriter, r *http.Request) { w.Header().Set("Cache-Control", "no-cache, max-age=31556926, public") staticDir, _ := fileutils.FindDir(model.ClientDir) - http.ServeFile(w, r, filepath.Join(staticDir, "root.html")) + contents, err := os.ReadFile(filepath.Join(staticDir, "root.html")) + if err != nil { + http.Error(w, err.Error(), http.StatusNotFound) + return + } + + titleTemplate := "%s" + originalHTML := fmt.Sprintf(titleTemplate, html.EscapeString(model.TeamSettingsDefaultSiteName)) + modifiedHTML := getOpenGraphMetaTags(c) + if originalHTML != modifiedHTML { + contents = bytes.ReplaceAll(contents, []byte(originalHTML), []byte(modifiedHTML)) + } + + w.Header().Set("Content-Type", "text/html") + w.Write(contents) } func staticFilesHandler(handler http.Handler) http.Handler { @@ -130,3 +148,27 @@ func unsupportedBrowserScriptHandler(w http.ResponseWriter, r *http.Request) { templatesDir, _ := templates.GetTemplateDirectory() http.ServeFile(w, r, filepath.Join(templatesDir, "unsupported_browser.js")) } + +func getOpenGraphMetaTags(c *Context) string { + siteName := model.TeamSettingsDefaultSiteName + customSiteName := c.App.Srv().Config().TeamSettings.SiteName + if customSiteName != nil && *customSiteName != "" { + siteName = *customSiteName + } + + siteDescription := model.TeamSettingsDefaultCustomDescriptionText + customSiteDescription := c.App.Srv().Config().TeamSettings.CustomDescriptionText + if customSiteDescription != nil && *customSiteDescription != "" { + siteDescription = *customSiteDescription + } + + titleTemplate := "%s" + titleHTML := fmt.Sprintf(titleTemplate, html.EscapeString(siteName)) + descriptionHTML := "" + if siteDescription != "" { + descriptionTemplate := "" + descriptionHTML = fmt.Sprintf(descriptionTemplate, html.EscapeString(siteDescription)) + } + + return titleHTML + descriptionHTML +}