CompileController.coffee 5.8 KB

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