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.

128 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
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. file, _ := fileBackend.Open(fileName)
  36. defer file.Close()
  37. header := make([]byte, 512)
  38. file.Read(header)
  39. extension := strings.TrimPrefix(filepath.Ext(fileName), ".")
  40. if strings.EqualFold("application/json", r.Header.Get("Accept")) {
  41. js, _ := json.Marshal(map[string]string{
  42. "filename": fileName,
  43. "expiry": strconv.FormatInt(metadata.Expiry.Unix(), 10),
  44. "size": strconv.FormatInt(metadata.Size, 10),
  45. "mimetype": metadata.Mimetype,
  46. "sha256sum": metadata.Sha256sum,
  47. })
  48. w.Write(js)
  49. return
  50. }
  51. var tpl *pongo2.Template
  52. if strings.HasPrefix(metadata.Mimetype, "image/") {
  53. tpl = Templates["display/image.html"]
  54. } else if strings.HasPrefix(metadata.Mimetype, "video/") {
  55. tpl = Templates["display/video.html"]
  56. } else if strings.HasPrefix(metadata.Mimetype, "audio/") {
  57. tpl = Templates["display/audio.html"]
  58. } else if metadata.Mimetype == "application/pdf" {
  59. tpl = Templates["display/pdf.html"]
  60. } else if extension == "story" {
  61. if metadata.Size < maxDisplayFileSizeBytes {
  62. bytes, err := fileBackend.Get(fileName)
  63. if err == nil {
  64. extra["contents"] = string(bytes)
  65. lines = strings.Split(extra["contents"], "\n")
  66. tpl = Templates["display/story.html"]
  67. }
  68. }
  69. } else if extension == "md" {
  70. if metadata.Size < maxDisplayFileSizeBytes {
  71. bytes, err := fileBackend.Get(fileName)
  72. if err == nil {
  73. unsafe := blackfriday.MarkdownCommon(bytes)
  74. html := bluemonday.UGCPolicy().SanitizeBytes(unsafe)
  75. extra["contents"] = string(html)
  76. tpl = Templates["display/md.html"]
  77. }
  78. }
  79. } else if strings.HasPrefix(metadata.Mimetype, "text/") || supportedBinExtension(extension) {
  80. if metadata.Size < maxDisplayFileSizeBytes {
  81. bytes, err := fileBackend.Get(fileName)
  82. if err == nil {
  83. extra["extension"] = extension
  84. extra["lang_hl"], extra["lang_ace"] = extensionToHlAndAceLangs(extension)
  85. extra["contents"] = string(bytes)
  86. tpl = Templates["display/bin.html"]
  87. }
  88. }
  89. }
  90. // Catch other files
  91. if tpl == nil {
  92. tpl = Templates["display/file.html"]
  93. }
  94. err = renderTemplate(tpl, pongo2.Context{
  95. "mime": metadata.Mimetype,
  96. "filename": fileName,
  97. "size": sizeHuman,
  98. "expiry": expiryHuman,
  99. "extra": extra,
  100. "lines": lines,
  101. "files": metadata.ArchiveFiles,
  102. }, r, w)
  103. if err != nil {
  104. oopsHandler(c, w, r, RespHTML, "")
  105. }
  106. }