CompileController.coffee 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. settingsOverride = req.body?.settingsOverride ? {};
  16. logger.log "root doc overriden" if settingsOverride.rootDoc_id?
  17. AuthenticationController.getLoggedInUserId req, (error, user_id) ->
  18. return next(error) if error?
  19. UserGetter.getUser user_id, {"features.compileGroup":1, "features.compileTimeout":1}, (err, user)->
  20. settingsOverride.timeout = user.features.compileTimeout || Settings.defaultFeatures.compileTimeout
  21. settingsOverride.compiler = user.features.compileGroup || Settings.defaultFeatures.compileGroup
  22. req.session.compileGroup = settingsOverride.compiler
  23. CompileManager.compile project_id, user_id, { isAutoCompile, settingsOverride }, (error, status, outputFiles) ->
  24. return next(error) if error?
  25. res.contentType("application/json")
  26. res.send 200, JSON.stringify {
  27. status: status
  28. outputFiles: outputFiles
  29. }
  30. downloadPdf: (req, res, next = (error) ->)->
  31. Metrics.inc "pdf-downloads"
  32. project_id = req.params.Project_id
  33. Project.findById project_id, {name: 1}, (err, project)->
  34. res.contentType("application/pdf")
  35. if !!req.query.popupDownload
  36. logger.log project_id: project_id, "download pdf as popup download"
  37. res.header('Content-Disposition', "attachment; filename=#{project.getSafeProjectName()}.pdf")
  38. else
  39. logger.log project_id: project_id, "download pdf to embed in browser"
  40. res.header('Content-Disposition', "filename=#{project.getSafeProjectName()}.pdf")
  41. CompileController.proxyToClsi("/project/#{project_id}/output/output.pdf", req, res, next)
  42. deleteAuxFiles: (req, res, next) ->
  43. project_id = req.params.Project_id
  44. ClsiManager.deleteAuxFiles project_id, (error) ->
  45. return next(error) if error?
  46. res.send(200)
  47. compileAndDownloadPdf: (req, res, next)->
  48. project_id = req.params.project_id
  49. CompileManager.compile project_id, null, {}, (err)->
  50. if err?
  51. logger.err err:err, project_id:project_id, "something went wrong compile and downloading pdf"
  52. res.send 500
  53. url = "/project/#{project_id}/output/output.pdf"
  54. CompileController.proxyToClsi url, req, res, next
  55. getFileFromClsi: (req, res, next = (error) ->) ->
  56. CompileController.proxyToClsi("/project/#{req.params.Project_id}/output/#{req.params.file}", req, res, next)
  57. proxySync: (req, res, next = (error) ->) ->
  58. CompileController.proxyToClsi(req.url, req, res, next)
  59. proxyToClsi: (url, req, res, next = (error) ->) ->
  60. logger.log url: url, "proxying to CLSI"
  61. if req.session.compileGroup == "priority"
  62. compilerUrl = Settings.apis.clsi_priority.url
  63. else
  64. compilerUrl = Settings.apis.clsi.url
  65. url = "#{compilerUrl}#{url}"
  66. oneMinute = 60 * 1000
  67. proxy = request(url: url, method: req.method, timeout: oneMinute)
  68. proxy.pipe(res)
  69. proxy.on "error", (error) ->
  70. logger.warn err: error, url: url, "CLSI proxy error"