CompileController.coffee 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. Metrics = require "../../infrastructure/Metrics"
  2. Project = require("../../models/Project").Project
  3. CompileManager = require("./CompileManager")
  4. ClsiManager = require("./ClsiManager")
  5. logger = require "logger-sharelatex"
  6. request = require "request"
  7. Settings = require "settings-sharelatex"
  8. AuthenticationController = require "../Authentication/AuthenticationController"
  9. UserGetter = require "../User/UserGetter"
  10. RateLimiter = require("../../infrastructure/RateLimiter")
  11. ClsiCookieManager = require("./ClsiCookieManager")
  12. module.exports = CompileController =
  13. compile: (req, res, next = (error) ->) ->
  14. res.setTimeout(5 * 60 * 1000)
  15. project_id = req.params.Project_id
  16. isAutoCompile = !!req.query?.auto_compile
  17. AuthenticationController.getLoggedInUserId req, (error, user_id) ->
  18. return next(error) if error?
  19. options = {
  20. isAutoCompile: isAutoCompile
  21. }
  22. if req.body?.rootDoc_id?
  23. options.rootDoc_id = req.body.rootDoc_id
  24. else if req.body?.settingsOverride?.rootDoc_id? # Can be removed after deploy
  25. options.rootDoc_id = req.body.settingsOverride.rootDoc_id
  26. if req.body?.compiler
  27. options.compiler = req.body.compiler
  28. if req.body?.draft
  29. options.draft = req.body.draft
  30. logger.log {options, project_id}, "got compile request"
  31. CompileManager.compile project_id, user_id, options, (error, status, outputFiles, clsiServerId, limits, validationProblems) ->
  32. return next(error) if error?
  33. res.contentType("application/json")
  34. res.status(200).send JSON.stringify {
  35. status: status
  36. outputFiles: outputFiles
  37. compileGroup: limits?.compileGroup
  38. clsiServerId:clsiServerId
  39. validationProblems:validationProblems
  40. }
  41. downloadPdf: (req, res, next = (error) ->)->
  42. Metrics.inc "pdf-downloads"
  43. project_id = req.params.Project_id
  44. isPdfjsPartialDownload = req.query?.pdfng
  45. rateLimit = (callback)->
  46. if isPdfjsPartialDownload
  47. callback null, true
  48. else
  49. rateLimitOpts =
  50. endpointName: "full-pdf-download"
  51. throttle: 1000
  52. subjectName : req.ip
  53. timeInterval : 60 * 60
  54. RateLimiter.addCount rateLimitOpts, callback
  55. Project.findById project_id, {name: 1}, (err, project)->
  56. res.contentType("application/pdf")
  57. if !!req.query.popupDownload
  58. logger.log project_id: project_id, "download pdf as popup download"
  59. res.header('Content-Disposition', "attachment; filename=#{project.getSafeProjectName()}.pdf")
  60. else
  61. logger.log project_id: project_id, "download pdf to embed in browser"
  62. res.header('Content-Disposition', "filename=#{project.getSafeProjectName()}.pdf")
  63. rateLimit (err, canContinue)->
  64. if err?
  65. logger.err err:err, "error checking rate limit for pdf download"
  66. return res.send 500
  67. else if !canContinue
  68. logger.log project_id:project_id, ip:req.ip, "rate limit hit downloading pdf"
  69. res.send 500
  70. else
  71. CompileController.proxyToClsi(project_id, "/project/#{project_id}/output/output.pdf", req, res, next)
  72. deleteAuxFiles: (req, res, next) ->
  73. project_id = req.params.Project_id
  74. CompileManager.deleteAuxFiles project_id, (error) ->
  75. return next(error) if error?
  76. res.sendStatus(200)
  77. compileAndDownloadPdf: (req, res, next)->
  78. project_id = req.params.project_id
  79. CompileManager.compile project_id, null, {}, (err)->
  80. if err?
  81. logger.err err:err, project_id:project_id, "something went wrong compile and downloading pdf"
  82. res.sendStatus 500
  83. url = "/project/#{project_id}/output/output.pdf"
  84. CompileController.proxyToClsi project_id, url, req, res, next
  85. getFileFromClsi: (req, res, next = (error) ->) ->
  86. project_id = req.params.Project_id
  87. build = req.params.build
  88. if build?
  89. url = "/project/#{project_id}/build/#{build}/output/#{req.params.file}"
  90. else
  91. url = "/project/#{project_id}/output/#{req.params.file}"
  92. CompileController.proxyToClsi(project_id, url, req, res, next)
  93. proxySync: (req, res, next = (error) ->) ->
  94. CompileController.proxyToClsi(req.params.Project_id, req.url, req, res, next)
  95. proxyToClsi: (project_id, url, req, res, next = (error) ->) ->
  96. if req.query?.compileGroup
  97. CompileController.proxyToClsiWithLimits(project_id, url, {compileGroup: req.query.compileGroup}, req, res, next)
  98. else
  99. CompileManager.getProjectCompileLimits project_id, (error, limits) ->
  100. return next(error) if error?
  101. CompileController.proxyToClsiWithLimits(project_id, url, limits, req, res, next)
  102. proxyToClsiWithLimits: (project_id, url, limits, req, res, next = (error) ->) ->
  103. ClsiCookieManager.getCookieJar project_id, (err, jar)->
  104. if err?
  105. logger.err err:err, "error getting cookie jar for clsi request"
  106. return callback(err)
  107. compilerUrl = Settings.apis.clsi.url
  108. url = "#{compilerUrl}#{url}"
  109. logger.log url: url, "proxying to CLSI"
  110. oneMinute = 60 * 1000
  111. # the base request
  112. options = { url: url, method: req.method, timeout: oneMinute, jar : jar }
  113. # if we have a build parameter, pass it through to the clsi
  114. if req.query?.pdfng && req.query?.build? # only for new pdf viewer
  115. options.qs = {}
  116. options.qs.build = req.query.build
  117. # if we are byte serving pdfs, pass through If-* and Range headers
  118. # do not send any others, there's a proxying loop if Host: is passed!
  119. if req.query?.pdfng
  120. newHeaders = {}
  121. for h, v of req.headers
  122. newHeaders[h] = req.headers[h] if h.match /^(If-|Range)/i
  123. options.headers = newHeaders
  124. proxy = request(options)
  125. proxy.pipe(res)
  126. proxy.on "error", (error) ->
  127. logger.warn err: error, url: url, "CLSI proxy error"
  128. wordCount: (req, res, next) ->
  129. project_id = req.params.Project_id
  130. file = req.query.file || false
  131. CompileManager.wordCount project_id, file, (error, body) ->
  132. return next(error) if error?
  133. res.contentType("application/json")
  134. res.send body