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.

55 lines
1.3 KiB

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. "github.com/flosch/pongo2"
  8. "github.com/rakyll/magicmime"
  9. "github.com/zenazn/goji/web"
  10. )
  11. func fileDisplayHandler(c web.C, w http.ResponseWriter, r *http.Request) {
  12. fileName := c.URLParams["name"]
  13. filePath := path.Join(Config.filesDir, fileName)
  14. fileInfo, err := os.Stat(filePath)
  15. if os.IsNotExist(err) {
  16. http.Error(w, http.StatusText(404), 404)
  17. return
  18. }
  19. if err := magicmime.Open(magicmime.MAGIC_MIME_TYPE |
  20. magicmime.MAGIC_SYMLINK |
  21. magicmime.MAGIC_ERROR); err != nil {
  22. http.Error(w, err.Error(), http.StatusInternalServerError)
  23. }
  24. defer magicmime.Close()
  25. mimetype, err := magicmime.TypeByFile(filePath)
  26. if err != nil {
  27. http.Error(w, err.Error(), http.StatusInternalServerError)
  28. }
  29. var tpl *pongo2.Template
  30. if strings.HasPrefix(mimetype, "image/") {
  31. tpl = pongo2.Must(pongo2.FromCache("templates/display/image.html"))
  32. } else if strings.HasPrefix(mimetype, "video/") {
  33. tpl = pongo2.Must(pongo2.FromCache("templates/display/video.html"))
  34. } else {
  35. tpl = pongo2.Must(pongo2.FromCache("templates/display/file.html"))
  36. }
  37. err = tpl.ExecuteWriter(pongo2.Context{
  38. "mime": mimetype,
  39. "filename": fileName,
  40. "size": fileInfo.Size(),
  41. }, w)
  42. if err != nil {
  43. http.Error(w, err.Error(), http.StatusInternalServerError)
  44. }
  45. }