ClsiManager.coffee 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. ClsiRequestManager = require("./ClsiRequestManager")
  10. module.exports = ClsiManager =
  11. _makeRequest: (project_id, opts, callback)->
  12. ClsiRequestManager.getCookieJar project_id, (err, jar)->
  13. if err?
  14. logger.err err:err, "error getting cookie jar for clsi request"
  15. return callback(err)
  16. opts.jar = jar
  17. request opts, callback
  18. sendRequest: (project_id, options = {}, callback = (error, success) ->) ->
  19. ClsiManager._buildRequest project_id, options, (error, req) ->
  20. return callback(error) if error?
  21. logger.log project_id: project_id, "sending compile to CLSI"
  22. ClsiManager._postToClsi project_id, req, options.compileGroup, (error, response) ->
  23. return callback(error) if error?
  24. logger.log project_id: project_id, response: response, "received compile response from CLSI"
  25. callback(
  26. null
  27. response?.compile?.status
  28. ClsiManager._parseOutputFiles(project_id, response?.compile?.outputFiles)
  29. )
  30. deleteAuxFiles: (project_id, options, callback = (error) ->) ->
  31. compilerUrl = @_getCompilerUrl(options?.compileGroup)
  32. opts =
  33. url:"#{compilerUrl}/project/#{project_id}"
  34. method:"DELETE"
  35. ClsiManager._makeRequest project_id, opts, callback
  36. _getCompilerUrl: (compileGroup) ->
  37. if compileGroup == "priority"
  38. return Settings.apis.clsi_priority.url
  39. else
  40. return Settings.apis.clsi.url
  41. _postToClsi: (project_id, req, compileGroup, callback = (error, response) ->) ->
  42. compilerUrl = @_getCompilerUrl(compileGroup)
  43. opts =
  44. url: "#{compilerUrl}/project/#{project_id}/compile"
  45. json: req
  46. method: "POST"
  47. ClsiManager._makeRequest project_id, opts, (error, response, body) ->
  48. return callback(error) if error?
  49. if 200 <= response.statusCode < 300
  50. callback null, body
  51. else if response.statusCode == 413
  52. callback null, compile:status:"project-too-large"
  53. else
  54. error = new Error("CLSI returned non-success code: #{response.statusCode}")
  55. logger.error err: error, project_id: project_id, "CLSI returned failure code"
  56. callback error, body
  57. _parseOutputFiles: (project_id, rawOutputFiles = []) ->
  58. outputFiles = []
  59. for file in rawOutputFiles
  60. outputFiles.push
  61. path: url.parse(file.url).path.replace("/project/#{project_id}/output/", "")
  62. type: file.type
  63. build: file.build
  64. return outputFiles
  65. VALID_COMPILERS: ["pdflatex", "latex", "xelatex", "lualatex"]
  66. _buildRequest: (project_id, options={}, callback = (error, request) ->) ->
  67. Project.findById project_id, {compiler: 1, rootDoc_id: 1, imageName: 1}, (error, project) ->
  68. return callback(error) if error?
  69. return callback(new Errors.NotFoundError("project does not exist: #{project_id}")) if !project?
  70. if project.compiler not in ClsiManager.VALID_COMPILERS
  71. project.compiler = "pdflatex"
  72. ProjectEntityHandler.getAllDocs project_id, (error, docs = {}) ->
  73. return callback(error) if error?
  74. ProjectEntityHandler.getAllFiles project_id, (error, files = {}) ->
  75. return callback(error) if error?
  76. resources = []
  77. rootResourcePath = null
  78. rootResourcePathOverride = null
  79. for path, doc of docs
  80. path = path.replace(/^\//, "") # Remove leading /
  81. resources.push
  82. path: path
  83. content: doc.lines.join("\n")
  84. if project.rootDoc_id? and doc._id.toString() == project.rootDoc_id.toString()
  85. rootResourcePath = path
  86. if options.rootDoc_id? and doc._id.toString() == options.rootDoc_id.toString()
  87. rootResourcePathOverride = path
  88. rootResourcePath = rootResourcePathOverride if rootResourcePathOverride?
  89. if !rootResourcePath?
  90. logger.warn {project_id}, "no root document found, setting to main.tex"
  91. rootResourcePath = "main.tex"
  92. for path, file of files
  93. path = path.replace(/^\//, "") # Remove leading /
  94. resources.push
  95. path: path
  96. url: "#{Settings.apis.filestore.url}/project/#{project._id}/file/#{file._id}"
  97. modified: file.created?.getTime()
  98. callback null, {
  99. compile:
  100. options:
  101. compiler: project.compiler
  102. timeout: options.timeout
  103. imageName: project.imageName
  104. draft: !!options.draft
  105. rootResourcePath: rootResourcePath
  106. resources: resources
  107. }
  108. wordCount: (project_id, file, options, callback = (error, response) ->) ->
  109. ClsiManager._buildRequest project_id, options, (error, req) ->
  110. compilerUrl = ClsiManager._getCompilerUrl(options?.compileGroup)
  111. filename = file || req?.compile?.rootResourcePath
  112. wordcount_url = "#{compilerUrl}/project/#{project_id}/wordcount?file=#{encodeURIComponent(filename)}"
  113. if req.compile.options.imageName?
  114. wordcount_url += "&image=#{encodeURIComponent(req.compile.options.imageName)}"
  115. opts =
  116. url: wordcount_url
  117. method: "GET"
  118. ClsiManager._makeRequest project_id, opts, (error, response, body) ->
  119. return callback(error) if error?
  120. if 200 <= response.statusCode < 300
  121. callback null, body
  122. else
  123. error = new Error("CLSI returned non-success code: #{response.statusCode}")
  124. logger.error err: error, project_id: project_id, "CLSI returned failure code"
  125. callback error, body