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.

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