Contains the Concourse pipeline definition for building a line-server container
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

148 lines
3.7 KiB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. package main
  2. import (
  3. "encoding/json"
  4. "io/ioutil"
  5. "net/http"
  6. "path/filepath"
  7. "regexp"
  8. "strconv"
  9. "strings"
  10. "time"
  11. "github.com/andreimarcu/linx-server/backends"
  12. "github.com/andreimarcu/linx-server/expiry"
  13. "github.com/dustin/go-humanize"
  14. "github.com/flosch/pongo2"
  15. "github.com/microcosm-cc/bluemonday"
  16. "github.com/russross/blackfriday"
  17. "github.com/zenazn/goji/web"
  18. )
  19. const maxDisplayFileSizeBytes = 1024 * 512
  20. var cliUserAgentRe = regexp.MustCompile("(?i)(lib)?curl|wget")
  21. func fileDisplayHandler(c web.C, w http.ResponseWriter, r *http.Request) {
  22. if !Config.noDirectAgents && cliUserAgentRe.MatchString(r.Header.Get("User-Agent")) {
  23. fileServeHandler(c, w, r)
  24. return
  25. }
  26. fileName := c.URLParams["name"]
  27. _, err := checkFile(fileName)
  28. if err == backends.NotFoundErr {
  29. notFoundHandler(c, w, r)
  30. return
  31. }
  32. metadata, err := storageBackend.Head(fileName)
  33. if err != nil {
  34. oopsHandler(c, w, r, RespAUTO, "Corrupt metadata.")
  35. return
  36. }
  37. var expiryHuman string
  38. if metadata.Expiry != expiry.NeverExpire {
  39. expiryHuman = humanize.RelTime(time.Now(), metadata.Expiry, "", "")
  40. }
  41. sizeHuman := humanize.Bytes(uint64(metadata.Size))
  42. extra := make(map[string]string)
  43. lines := []string{}
  44. extension := strings.TrimPrefix(filepath.Ext(fileName), ".")
  45. if strings.EqualFold("application/json", r.Header.Get("Accept")) {
  46. js, _ := json.Marshal(map[string]string{
  47. "filename": fileName,
  48. "expiry": strconv.FormatInt(metadata.Expiry.Unix(), 10),
  49. "size": strconv.FormatInt(metadata.Size, 10),
  50. "mimetype": metadata.Mimetype,
  51. "sha256sum": metadata.Sha256sum,
  52. })
  53. w.Write(js)
  54. return
  55. }
  56. var tpl *pongo2.Template
  57. if strings.HasPrefix(metadata.Mimetype, "image/") {
  58. tpl = Templates["display/image.html"]
  59. } else if strings.HasPrefix(metadata.Mimetype, "video/") {
  60. tpl = Templates["display/video.html"]
  61. } else if strings.HasPrefix(metadata.Mimetype, "audio/") {
  62. tpl = Templates["display/audio.html"]
  63. } else if metadata.Mimetype == "application/pdf" {
  64. tpl = Templates["display/pdf.html"]
  65. } else if extension == "story" {
  66. metadata, reader, err := storageBackend.Get(fileName)
  67. if err != nil {
  68. oopsHandler(c, w, r, RespHTML, err.Error())
  69. }
  70. if metadata.Size < maxDisplayFileSizeBytes {
  71. bytes, err := ioutil.ReadAll(reader)
  72. if err == nil {
  73. extra["contents"] = string(bytes)
  74. lines = strings.Split(extra["contents"], "\n")
  75. tpl = Templates["display/story.html"]
  76. }
  77. }
  78. } else if extension == "md" {
  79. metadata, reader, err := storageBackend.Get(fileName)
  80. if err != nil {
  81. oopsHandler(c, w, r, RespHTML, err.Error())
  82. }
  83. if metadata.Size < maxDisplayFileSizeBytes {
  84. bytes, err := ioutil.ReadAll(reader)
  85. if err == nil {
  86. unsafe := blackfriday.MarkdownCommon(bytes)
  87. html := bluemonday.UGCPolicy().SanitizeBytes(unsafe)
  88. extra["contents"] = string(html)
  89. tpl = Templates["display/md.html"]
  90. }
  91. }
  92. } else if strings.HasPrefix(metadata.Mimetype, "text/") || supportedBinExtension(extension) {
  93. metadata, reader, err := storageBackend.Get(fileName)
  94. if err != nil {
  95. oopsHandler(c, w, r, RespHTML, err.Error())
  96. }
  97. if metadata.Size < maxDisplayFileSizeBytes {
  98. bytes, err := ioutil.ReadAll(reader)
  99. if err == nil {
  100. extra["extension"] = extension
  101. extra["lang_hl"], extra["lang_ace"] = extensionToHlAndAceLangs(extension)
  102. extra["contents"] = string(bytes)
  103. tpl = Templates["display/bin.html"]
  104. }
  105. }
  106. }
  107. // Catch other files
  108. if tpl == nil {
  109. tpl = Templates["display/file.html"]
  110. }
  111. err = renderTemplate(tpl, pongo2.Context{
  112. "mime": metadata.Mimetype,
  113. "filename": fileName,
  114. "size": sizeHuman,
  115. "expiry": expiryHuman,
  116. "extra": extra,
  117. "lines": lines,
  118. "files": metadata.ArchiveFiles,
  119. }, r, w)
  120. if err != nil {
  121. oopsHandler(c, w, r, RespHTML, "")
  122. }
  123. }