Procházet zdrojové kódy

pkg/context: Render live notice banner from file (#5750)

* pkg/context: Render live notice banner from file

- Contexter checks if there is a file called 'notice' under the
GOGS_CUSTOM directory and loads it.
- The first line is treated as a header/title and everything else as the
message body.
- Message body is rendered as HTML (tags allowed).
- File size is limited to 1024 bytes.
- File mime type must be text.
- Notice is rendered in head.tmpl for all pages.

* pkg/context: Rename maxlen to maxSize

Rename maxlen to maxSize for the maximum size (in bytes) of the notice
file to render.
Define the variable when needed to avoid instantiating it when the file
doesn't exist.

Co-Authored-By: ᴊ. ᴄʜᴇɴ <u@gogs.io>

* pkg/context: Package name after license header

Co-Authored-By: ᴊ. ᴄʜᴇɴ <u@gogs.io>

* pkg/context: Don't print 'Found notice file'

Becomes too verbose as it prints on every page load when the file
exists.

* pkg/context: Match project conventions

Import order and grouping

Variable names:
    fileloc -> fpath
    fp -> f
    finfo -> fi

* pkg/context: Remove empty line

Co-Authored-By: ᴊ. ᴄʜᴇɴ <u@gogs.io>

* pkg/context: Render notice as markdown

Server notice file should be named 'notice.md'.  The contents of the
file are treated as markdown and rendered as a warning message at the
top of every page.

* Update notice.go


Co-authored-by: ᴊ. ᴄʜᴇɴ <u@gogs.io>
Achilleas Koutsou před 5 roky
rodič
revize
dc13eb6df0
3 změnil soubory, kde provedl 72 přidání a 0 odebrání
  1. 2 0
      pkg/context/context.go
  2. 62 0
      pkg/context/notice.go
  3. 8 0
      templates/base/head.tmpl

+ 2 - 0
pkg/context/context.go

@@ -327,6 +327,8 @@ func Contexter() macaron.Handler {
 		c.Data["ShowFooterBranding"] = setting.ShowFooterBranding
 		c.Data["ShowFooterVersion"] = setting.ShowFooterVersion
 
+		c.Data["ServerNotice"] = readServerNotice()
+
 		ctx.Map(c)
 	}
 }

+ 62 - 0
pkg/context/notice.go

@@ -0,0 +1,62 @@
+// Copyright 2019 The Gogs Authors. All rights reserved.
+// Use of this source code is governed by a MIT-style
+// license that can be found in the LICENSE file.
+
+package context
+
+import (
+	"os"
+	"path"
+
+	"github.com/Unknwon/com"
+	log "gopkg.in/clog.v1"
+
+	"github.com/gogs/gogs/pkg/markup"
+	"github.com/gogs/gogs/pkg/setting"
+	"github.com/gogs/gogs/pkg/tool"
+)
+
+// readServerNotice checks if a notice file exists and loads the message to display
+// on all pages.
+func readServerNotice() string {
+	fpath := path.Join(setting.CustomPath, "notice.md")
+	if !com.IsExist(fpath) {
+		return ""
+	}
+
+	f, err := os.Open(fpath)
+	if err != nil {
+		log.Error(2, "Failed to open notice file %s: %v", fpath, err)
+		return ""
+	}
+	defer f.Close()
+
+	fi, err := f.Stat()
+	if err != nil {
+		log.Error(2, "Failed to stat notice file %s: %v", fpath, err)
+		return ""
+	}
+
+	// Limit size to prevent very large messages from breaking pages
+	var maxSize int64 = 1024
+
+	if fi.Size() > maxSize { // Refuse to print very long messages
+		log.Error(2, "Notice file %s size too large [%d > %d]: refusing to render", fpath, fi.Size(), maxSize)
+		return ""
+	}
+
+	buf := make([]byte, maxSize)
+	n, err := f.Read(buf)
+	if err != nil {
+		log.Error(2, "Failed to read notice file: %v", err)
+		return ""
+	}
+	buf = buf[:n]
+
+	if !tool.IsTextFile(buf) {
+		log.Error(2, "Notice file %s does not appear to be a text file: aborting", fpath)
+		return ""
+	}
+
+	return string(markup.RawMarkdown(buf, ""))
+}

+ 8 - 0
templates/base/head.tmpl

@@ -192,6 +192,14 @@
 				</div><!-- end container -->
 			</div><!-- end bar -->
 		{{end}}
+
+		{{if .ServerNotice}}
+			<div class="ui container grid warning message">
+				<div class="content">
+					{{.ServerNotice | Str2HTML}}
+				</div>
+			</div>
+		{{end}}
 {{/*
 	</div>
 </body>