CompileController.coffee 2.6 KB

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