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.

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