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.

67 lines
1.3 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. w.Header().Set("Etag", timeStartedStr)
  38. w.Header().Set("Cache-Control", "max-age=86400")
  39. http.ServeContent(w, r, filePath, timeStarted, file)
  40. return
  41. }
  42. }
  43. func fileExistsAndNotExpired(filename string) bool {
  44. filePath := path.Join(Config.filesDir, filename)
  45. _, err := os.Stat(filePath)
  46. if err != nil {
  47. return false
  48. }
  49. if isFileExpired(filename) {
  50. os.Remove(path.Join(Config.filesDir, filename))
  51. os.Remove(path.Join(Config.metaDir, filename))
  52. return false
  53. }
  54. return true
  55. }