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.

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