CompileController.coffee 5.2 KB

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