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.

65 lines
1.2 KiB

  1. package main
  2. import (
  3. "net/http"
  4. "os"
  5. "path"
  6. "strings"
  7. "github.com/zenazn/goji/web"
  8. )
  9. func fileServeHandler(c web.C, w http.ResponseWriter, r *http.Request) {
  10. fileName := c.URLParams["name"]
  11. filePath := path.Join(Config.filesDir, fileName)
  12. if !fileExistsAndNotExpired(fileName) {
  13. notFoundHandler(c, w, r)
  14. return
  15. }
  16. if !Config.allowHotlink {
  17. referer := r.Header.Get("Referer")
  18. if referer != "" && !strings.HasPrefix(referer, Config.siteURL) {
  19. w.WriteHeader(403)
  20. return
  21. }
  22. }
  23. http.ServeFile(w, r, filePath)
  24. }
  25. func staticHandler(c web.C, w http.ResponseWriter, r *http.Request) {
  26. path := r.URL.Path
  27. if path[len(path)-1:] == "/" {
  28. notFoundHandler(c, w, r)
  29. return
  30. } else {
  31. filePath := strings.TrimPrefix(path, "/static/")
  32. file, err := staticBox.Open(filePath)
  33. if err != nil {
  34. oopsHandler(c, w, r)
  35. return
  36. }
  37. http.ServeContent(w, r, filePath, timeStarted, file)
  38. return
  39. }
  40. }
  41. func fileExistsAndNotExpired(filename string) bool {
  42. filePath := path.Join(Config.filesDir, filename)
  43. _, err := os.Stat(filePath)
  44. if err != nil {
  45. return false
  46. }
  47. if isFileExpired(filename) {
  48. os.Remove(path.Join(Config.filesDir, filename))
  49. os.Remove(path.Join(Config.metaDir, filename))
  50. return false
  51. }
  52. return true
  53. }