ClsiManager.coffee 3.1 KB

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