context_gin.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package context
  2. import (
  3. "os"
  4. "path"
  5. "strings"
  6. "github.com/G-Node/gogs/internal/setting"
  7. "github.com/G-Node/gogs/internal/tool"
  8. "github.com/unknwon/com"
  9. log "gopkg.in/clog.v1"
  10. )
  11. // readNotice checks if a notice file exists and loads the message to display
  12. // on all pages.
  13. func readNotice(c *Context) {
  14. fileloc := path.Join(setting.CustomPath, "notice")
  15. var maxlen int64 = 1024
  16. if !com.IsExist(fileloc) {
  17. return
  18. }
  19. log.Trace("Found notice file")
  20. fp, err := os.Open(fileloc)
  21. if err != nil {
  22. log.Error(2, "Failed to open notice file %s: %v", fileloc, err)
  23. return
  24. }
  25. defer fp.Close()
  26. finfo, err := fp.Stat()
  27. if err != nil {
  28. log.Error(2, "Failed to stat notice file %s: %v", fileloc, err)
  29. return
  30. }
  31. if finfo.Size() > maxlen { // Refuse to print very long messages
  32. log.Error(2, "Notice file %s size too large [%d > %d]: refusing to render", fileloc, finfo.Size(), maxlen)
  33. return
  34. }
  35. buf := make([]byte, maxlen)
  36. n, err := fp.Read(buf)
  37. if err != nil {
  38. log.Error(2, "Failed to read notice file: %v", err)
  39. return
  40. }
  41. buf = buf[:n]
  42. if !tool.IsTextFile(buf) {
  43. log.Error(2, "Notice file %s does not appear to be a text file: aborting", fileloc)
  44. return
  45. }
  46. noticetext := strings.SplitN(string(buf), "\n", 2)
  47. c.Data["HasNotice"] = true
  48. c.Data["NoticeTitle"] = noticetext[0]
  49. c.Data["NoticeMessage"] = noticetext[1]
  50. }