CompileController.coffee 5.3 KB

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