From 586e6ca1e46b420e29872c34613f40a9d4a5b8f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20M=C3=BCller?= Date: Mon, 12 Feb 2018 21:04:39 +0100 Subject: [PATCH] template simplification --- assets/templates/note.html | 11 ++++++++++- assets/templates/stats.html | 10 ---------- render.go | 5 ++++- server.go | 30 ++++++++++++------------------ storage.go | 22 ++++++++++++++-------- test/main.go | 4 ++-- 6 files changed, 42 insertions(+), 40 deletions(-) delete mode 100644 assets/templates/stats.html diff --git a/assets/templates/note.html b/assets/templates/note.html index 82f288f..9d92a37 100644 --- a/assets/templates/note.html +++ b/assets/templates/note.html @@ -9,10 +9,19 @@ + {{if .Fraud}} {{.Ads}} + +
+ + {{else}}
{{.Content}}
+ {{end}} diff --git a/assets/templates/stats.html b/assets/templates/stats.html deleted file mode 100644 index c703d07..0000000 --- a/assets/templates/stats.html +++ /dev/null @@ -1,10 +0,0 @@ -{{define "Stats"}} -

Statistics

- - - {{if not .Edited.IsZero}} - - {{end}} - -
Published{{.Published.Format "Jan 02, 2006 15:04:05 UTC"}}
Edited{{.Edited.Format "Jan 02, 2006 15:04:05 UTC"}}
Views{{.Views}}
-{{end}} diff --git a/render.go b/render.go index a35c80e..7628166 100644 --- a/render.go +++ b/render.go @@ -1,6 +1,7 @@ package main import ( + "encoding/base64" "errors" "html/template" "io/ioutil" @@ -25,7 +26,6 @@ var ( rexpNewLine = regexp.MustCompile("[\n\r]") rexpNonAlphaNum = regexp.MustCompile("[`~!@#$%^&*_|+=?;:'\",.<>{}\\/]") rexpNoScriptIframe = regexp.MustCompile("(<.*?script.*?>.*?<.*?/.*?script.*?>|<.*?iframe.*?>|)") - rexpLink = regexp.MustCompile("(ht|f)tps?://[^\\s]+") errorUnathorised = errors.New("password is wrong") errorBadRequest = errors.New("password is empty") @@ -40,6 +40,9 @@ func (n *Note) prepare() { n.Text = rexpNoScriptIframe.ReplaceAllString(n.Text, "") n.Title = strings.TrimSpace(rexpNonAlphaNum.ReplaceAllString(fstLine[:maxLength], "")) n.Content = mdTmplHTML([]byte(n.Text)) + if n.Fraud() { + n.Encoded = base64.StdEncoding.EncodeToString([]byte(n.Content)) + } } var mdRenderer = markdown.New(markdown.HTML(true)) diff --git a/server.go b/server.go index ed8fef1..1f48bf1 100644 --- a/server.go +++ b/server.go @@ -1,8 +1,8 @@ package main import ( - "bytes" "encoding/json" + "fmt" "html/template" "io" "io/ioutil" @@ -19,8 +19,6 @@ import ( "github.com/labstack/gommon/log" ) -const fraudThreshold = 7 - var TEST_MODE = false type Template struct{ templates *template.Template } @@ -41,14 +39,14 @@ func main() { TEST_MODE = os.Getenv("TEST_MODE") != "" - var ads []byte adsFName := os.Getenv("ADS") + var ads template.HTML if adsFName != "" { - var err error - ads, err = ioutil.ReadFile(adsFName) + data, err := ioutil.ReadFile(adsFName) if err != nil { e.Logger.Errorf("couldn't read file %s: %v", adsFName, err) } + ads = mdTmplHTML(data) } go flushStatsLoop(e.Logger, db) @@ -79,11 +77,8 @@ func main() { return c.String(code, statuses[code]) } defer incViews(n, db) - fraud := n.fraudelent() - if fraud { - n.Ads = mdTmplHTML(ads) - } - c.Logger().Debugf("/%s delivered (fraud: %t)", id, fraud) + n.Ads = ads + c.Logger().Debugf("/%s delivered (fraud: %t)", id, n.Fraud()) return c.Render(code, "Note", n) }) @@ -93,7 +88,7 @@ func main() { var content string if code == http.StatusOK { defer incViews(n, db) - if n.fraudelent() { + if n.Fraud() { code = http.StatusForbidden content = statuses[code] c.Logger().Warnf("/%s/export failed (code: %d)", id, code) @@ -112,12 +107,11 @@ func main() { c.Logger().Errorf("/%s/stats failed (code: %d)", id, code) return c.String(code, statuses[code]) } - n.prepare() - buf := bytes.NewBuffer([]byte{}) - e.Renderer.Render(buf, "Stats", n, c) - n.Content = template.HTML(buf.String()) - c.Logger().Debugf("/%s/stats delivered", id) - return c.Render(code, "Note", n) + stats := fmt.Sprintf("Published: %s\n Views: %d", n.Published, n.Views) + if !n.Edited.IsZero() { + stats = fmt.Sprintf("Published: %s\n Edited: %s\n Views: %d", n.Published, n.Edited, n.Views) + } + return c.String(code, stats) }) e.GET("/:id/edit", func(c echo.Context) error { diff --git a/storage.go b/storage.go index aa938b0..8ae3f54 100644 --- a/storage.go +++ b/storage.go @@ -21,18 +21,24 @@ func init() { rand.Seed(time.Now().UnixNano()) } -const idLength = 5 +const ( + idLength = 5 + fraudThreshold = 7 +) -var rexpNoteID = regexp.MustCompile("[a-z0-9]+") +var ( + rexpNoteID = regexp.MustCompile("[a-z0-9]+") + rexpLink = regexp.MustCompile("(ht|f)tps?://[^\\s]+") +) type Note struct { - ID, Title, Text, Password, DeprecatedPassword string - Published, Edited time.Time - Views int - Content, Ads template.HTML + ID, Title, Text, Password, DeprecatedPassword, Encoded string + Published, Edited time.Time + Views int + Content, Ads template.HTML } -func (n *Note) fraudelent() bool { +func (n *Note) Fraud() bool { res := rexpLink.FindAllString(n.Text, -1) if len(res) < 3 { return false @@ -40,7 +46,7 @@ func (n *Note) fraudelent() bool { stripped := rexpLink.ReplaceAllString(n.Text, "") l1 := len(n.Text) l2 := len(stripped) - return n.Views > 100 && + return n.Views > 30 && int(math.Ceil(100*float64(l1-l2)/float64(l1))) > fraudThreshold } diff --git a/test/main.go b/test/main.go index 9202865..71be9f6 100644 --- a/test/main.go +++ b/test/main.go @@ -135,7 +135,7 @@ func main() { Get(service + "/" + id + "/stats"). Send(). ExpectStatus(200). - ExpectContent("Views4"). + ExpectContent("Views: 4"). ExpectContent("Published") frisby.Create("Test edit page of new note"). @@ -310,7 +310,7 @@ func main() { Get(service + "/" + id + "/stats"). Send(). ExpectStatus(200). - ExpectContent("Views102"). + ExpectContent("Views: 102"). ExpectContent("Published") frisby.Create("Test export of fraudulent note").