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.

77 lines
1.7 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. "net/http"
  5. "os"
  6. "path"
  7. "strconv"
  8. "strings"
  9. "time"
  10. "bitbucket.org/taruti/mimemagic"
  11. "github.com/dustin/go-humanize"
  12. "github.com/flosch/pongo2"
  13. "github.com/zenazn/goji/web"
  14. )
  15. func fileDisplayHandler(c web.C, w http.ResponseWriter, r *http.Request) {
  16. fileName := c.URLParams["name"]
  17. filePath := path.Join(Config.filesDir, fileName)
  18. fileInfo, err := os.Stat(filePath)
  19. if !fileExistsAndNotExpired(fileName) {
  20. notFoundHandler(c, w, r)
  21. return
  22. }
  23. expiry, _ := metadataGetExpiry(fileName)
  24. var expiryHuman string
  25. if expiry != 0 {
  26. expiryHuman = humanize.RelTime(time.Now(), time.Unix(expiry, 0), "", "")
  27. }
  28. sizeHuman := humanize.Bytes(uint64(fileInfo.Size()))
  29. file, _ := os.Open(filePath)
  30. header := make([]byte, 512)
  31. file.Read(header)
  32. file.Close()
  33. mimetype := mimemagic.Match("", header)
  34. if strings.EqualFold("application/json", r.Header.Get("Accept")) {
  35. js, _ := json.Marshal(map[string]string{
  36. "filename": fileName,
  37. "mimetype": mimetype,
  38. "expiry": strconv.FormatInt(expiry, 10),
  39. "size": strconv.FormatInt(fileInfo.Size(), 10),
  40. })
  41. w.Write(js)
  42. return
  43. }
  44. var tpl *pongo2.Template
  45. if strings.HasPrefix(mimetype, "image/") {
  46. tpl = Templates["display/image.html"]
  47. } else if strings.HasPrefix(mimetype, "video/") {
  48. tpl = Templates["display/video.html"]
  49. } else if strings.HasPrefix(mimetype, "audio/") {
  50. tpl = Templates["display/audio.html"]
  51. } else if mimetype == "application/pdf" {
  52. tpl = Templates["display/pdf.html"]
  53. } else {
  54. tpl = Templates["display/file.html"]
  55. }
  56. err = tpl.ExecuteWriter(pongo2.Context{
  57. "mime": mimetype,
  58. "filename": fileName,
  59. "size": sizeHuman,
  60. "expiry": expiryHuman,
  61. }, w)
  62. if err != nil {
  63. oopsHandler(c, w, r)
  64. }
  65. }