CompileController.coffee 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. ClsiRequestManager = require("./ClsiRequestManager")
  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, output, limits) ->
  32. return next(error) if error?
  33. res.contentType("application/json")
  34. res.send 200, JSON.stringify {
  35. status: status
  36. outputFiles: outputFiles
  37. compileGroup: limits?.compileGroup
  38. }
  39. downloadPdf: (req, res, next = (error) ->)->
  40. Metrics.inc "pdf-downloads"
  41. project_id = req.params.Project_id
  42. isPdfjsPartialDownload = req.query?.pdfng
  43. rateLimit = (callback)->
  44. if isPdfjsPartialDownload
  45. callback null, true
  46. else
  47. rateLimitOpts =
  48. endpointName: "full-pdf-download"
  49. throttle: 1000
  50. subjectName : req.ip
  51. timeInterval : 60 * 60
  52. RateLimiter.addCount rateLimitOpts, callback
  53. Project.findById project_id, {name: 1}, (err, project)->
  54. res.contentType("application/pdf")
  55. if !!req.query.popupDownload
  56. logger.log project_id: project_id, "download pdf as popup download"
  57. res.header('Content-Disposition', "attachment; filename=#{project.getSafeProjectName()}.pdf")
  58. else
  59. logger.log project_id: project_id, "download pdf to embed in browser"
  60. res.header('Content-Disposition', "filename=#{project.getSafeProjectName()}.pdf")
  61. rateLimit (err, canContinue)->
  62. if err?
  63. logger.err err:err, "error checking rate limit for pdf download"
  64. return res.send 500
  65. else if !canContinue
  66. logger.log project_id:project_id, ip:req.ip, "rate limit hit downloading pdf"
  67. res.send 500
  68. else
  69. CompileController.proxyToClsi(project_id, "/project/#{project_id}/output/output.pdf", req, res, next)
  70. deleteAuxFiles: (req, res, next) ->
  71. project_id = req.params.Project_id
  72. CompileManager.deleteAuxFiles project_id, (error) ->
  73. return next(error) if error?
  74. res.sendStatus(200)
  75. compileAndDownloadPdf: (req, res, next)->
  76. project_id = req.params.project_id
  77. CompileManager.compile project_id, null, {}, (err)->
  78. if err?
  79. logger.err err:err, project_id:project_id, "something went wrong compile and downloading pdf"
  80. res.sendStatus 500
  81. url = "/project/#{project_id}/output/output.pdf"
  82. CompileController.proxyToClsi project_id, url, req, res, next
  83. getFileFromClsi: (req, res, next = (error) ->) ->
  84. project_id = req.params.Project_id
  85. CompileController.proxyToClsi(project_id, "/project/#{project_id}/output/#{req.params.file}", req, res, next)
  86. proxySync: (req, res, next = (error) ->) ->
  87. CompileController.proxyToClsi(req.params.Project_id, req.url, req, res, next)
  88. proxyToClsi: (project_id, url, req, res, next = (error) ->) ->
  89. if req.query?.compileGroup
  90. CompileController.proxyToClsiWithLimits(project_id, url, {compileGroup: req.query.compileGroup}, req, res, next)
  91. else
  92. CompileManager.getProjectCompileLimits project_id, (error, limits) ->
  93. return next(error) if error?
  94. CompileController.proxyToClsiWithLimits(project_id, url, limits, req, res, next)
  95. proxyToClsiWithLimits: (project_id, url, limits, req, res, next = (error) ->) ->
  96. ClsiRequestManager.getCookieJar project_id, (err, jar)->
  97. if err?
  98. logger.err err:err, "error getting cookie jar for clsi request"
  99. return callback(err)
  100. if limits.compileGroup == "priority"
  101. compilerUrl = Settings.apis.clsi_priority.url
  102. else
  103. compilerUrl = Settings.apis.clsi.url
  104. url = "#{compilerUrl}#{url}"
  105. logger.log url: url, "proxying to CLSI"
  106. oneMinute = 60 * 1000
  107. # the base request
  108. options = { url: url, method: req.method, timeout: oneMinute, jar : jar }
  109. # if we have a build parameter, pass it through to the clsi
  110. if req.query?.pdfng && req.query?.build? # only for new pdf viewer
  111. options.qs = {}
  112. options.qs.build = req.query.build
  113. # if we are byte serving pdfs, pass through If-* and Range headers
  114. # do not send any others, there's a proxying loop if Host: is passed!
  115. if req.query?.pdfng
  116. newHeaders = {}
  117. for h, v of req.headers
  118. newHeaders[h] = req.headers[h] if h.match /^(If-|Range)/i
  119. options.headers = newHeaders
  120. proxy = request(options)
  121. proxy.pipe(res)
  122. proxy.on "error", (error) ->
  123. logger.warn err: error, url: url, "CLSI proxy error"
  124. wordCount: (req, res, next) ->
  125. project_id = req.params.Project_id
  126. file = req.query.file || false
  127. CompileManager.wordCount project_id, file, (error, body) ->
  128. return next(error) if error?
  129. res.contentType("application/json")
  130. res.send body