ExpressLocals.coffee 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. logger = require 'logger-sharelatex'
  2. fs = require 'fs'
  3. crypto = require 'crypto'
  4. Settings = require('settings-sharelatex')
  5. SubscriptionFormatters = require('../Features/Subscription/SubscriptionFormatters')
  6. querystring = require('querystring')
  7. fingerprints = {}
  8. Path = require 'path'
  9. jsPath =
  10. if Settings.useMinifiedJs
  11. "/minjs/"
  12. else
  13. "/js/"
  14. logger.log "Generating file fingerprints..."
  15. for path in [
  16. "#{jsPath}libs/require.js",
  17. "#{jsPath}ide.js",
  18. "#{jsPath}main.js",
  19. "#{jsPath}list.js",
  20. "#{jsPath}libs/pdf.js",
  21. "#{jsPath}libs/pdf.worker.js",
  22. "/stylesheets/mainStyle.css",
  23. "/brand/plans.css"
  24. ]
  25. filePath = Path.join __dirname, "../../../", "public#{path}"
  26. exists = fs.existsSync filePath
  27. if exists
  28. content = fs.readFileSync filePath
  29. hash = crypto.createHash("md5").update(content).digest("hex")
  30. logger.log "#{filePath}: #{hash}"
  31. fingerprints[path] = hash
  32. else
  33. logger.log filePath:filePath, "file does not exist for fingerprints"
  34. module.exports = (app)->
  35. app.use (req, res, next)->
  36. res.locals.session = req.session
  37. next()
  38. app.use (req, res, next)->
  39. res.locals.jsPath = jsPath
  40. next()
  41. app.use (req, res, next)->
  42. res.locals.settings = Settings
  43. next()
  44. app.use (req, res, next)->
  45. res.locals.getSiteHost = ->
  46. Settings.siteUrl.substring(Settings.siteUrl.indexOf("//")+2)
  47. next()
  48. app.use (req, res, next)->
  49. res.locals.formatProjectPublicAccessLevel = (privilegeLevel)->
  50. formatedPrivileges = private:"Private", readOnly:"Public: Read Only", readAndWrite:"Public: Read and Write"
  51. return formatedPrivileges[privilegeLevel] || "Private"
  52. next()
  53. app.use (req, res, next)->
  54. res.locals.buildReferalUrl = (referal_medium) ->
  55. url = Settings.siteUrl
  56. if req.session? and req.session.user? and req.session.user.referal_id?
  57. url+="?r=#{req.session.user.referal_id}&rm=#{referal_medium}&rs=b" # Referal source = bonus
  58. return url
  59. res.locals.getReferalId = ->
  60. if req.session? and req.session.user? and req.session.user.referal_id
  61. return req.session.user.referal_id
  62. res.locals.getReferalTagLine = ->
  63. tagLines = [
  64. "Roar!"
  65. "Shout about us!"
  66. "Please recommend us"
  67. "Tell the world!"
  68. "Thanks for using ShareLaTeX"
  69. ]
  70. return tagLines[Math.floor(Math.random()*tagLines.length)]
  71. res.locals.getRedirAsQueryString = ->
  72. if req.query.redir?
  73. return "?#{querystring.stringify({redir:req.query.redir})}"
  74. return ""
  75. next()
  76. app.use (req, res, next) ->
  77. res.locals.csrfToken = req.session._csrf
  78. next()
  79. app.use (req, res, next)->
  80. res.locals.fingerprint = (path) ->
  81. if fingerprints[path]?
  82. return fingerprints[path]
  83. else
  84. logger.err "No fingerprint for file: #{path}"
  85. return ""
  86. next()
  87. app.use (req, res, next)->
  88. res.locals.formatPrice = SubscriptionFormatters.formatPrice
  89. next()
  90. app.use (req, res, next)->
  91. if req.session.user?
  92. res.locals.user =
  93. email: req.session.user.email
  94. first_name: req.session.user.first_name
  95. last_name: req.session.user.last_name
  96. if req.session.justRegistered
  97. res.locals.justRegistered = true
  98. delete req.session.justRegistered
  99. if req.session.justLoggedIn
  100. res.locals.justLoggedIn = true
  101. delete req.session.justLoggedIn
  102. res.locals.gaToken = Settings.analytics?.ga?.token
  103. res.locals.tenderUrl = Settings.tenderUrl
  104. next()
  105. app.use (req, res, next) ->
  106. if req.query? and req.query.scribtex_path?
  107. res.locals.lookingForScribtex = true
  108. res.locals.scribtexPath = req.query.scribtex_path
  109. next()