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.

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