CompileController.coffee 2.8 KB

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