CompileController.coffee 3.1 KB

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