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.

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