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.

116 lines
2.9 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
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. "bitbucket.org/taruti/mimemagic"
  13. "github.com/dustin/go-humanize"
  14. "github.com/flosch/pongo2"
  15. "github.com/microcosm-cc/bluemonday"
  16. "github.com/russross/blackfriday"
  17. "github.com/zenazn/goji/web"
  18. )
  19. const maxDisplayFileSizeBytes = 1024 * 512
  20. func fileDisplayHandler(c web.C, w http.ResponseWriter, r *http.Request) {
  21. fileName := c.URLParams["name"]
  22. filePath := path.Join(Config.filesDir, fileName)
  23. fileInfo, err := os.Stat(filePath)
  24. if !fileExistsAndNotExpired(fileName) {
  25. notFoundHandler(c, w, r)
  26. return
  27. }
  28. expiry, _ := metadataGetExpiry(fileName)
  29. var expiryHuman string
  30. if expiry != neverExpire {
  31. expiryHuman = humanize.RelTime(time.Now(), expiry, "", "")
  32. }
  33. sizeHuman := humanize.Bytes(uint64(fileInfo.Size()))
  34. extra := make(map[string]string)
  35. file, _ := os.Open(filePath)
  36. header := make([]byte, 512)
  37. file.Read(header)
  38. file.Close()
  39. mimetype := mimemagic.Match("", header)
  40. extension := strings.TrimPrefix(filepath.Ext(fileName), ".")
  41. if strings.EqualFold("application/json", r.Header.Get("Accept")) {
  42. js, _ := json.Marshal(map[string]string{
  43. "filename": fileName,
  44. "mimetype": mimetype,
  45. "expiry": strconv.FormatInt(expiry.Unix(), 10),
  46. "size": strconv.FormatInt(fileInfo.Size(), 10),
  47. })
  48. w.Write(js)
  49. return
  50. }
  51. var tpl *pongo2.Template
  52. if strings.HasPrefix(mimetype, "image/") {
  53. tpl = Templates["display/image.html"]
  54. } else if strings.HasPrefix(mimetype, "video/") {
  55. tpl = Templates["display/video.html"]
  56. } else if strings.HasPrefix(mimetype, "audio/") {
  57. tpl = Templates["display/audio.html"]
  58. } else if mimetype == "application/pdf" {
  59. tpl = Templates["display/pdf.html"]
  60. } else if supportedBinExtension(extension) {
  61. if fileInfo.Size() < maxDisplayFileSizeBytes {
  62. bytes, err := ioutil.ReadFile(filePath)
  63. if err != nil {
  64. tpl = Templates["display/file.html"]
  65. } else {
  66. extra["extension"] = extension
  67. extra["lang_hl"], extra["lang_ace"] = extensionToHlAndAceLangs(extension)
  68. extra["contents"] = string(bytes)
  69. tpl = Templates["display/bin.html"]
  70. }
  71. } else {
  72. tpl = Templates["display/file.html"]
  73. }
  74. } else if extension == "md" {
  75. if fileInfo.Size() < maxDisplayFileSizeBytes {
  76. bytes, err := ioutil.ReadFile(filePath)
  77. if err != nil {
  78. tpl = Templates["display/file.html"]
  79. } else {
  80. unsafe := blackfriday.MarkdownCommon(bytes)
  81. html := bluemonday.UGCPolicy().SanitizeBytes(unsafe)
  82. extra["contents"] = string(html)
  83. tpl = Templates["display/md.html"]
  84. }
  85. } else {
  86. tpl = Templates["display/file.html"]
  87. }
  88. } else {
  89. tpl = Templates["display/file.html"]
  90. }
  91. err = tpl.ExecuteWriter(pongo2.Context{
  92. "mime": mimetype,
  93. "filename": fileName,
  94. "size": sizeHuman,
  95. "expiry": expiryHuman,
  96. "extra": extra,
  97. }, w)
  98. if err != nil {
  99. oopsHandler(c, w, r, RespHTML, "")
  100. }
  101. }