notice.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // Copyright 2019 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package context
  5. import (
  6. "os"
  7. "path"
  8. "github.com/unknwon/com"
  9. log "unknwon.dev/clog/v2"
  10. "gogs.io/gogs/internal/markup"
  11. "gogs.io/gogs/internal/setting"
  12. "gogs.io/gogs/internal/tool"
  13. )
  14. // renderNoticeBanner checks if a notice banner file exists and loads the message to display
  15. // on all pages.
  16. func (c *Context) renderNoticeBanner() {
  17. fpath := path.Join(setting.CustomPath, "notice", "banner.md")
  18. if !com.IsExist(fpath) {
  19. return
  20. }
  21. f, err := os.Open(fpath)
  22. if err != nil {
  23. log.Error("Failed to open file %q: %v", fpath, err)
  24. return
  25. }
  26. defer f.Close()
  27. fi, err := f.Stat()
  28. if err != nil {
  29. log.Error("Failed to stat file %q: %v", fpath, err)
  30. return
  31. }
  32. // Limit size to prevent very large messages from breaking pages
  33. var maxSize int64 = 1024
  34. if fi.Size() > maxSize { // Refuse to print very long messages
  35. log.Warn("Notice banner file %q size too large [%d > %d]: refusing to render", fpath, fi.Size(), maxSize)
  36. return
  37. }
  38. buf := make([]byte, maxSize)
  39. n, err := f.Read(buf)
  40. if err != nil {
  41. log.Error("Failed to read file %q: %v", fpath, err)
  42. return
  43. }
  44. buf = buf[:n]
  45. if !tool.IsTextFile(buf) {
  46. log.Warn("Notice banner file %q does not appear to be a text file: aborting", fpath)
  47. return
  48. }
  49. c.Data["ServerNotice"] = string(markup.RawMarkdown(buf, ""))
  50. }