CompileController.coffee 8.0 KB

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