CompileController.coffee 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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) ->
  28. return next(error) if error?
  29. res.contentType("application/json")
  30. res.send 200, JSON.stringify {
  31. status: status
  32. outputFiles: outputFiles
  33. }
  34. downloadPdf: (req, res, next = (error) ->)->
  35. Metrics.inc "pdf-downloads"
  36. project_id = req.params.Project_id
  37. Project.findById project_id, {name: 1}, (err, project)->
  38. res.contentType("application/pdf")
  39. if !!req.query.popupDownload
  40. logger.log project_id: project_id, "download pdf as popup download"
  41. res.header('Content-Disposition', "attachment; filename=#{project.getSafeProjectName()}.pdf")
  42. else
  43. logger.log project_id: project_id, "download pdf to embed in browser"
  44. res.header('Content-Disposition', "filename=#{project.getSafeProjectName()}.pdf")
  45. CompileController.proxyToClsi(project_id, "/project/#{project_id}/output/output.pdf", req, res, next)
  46. deleteAuxFiles: (req, res, next) ->
  47. project_id = req.params.Project_id
  48. CompileManager.deleteAuxFiles project_id, (error) ->
  49. return next(error) if error?
  50. res.send(200)
  51. compileAndDownloadPdf: (req, res, next)->
  52. project_id = req.params.project_id
  53. CompileManager.compile project_id, null, {}, (err)->
  54. if err?
  55. logger.err err:err, project_id:project_id, "something went wrong compile and downloading pdf"
  56. res.send 500
  57. url = "/project/#{project_id}/output/output.pdf"
  58. CompileController.proxyToClsi project_id, url, req, res, next
  59. getFileFromClsi: (req, res, next = (error) ->) ->
  60. project_id = req.params.Project_id
  61. CompileController.proxyToClsi(project_id, "/project/#{project_id}/output/#{req.params.file}", req, res, next)
  62. proxySync: (req, res, next = (error) ->) ->
  63. CompileController.proxyToClsi(req.params.Project_id, req.url, req, res, next)
  64. proxyToClsi: (project_id, url, req, res, next = (error) ->) ->
  65. CompileManager.getProjectCompileLimits project_id, (error, limits) ->
  66. return next(error) if error?
  67. if limits.compileGroup == "priority"
  68. compilerUrl = Settings.apis.clsi_priority.url
  69. else
  70. compilerUrl = Settings.apis.clsi.url
  71. url = "#{compilerUrl}#{url}"
  72. logger.log url: url, "proxying to CLSI"
  73. oneMinute = 60 * 1000
  74. proxy = request(url: url, method: req.method, timeout: oneMinute)
  75. proxy.pipe(res)
  76. proxy.on "error", (error) ->
  77. logger.warn err: error, url: url, "CLSI proxy error"