ClsiManager.coffee 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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
  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, 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. for path, doc of docs
  62. path = path.replace(/^\//, "") # Remove leading /
  63. resources.push
  64. path: path
  65. content: doc.lines.join("\n")
  66. if project.rootDoc_id? and doc._id.toString() == project.rootDoc_id.toString()
  67. rootResourcePath = path
  68. for path, file of files
  69. path = path.replace(/^\//, "") # Remove leading /
  70. resources.push
  71. path: path
  72. url: "#{Settings.apis.filestore.url}/project/#{project._id}/file/#{file._id}"
  73. modified: file.created?.getTime()
  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. }