notice.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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/filepath"
  8. "github.com/unknwon/com"
  9. log "unknwon.dev/clog/v2"
  10. "gogs.io/gogs/internal/conf"
  11. "gogs.io/gogs/internal/markup"
  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 := filepath.Join(conf.CustomDir(), "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. }