ExpressLocals.coffee 3.8 KB

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