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.

147 lines
3.9 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")) && !strings.EqualFold("application/json", r.Header.Get("Accept")) {
  23. fileServeHandler(c, w, r)
  24. return
  25. }
  26. fileName := c.URLParams["name"]
  27. metadata, err := checkFile(fileName)
  28. if err == backends.NotFoundErr {
  29. notFoundHandler(c, w, r)
  30. return
  31. } else 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. "direct_url": getSiteURL(r) + Config.selifPath + fileName,
  47. "expiry": strconv.FormatInt(metadata.Expiry.Unix(), 10),
  48. "size": strconv.FormatInt(metadata.Size, 10),
  49. "mimetype": metadata.Mimetype,
  50. "sha256sum": metadata.Sha256sum,
  51. })
  52. w.Write(js)
  53. return
  54. }
  55. var tpl *pongo2.Template
  56. if strings.HasPrefix(metadata.Mimetype, "image/") {
  57. tpl = Templates["display/image.html"]
  58. } else if strings.HasPrefix(metadata.Mimetype, "video/") {
  59. tpl = Templates["display/video.html"]
  60. } else if strings.HasPrefix(metadata.Mimetype, "audio/") {
  61. tpl = Templates["display/audio.html"]
  62. } else if metadata.Mimetype == "application/pdf" {
  63. tpl = Templates["display/pdf.html"]
  64. } else if extension == "story" {
  65. metadata, reader, err := storageBackend.Get(fileName)
  66. if err != nil {
  67. oopsHandler(c, w, r, RespHTML, err.Error())
  68. }
  69. if metadata.Size < maxDisplayFileSizeBytes {
  70. bytes, err := ioutil.ReadAll(reader)
  71. if err == nil {
  72. extra["contents"] = string(bytes)
  73. lines = strings.Split(extra["contents"], "\n")
  74. tpl = Templates["display/story.html"]
  75. }
  76. }
  77. } else if extension == "md" {
  78. metadata, reader, err := storageBackend.Get(fileName)
  79. if err != nil {
  80. oopsHandler(c, w, r, RespHTML, err.Error())
  81. }
  82. if metadata.Size < maxDisplayFileSizeBytes {
  83. bytes, err := ioutil.ReadAll(reader)
  84. if err == nil {
  85. unsafe := blackfriday.MarkdownCommon(bytes)
  86. html := bluemonday.UGCPolicy().SanitizeBytes(unsafe)
  87. extra["contents"] = string(html)
  88. tpl = Templates["display/md.html"]
  89. }
  90. }
  91. } else if strings.HasPrefix(metadata.Mimetype, "text/") || supportedBinExtension(extension) {
  92. metadata, reader, err := storageBackend.Get(fileName)
  93. if err != nil {
  94. oopsHandler(c, w, r, RespHTML, err.Error())
  95. }
  96. if metadata.Size < maxDisplayFileSizeBytes {
  97. bytes, err := ioutil.ReadAll(reader)
  98. if err == nil {
  99. extra["extension"] = extension
  100. extra["lang_hl"], extra["lang_ace"] = extensionToHlAndAceLangs(extension)
  101. extra["contents"] = string(bytes)
  102. tpl = Templates["display/bin.html"]
  103. }
  104. }
  105. }
  106. // Catch other files
  107. if tpl == nil {
  108. tpl = Templates["display/file.html"]
  109. }
  110. err = renderTemplate(tpl, pongo2.Context{
  111. "mime": metadata.Mimetype,
  112. "filename": fileName,
  113. "size": sizeHuman,
  114. "expiry": expiryHuman,
  115. "expirylist": listExpirationTimes(),
  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. }