CompileController.coffee 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. module.exports = CompileController =
  11. compile: (req, res, next = (error) ->) ->
  12. res.setTimeout(5 * 60 * 1000)
  13. project_id = req.params.Project_id
  14. isAutoCompile = !!req.query?.auto_compile
  15. AuthenticationController.getLoggedInUserId req, (error, user_id) ->
  16. return next(error) if error?
  17. options = {
  18. isAutoCompile: isAutoCompile
  19. }
  20. if req.body?.rootDoc_id?
  21. options.rootDoc_id = req.body.rootDoc_id
  22. else if req.body?.settingsOverride?.rootDoc_id? # Can be removed after deploy
  23. options.rootDoc_id = req.body.settingsOverride.rootDoc_id
  24. if req.body?.compiler
  25. options.compiler = req.body.compiler
  26. logger.log {options, project_id}, "got compile request"
  27. CompileManager.compile project_id, user_id, options, (error, status, outputFiles, output, limits) ->
  28. return next(error) if error?
  29. res.contentType("application/json")
  30. res.send 200, JSON.stringify {
  31. status: status
  32. outputFiles: outputFiles
  33. compileGroup: limits?.compileGroup
  34. }
  35. downloadPdf: (req, res, next = (error) ->)->
  36. Metrics.inc "pdf-downloads"
  37. project_id = req.params.Project_id
  38. Project.findById project_id, {name: 1}, (err, project)->
  39. res.contentType("application/pdf")
  40. if !!req.query.popupDownload
  41. logger.log project_id: project_id, "download pdf as popup download"
  42. res.header('Content-Disposition', "attachment; filename=#{project.getSafeProjectName()}.pdf")
  43. else
  44. logger.log project_id: project_id, "download pdf to embed in browser"
  45. res.header('Content-Disposition', "filename=#{project.getSafeProjectName()}.pdf")
  46. CompileController.proxyToClsi(project_id, "/project/#{project_id}/output/output.pdf", req, res, next)
  47. deleteAuxFiles: (req, res, next) ->
  48. project_id = req.params.Project_id
  49. CompileManager.deleteAuxFiles project_id, (error) ->
  50. return next(error) if error?
  51. res.sendStatus(200)
  52. compileAndDownloadPdf: (req, res, next)->
  53. project_id = req.params.project_id
  54. CompileManager.compile project_id, null, {}, (err)->
  55. if err?
  56. logger.err err:err, project_id:project_id, "something went wrong compile and downloading pdf"
  57. res.sendStatus 500
  58. url = "/project/#{project_id}/output/output.pdf"
  59. CompileController.proxyToClsi project_id, url, req, res, next
  60. getFileFromClsi: (req, res, next = (error) ->) ->
  61. project_id = req.params.Project_id
  62. CompileController.proxyToClsi(project_id, "/project/#{project_id}/output/#{req.params.file}", req, res, next)
  63. proxySync: (req, res, next = (error) ->) ->
  64. CompileController.proxyToClsi(req.params.Project_id, req.url, req, res, next)
  65. proxyToClsi: (project_id, url, req, res, next = (error) ->) ->
  66. if req.query?.compileGroup
  67. CompileController.proxyToClsiWithLimits(project_id, url, {compileGroup: req.query.compileGroup}, req, res, next)
  68. else
  69. CompileManager.getProjectCompileLimits project_id, (error, limits) ->
  70. return next(error) if error?
  71. CompileController.proxyToClsiWithLimits(project_id, url, limits, req, res, next)
  72. proxyToClsiWithLimits: (project_id, url, limits, req, res, next = (error) ->) ->
  73. if limits.compileGroup == "priority"
  74. compilerUrl = Settings.apis.clsi_priority.url
  75. else
  76. compilerUrl = Settings.apis.clsi.url
  77. url = "#{compilerUrl}#{url}"
  78. logger.log url: url, "proxying to CLSI"
  79. oneMinute = 60 * 1000
  80. # the base request
  81. options = { url: url, method: req.method, timeout: oneMinute }
  82. # if we have a build parameter, pass it through to the clsi
  83. if req.query?.pdfng && req.query?.build? # only for new pdf viewer
  84. options.qs = {}
  85. options.qs.build = req.query.build
  86. # if we are byte serving pdfs, pass through If-* and Range headers
  87. # do not send any others, there's a proxying loop if Host: is passed!
  88. if req.query?.pdfng
  89. newHeaders = {}
  90. for h, v of req.headers
  91. newHeaders[h] = req.headers[h] if h.match /^(If-|Range)/i
  92. options.headers = newHeaders
  93. proxy = request(options)
  94. proxy.pipe(res)
  95. proxy.on "error", (error) ->
  96. logger.warn err: error, url: url, "CLSI proxy error"