2017-04-12 08:27:57 -04:00
|
|
|
// Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
|
2016-02-08 07:26:10 -05:00
|
|
|
// See License.txt for license information.
|
|
|
|
|
|
|
|
|
|
package utils
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
2018-03-21 14:27:14 -04:00
|
|
|
"errors"
|
2018-04-27 12:49:45 -07:00
|
|
|
"fmt"
|
2016-02-08 07:26:10 -05:00
|
|
|
"html/template"
|
2017-11-20 11:57:45 -06:00
|
|
|
"io"
|
2018-03-21 14:27:14 -04:00
|
|
|
"path/filepath"
|
2017-09-14 02:29:30 -04:00
|
|
|
"reflect"
|
2018-07-21 01:52:20 +09:00
|
|
|
"strings"
|
2017-11-20 11:57:45 -06:00
|
|
|
"sync/atomic"
|
2016-02-08 07:26:10 -05:00
|
|
|
|
2017-03-24 23:31:34 -07:00
|
|
|
"github.com/fsnotify/fsnotify"
|
2018-04-27 12:49:45 -07:00
|
|
|
"github.com/mattermost/mattermost-server/mlog"
|
2016-04-28 06:53:30 -07:00
|
|
|
"github.com/nicksnyder/go-i18n/i18n"
|
2016-02-08 07:26:10 -05:00
|
|
|
)
|
|
|
|
|
|
2017-11-20 11:57:45 -06:00
|
|
|
type HTMLTemplateWatcher struct {
|
|
|
|
|
templates atomic.Value
|
|
|
|
|
stop chan struct{}
|
|
|
|
|
stopped chan struct{}
|
2016-02-08 07:26:10 -05:00
|
|
|
}
|
|
|
|
|
|
2017-11-20 11:57:45 -06:00
|
|
|
func NewHTMLTemplateWatcher(directory string) (*HTMLTemplateWatcher, error) {
|
|
|
|
|
templatesDir, _ := FindDir(directory)
|
2018-04-27 12:49:45 -07:00
|
|
|
mlog.Debug(fmt.Sprintf("Parsing server templates at %v", templatesDir))
|
2016-03-14 16:07:58 -07:00
|
|
|
|
2017-11-20 11:57:45 -06:00
|
|
|
ret := &HTMLTemplateWatcher{
|
|
|
|
|
stop: make(chan struct{}),
|
|
|
|
|
stopped: make(chan struct{}),
|
|
|
|
|
}
|
2016-03-14 16:07:58 -07:00
|
|
|
|
2017-11-20 11:57:45 -06:00
|
|
|
watcher, err := fsnotify.NewWatcher()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
2016-03-14 16:07:58 -07:00
|
|
|
}
|
|
|
|
|
|
2017-11-20 11:57:45 -06:00
|
|
|
if err = watcher.Add(templatesDir); err != nil {
|
|
|
|
|
return nil, err
|
2016-02-08 07:26:10 -05:00
|
|
|
}
|
|
|
|
|
|
2018-03-21 14:27:14 -04:00
|
|
|
if htmlTemplates, err := template.ParseGlob(filepath.Join(templatesDir, "*.html")); err != nil {
|
2017-11-20 11:57:45 -06:00
|
|
|
return nil, err
|
|
|
|
|
} else {
|
|
|
|
|
ret.templates.Store(htmlTemplates)
|
2016-02-08 07:26:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
go func() {
|
2017-11-20 11:57:45 -06:00
|
|
|
defer close(ret.stopped)
|
|
|
|
|
defer watcher.Close()
|
|
|
|
|
|
2016-02-08 07:26:10 -05:00
|
|
|
for {
|
|
|
|
|
select {
|
2017-11-20 11:57:45 -06:00
|
|
|
case <-ret.stop:
|
|
|
|
|
return
|
2016-02-08 07:26:10 -05:00
|
|
|
case event := <-watcher.Events:
|
|
|
|
|
if event.Op&fsnotify.Write == fsnotify.Write {
|
2018-04-27 12:49:45 -07:00
|
|
|
mlog.Info(fmt.Sprintf("Re-parsing templates because of modified file %v", event.Name))
|
2018-03-21 14:27:14 -04:00
|
|
|
if htmlTemplates, err := template.ParseGlob(filepath.Join(templatesDir, "*.html")); err != nil {
|
2018-04-27 12:49:45 -07:00
|
|
|
mlog.Error(fmt.Sprintf("Failed to parse templates %v", err))
|
2017-11-20 11:57:45 -06:00
|
|
|
} else {
|
|
|
|
|
ret.templates.Store(htmlTemplates)
|
2016-02-08 07:26:10 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
case err := <-watcher.Errors:
|
2018-04-27 12:49:45 -07:00
|
|
|
mlog.Error(fmt.Sprintf("Failed in directory watcher %s", err))
|
2016-02-08 07:26:10 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
|
2017-11-20 11:57:45 -06:00
|
|
|
return ret, nil
|
2016-02-08 07:26:10 -05:00
|
|
|
}
|
|
|
|
|
|
2017-11-20 11:57:45 -06:00
|
|
|
func (w *HTMLTemplateWatcher) Templates() *template.Template {
|
|
|
|
|
return w.templates.Load().(*template.Template)
|
2016-02-08 07:26:10 -05:00
|
|
|
}
|
|
|
|
|
|
2017-11-20 11:57:45 -06:00
|
|
|
func (w *HTMLTemplateWatcher) Close() {
|
|
|
|
|
close(w.stop)
|
|
|
|
|
<-w.stopped
|
|
|
|
|
}
|
2016-04-28 06:53:30 -07:00
|
|
|
|
2017-11-20 11:57:45 -06:00
|
|
|
type HTMLTemplate struct {
|
|
|
|
|
Templates *template.Template
|
|
|
|
|
TemplateName string
|
|
|
|
|
Props map[string]interface{}
|
|
|
|
|
Html map[string]template.HTML
|
|
|
|
|
}
|
2016-06-16 05:44:02 -07:00
|
|
|
|
2017-11-20 11:57:45 -06:00
|
|
|
func NewHTMLTemplate(templates *template.Template, templateName string) *HTMLTemplate {
|
|
|
|
|
return &HTMLTemplate{
|
|
|
|
|
Templates: templates,
|
|
|
|
|
TemplateName: templateName,
|
|
|
|
|
Props: make(map[string]interface{}),
|
|
|
|
|
Html: make(map[string]template.HTML),
|
2016-06-16 05:44:02 -07:00
|
|
|
}
|
2016-02-08 07:26:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (t *HTMLTemplate) Render() string {
|
|
|
|
|
var text bytes.Buffer
|
2017-11-20 11:57:45 -06:00
|
|
|
t.RenderToWriter(&text)
|
2016-02-08 07:26:10 -05:00
|
|
|
return text.String()
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-20 11:57:45 -06:00
|
|
|
func (t *HTMLTemplate) RenderToWriter(w io.Writer) error {
|
2018-03-21 14:27:14 -04:00
|
|
|
if t.Templates == nil {
|
|
|
|
|
return errors.New("no html templates")
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-20 11:57:45 -06:00
|
|
|
if err := t.Templates.ExecuteTemplate(w, t.TemplateName, t); err != nil {
|
2018-04-27 12:49:45 -07:00
|
|
|
mlog.Error(fmt.Sprintf("Error rendering template %v err=%v", t.TemplateName, err))
|
2016-02-08 07:26:10 -05:00
|
|
|
return err
|
|
|
|
|
}
|
2017-09-05 17:39:45 -04:00
|
|
|
|
2016-02-08 07:26:10 -05:00
|
|
|
return nil
|
|
|
|
|
}
|
2017-09-05 17:39:45 -04:00
|
|
|
|
|
|
|
|
func TranslateAsHtml(t i18n.TranslateFunc, translationID string, args map[string]interface{}) template.HTML {
|
2018-07-21 01:52:20 +09:00
|
|
|
message := t(translationID, escapeForHtml(args))
|
|
|
|
|
message = strings.Replace(message, "[[", "<strong>", -1)
|
|
|
|
|
message = strings.Replace(message, "]]", "</strong>", -1)
|
|
|
|
|
return template.HTML(message)
|
2017-09-05 17:39:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func escapeForHtml(arg interface{}) interface{} {
|
|
|
|
|
switch typedArg := arg.(type) {
|
|
|
|
|
case string:
|
|
|
|
|
return template.HTMLEscapeString(typedArg)
|
2017-09-14 02:29:30 -04:00
|
|
|
case *string:
|
|
|
|
|
return template.HTMLEscapeString(*typedArg)
|
2017-09-05 17:39:45 -04:00
|
|
|
case map[string]interface{}:
|
|
|
|
|
safeArg := make(map[string]interface{}, len(typedArg))
|
|
|
|
|
for key, value := range typedArg {
|
|
|
|
|
safeArg[key] = escapeForHtml(value)
|
|
|
|
|
}
|
|
|
|
|
return safeArg
|
|
|
|
|
default:
|
2018-04-27 12:49:45 -07:00
|
|
|
mlog.Warn(fmt.Sprintf("Unable to escape value for HTML template %v of type %v", arg, reflect.ValueOf(arg).Type()))
|
2017-09-05 17:39:45 -04:00
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
}
|