ClsiManager.coffee 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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, callback = (error, success) ->) ->
  11. ClsiManager._buildRequest project_id, (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 == "success")
  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. _postToClsi: (project_id, req, callback = (error, response) ->) ->
  27. request.post {
  28. url: "#{Settings.apis.clsi.url}/project/#{project_id}/compile"
  29. json: req
  30. jar: false
  31. }, (error, response, body) ->
  32. callback error, body
  33. _parseOutputFiles: (project_id, rawOutputFiles = []) ->
  34. outputFiles = []
  35. for file in rawOutputFiles
  36. outputFiles.push
  37. path: url.parse(file.url).path.replace("/project/#{project_id}/output/", "")
  38. type: file.type
  39. return outputFiles
  40. VALID_COMPILERS: ["pdflatex", "latex", "xelatex", "lualatex"]
  41. _buildRequest: (project_id, callback = (error, request) ->) ->
  42. Project.findById project_id, {compiler: 1, rootDoc_id: 1}, (error, project) ->
  43. return callback(error) if error?
  44. return callback(new Errors.NotFoundError("project does not exist: #{project_id}")) if !project?
  45. if project.compiler not in ClsiManager.VALID_COMPILERS
  46. project.compiler = "pdflatex"
  47. ProjectEntityHandler.getAllDocs project_id, (error, docs = {}) ->
  48. return callback(error) if error?
  49. ProjectEntityHandler.getAllFiles project_id, (error, files = {}) ->
  50. return callback(error) if error?
  51. resources = []
  52. rootResourcePath = null
  53. for path, doc of docs
  54. path = path.replace(/^\//, "") # Remove leading /
  55. resources.push
  56. path: path
  57. content: doc.lines.join("\n")
  58. if project.rootDoc_id? and doc._id.toString() == project.rootDoc_id.toString()
  59. rootResourcePath = path
  60. for path, file of files
  61. path = path.replace(/^\//, "") # Remove leading /
  62. resources.push
  63. path: path
  64. url: "#{Settings.apis.filestore.url}/project/#{project._id}/file/#{file._id}"
  65. modified: file.created?.getTime()
  66. if !rootResourcePath?
  67. callback new Error("no root document exists")
  68. else
  69. callback null, {
  70. compile:
  71. options:
  72. compiler: project.compiler
  73. rootResourcePath: rootResourcePath
  74. resources: resources
  75. }