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.

119 lines
2.8 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. file, _ := os.Open(filePath)
  39. defer file.Close()
  40. header := make([]byte, 512)
  41. file.Read(header)
  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. "expiry": strconv.FormatInt(metadata.Expiry.Unix(), 10),
  47. "size": strconv.FormatInt(metadata.Size, 10),
  48. "mimetype": metadata.Mimetype,
  49. "sha256sum": metadata.Sha256sum,
  50. })
  51. w.Write(js)
  52. return
  53. }
  54. var tpl *pongo2.Template
  55. if strings.HasPrefix(metadata.Mimetype, "image/") {
  56. tpl = Templates["display/image.html"]
  57. } else if strings.HasPrefix(metadata.Mimetype, "video/") {
  58. tpl = Templates["display/video.html"]
  59. } else if strings.HasPrefix(metadata.Mimetype, "audio/") {
  60. tpl = Templates["display/audio.html"]
  61. } else if metadata.Mimetype == "application/pdf" {
  62. tpl = Templates["display/pdf.html"]
  63. } else if supportedBinExtension(extension) {
  64. if metadata.Size < maxDisplayFileSizeBytes {
  65. bytes, err := ioutil.ReadFile(filePath)
  66. if err == nil {
  67. extra["extension"] = extension
  68. extra["lang_hl"], extra["lang_ace"] = extensionToHlAndAceLangs(extension)
  69. extra["contents"] = string(bytes)
  70. tpl = Templates["display/bin.html"]
  71. }
  72. }
  73. } else if extension == "md" {
  74. if metadata.Size < maxDisplayFileSizeBytes {
  75. bytes, err := ioutil.ReadFile(filePath)
  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. }
  84. // Catch other files
  85. if tpl == nil {
  86. tpl = Templates["display/file.html"]
  87. }
  88. err = tpl.ExecuteWriter(pongo2.Context{
  89. "mime": metadata.Mimetype,
  90. "filename": fileName,
  91. "size": sizeHuman,
  92. "expiry": expiryHuman,
  93. "extra": extra,
  94. "files": metadata.ArchiveFiles,
  95. }, w)
  96. if err != nil {
  97. oopsHandler(c, w, r, RespHTML, "")
  98. }
  99. }