CompileController.coffee 6.7 KB

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