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.

133 lines
3.5 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. "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. if metadata.Size < maxDisplayFileSizeBytes {
  66. bytes, err := fileBackend.Get(fileName)
  67. if err == nil {
  68. extra["contents"] = string(bytes)
  69. lines = strings.Split(extra["contents"], "\n")
  70. tpl = Templates["display/story.html"]
  71. }
  72. }
  73. } else if extension == "md" {
  74. if metadata.Size < maxDisplayFileSizeBytes {
  75. bytes, err := fileBackend.Get(fileName)
  76. if err == nil {
  77. unsafe := blackfriday.MarkdownCommon(bytes)
  78. html := bluemonday.UGCPolicy().SanitizeBytes(unsafe)
  79. extra["contents"] = string(html)
  80. tpl = Templates["display/md.html"]
  81. }
  82. }
  83. } else if strings.HasPrefix(metadata.Mimetype, "text/") || supportedBinExtension(extension) {
  84. if metadata.Size < maxDisplayFileSizeBytes {
  85. bytes, err := fileBackend.Get(fileName)
  86. if err == nil {
  87. extra["extension"] = extension
  88. extra["lang_hl"], extra["lang_ace"] = extensionToHlAndAceLangs(extension)
  89. extra["contents"] = string(bytes)
  90. tpl = Templates["display/bin.html"]
  91. }
  92. }
  93. }
  94. // Catch other files
  95. if tpl == nil {
  96. tpl = Templates["display/file.html"]
  97. }
  98. err = renderTemplate(tpl, pongo2.Context{
  99. "mime": metadata.Mimetype,
  100. "filename": fileName,
  101. "size": sizeHuman,
  102. "expiry": expiryHuman,
  103. "expirylist": listExpirationTimes(),
  104. "extra": extra,
  105. "lines": lines,
  106. "files": metadata.ArchiveFiles,
  107. }, r, w)
  108. if err != nil {
  109. oopsHandler(c, w, r, RespHTML, "")
  110. }
  111. }