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.

54 lines
1.2 KiB

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. absPath := path.Join(Config.filesDir, filename)
  14. fileInfo, err := os.Stat(absPath)
  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(absPath)
  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 {
  33. tpl = pongo2.Must(pongo2.FromCache("templates/display/file.html"))
  34. }
  35. err = tpl.ExecuteWriter(pongo2.Context{
  36. "mime": mimetype,
  37. "sitename": Config.siteName,
  38. "filename": filename,
  39. "size": fileInfo.Size(),
  40. }, w)
  41. if err != nil {
  42. http.Error(w, err.Error(), http.StatusInternalServerError)
  43. }
  44. }