ClsiManager.coffee 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. Path = require "path"
  2. async = require "async"
  3. Settings = require "settings-sharelatex"
  4. request = require('request')
  5. Project = require("../../models/Project").Project
  6. ProjectEntityHandler = require("../Project/ProjectEntityHandler")
  7. logger = require "logger-sharelatex"
  8. url = require("url")
  9. module.exports = ClsiManager =
  10. sendRequest: (project_id, settingsOverride = {}, callback = (error, success) ->) ->
  11. ClsiManager._buildRequest project_id, settingsOverride, (error, req) ->
  12. return callback(error) if error?
  13. logger.log project_id: project_id, "sending compile to CLSI"
  14. ClsiManager._postToClsi project_id, req, (error, response) ->
  15. return callback(error) if error?
  16. logger.log project_id: project_id, response: response, "received compile response from CLSI"
  17. callback(
  18. null
  19. response?.compile?.status
  20. ClsiManager._parseOutputFiles(project_id, response?.compile?.outputFiles)
  21. )
  22. getLogLines: (project_id, callback = (error, lines) ->) ->
  23. request "#{Settings.apis.clsi.url}/project/#{project_id}/output/output.log", (error, response, body) ->
  24. return callback(error) if error?
  25. callback null, body?.split("\n") or []
  26. deleteAuxFiles: (project_id, callback = (error) ->) ->
  27. request.del "#{Settings.apis.clsi.url}/project/#{project_id}", callback
  28. _postToClsi: (project_id, req, callback = (error, response) ->) ->
  29. request.post {
  30. url: "#{Settings.apis.clsi.url}/project/#{project_id}/compile"
  31. json: req
  32. jar: false
  33. }, (error, response, body) ->
  34. return callback(error) if error?
  35. if 200 <= response.statusCode < 300
  36. callback null, body
  37. else
  38. error = new Error("CLSI returned non-success code: #{response.statusCode}")
  39. logger.error err: error, project_id: project_id, "CLSI returned failure code"
  40. callback error, body
  41. _parseOutputFiles: (project_id, rawOutputFiles = []) ->
  42. outputFiles = []
  43. for file in rawOutputFiles
  44. outputFiles.push
  45. path: url.parse(file.url).path.replace("/project/#{project_id}/output/", "")
  46. type: file.type
  47. return outputFiles
  48. VALID_COMPILERS: ["pdflatex", "latex", "xelatex", "lualatex"]
  49. _buildRequest: (project_id, settingsOverride={}, callback = (error, request) ->) ->
  50. Project.findById project_id, {compiler: 1, rootDoc_id: 1}, (error, project) ->
  51. return callback(error) if error?
  52. return callback(new Errors.NotFoundError("project does not exist: #{project_id}")) if !project?
  53. if project.compiler not in ClsiManager.VALID_COMPILERS
  54. project.compiler = "pdflatex"
  55. ProjectEntityHandler.getAllDocs project_id, (error, docs = {}) ->
  56. return callback(error) if error?
  57. ProjectEntityHandler.getAllFiles project_id, (error, files = {}) ->
  58. return callback(error) if error?
  59. resources = []
  60. rootResourcePath = null
  61. rootResourcePathOverride = null
  62. for path, doc of docs
  63. path = path.replace(/^\//, "") # Remove leading /
  64. resources.push
  65. path: path
  66. content: doc.lines.join("\n")
  67. if project.rootDoc_id? and doc._id.toString() == project.rootDoc_id.toString()
  68. rootResourcePath = path
  69. if settingsOverride.rootDoc_id? and doc._id.toString() == settingsOverride.rootDoc_id.toString()
  70. rootResourcePathOverride = path
  71. rootResourcePath = rootResourcePathOverride if rootResourcePathOverride?
  72. for path, file of files
  73. path = path.replace(/^\//, "") # Remove leading /
  74. resources.push
  75. path: path
  76. url: "#{Settings.apis.filestore.url}/project/#{project._id}/file/#{file._id}"
  77. modified: file.created?.getTime()
  78. if !rootResourcePath?
  79. callback new Error("no root document exists")
  80. else
  81. callback null, {
  82. compile:
  83. options:
  84. compiler: project.compiler
  85. rootResourcePath: rootResourcePath
  86. resources: resources
  87. }