CompileController.coffee 7.9 KB

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