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.

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