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.

130 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
  1. package main
  2. import (
  3. "encoding/json"
  4. "io/ioutil"
  5. "net/http"
  6. "os"
  7. "path"
  8. "path/filepath"
  9. "strconv"
  10. "strings"
  11. "time"
  12. "github.com/dustin/go-humanize"
  13. "github.com/flosch/pongo2"
  14. "github.com/microcosm-cc/bluemonday"
  15. "github.com/russross/blackfriday"
  16. "github.com/zenazn/goji/web"
  17. )
  18. const maxDisplayFileSizeBytes = 1024 * 512
  19. func fileDisplayHandler(c web.C, w http.ResponseWriter, r *http.Request) {
  20. fileName := c.URLParams["name"]
  21. filePath := path.Join(Config.filesDir, fileName)
  22. err := checkFile(fileName)
  23. if err == NotFoundErr {
  24. notFoundHandler(c, w, r)
  25. return
  26. }
  27. metadata, err := metadataRead(fileName)
  28. if err != nil {
  29. oopsHandler(c, w, r, RespAUTO, "Corrupt metadata.")
  30. return
  31. }
  32. var expiryHuman string
  33. if metadata.Expiry != neverExpire {
  34. expiryHuman = humanize.RelTime(time.Now(), metadata.Expiry, "", "")
  35. }
  36. sizeHuman := humanize.Bytes(uint64(metadata.Size))
  37. extra := make(map[string]string)
  38. lines := []string{}
  39. file, _ := os.Open(filePath)
  40. defer file.Close()
  41. header := make([]byte, 512)
  42. file.Read(header)
  43. extension := strings.TrimPrefix(filepath.Ext(fileName), ".")
  44. if strings.EqualFold("application/json", r.Header.Get("Accept")) {
  45. js, _ := json.Marshal(map[string]string{
  46. "filename": 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 supportedBinExtension(extension) {
  65. if metadata.Size < maxDisplayFileSizeBytes {
  66. bytes, err := ioutil.ReadFile(filePath)
  67. if err == nil {
  68. extra["extension"] = extension
  69. extra["lang_hl"], extra["lang_ace"] = extensionToHlAndAceLangs(extension)
  70. extra["contents"] = string(bytes)
  71. tpl = Templates["display/bin.html"]
  72. }
  73. }
  74. } else if extension == "story" {
  75. if metadata.Size < maxDisplayFileSizeBytes {
  76. bytes, err := ioutil.ReadFile(filePath)
  77. if err == nil {
  78. extra["contents"] = string(bytes)
  79. lines = strings.Split(extra["contents"], "\n")
  80. tpl = Templates["display/story.html"]
  81. }
  82. }
  83. } else if extension == "md" {
  84. if metadata.Size < maxDisplayFileSizeBytes {
  85. bytes, err := ioutil.ReadFile(filePath)
  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. }
  94. // Catch other files
  95. if tpl == nil {
  96. tpl = Templates["display/file.html"]
  97. }
  98. err = tpl.ExecuteWriter(pongo2.Context{
  99. "mime": metadata.Mimetype,
  100. "filename": fileName,
  101. "size": sizeHuman,
  102. "expiry": expiryHuman,
  103. "extra": extra,
  104. "lines": lines,
  105. "files": metadata.ArchiveFiles,
  106. }, w)
  107. if err != nil {
  108. oopsHandler(c, w, r, RespHTML, "")
  109. }
  110. }