2014-02-12 10:23:40 +00:00
|
|
|
Path = require "path"
|
|
|
|
async = require "async"
|
|
|
|
Settings = require "settings-sharelatex"
|
|
|
|
request = require('request')
|
|
|
|
Project = require("../../models/Project").Project
|
2014-05-06 11:54:26 +00:00
|
|
|
ProjectEntityHandler = require("../Project/ProjectEntityHandler")
|
2014-02-12 10:23:40 +00:00
|
|
|
logger = require "logger-sharelatex"
|
|
|
|
url = require("url")
|
|
|
|
|
|
|
|
module.exports = ClsiManager =
|
2014-06-01 14:43:52 +00:00
|
|
|
sendRequest: (project_id, settingsOverride = {}, callback = (error, success) ->) ->
|
|
|
|
ClsiManager._buildRequest project_id, settingsOverride, (error, req) ->
|
2014-02-12 10:23:40 +00:00
|
|
|
return callback(error) if error?
|
2014-05-06 11:54:26 +00:00
|
|
|
logger.log project_id: project_id, "sending compile to CLSI"
|
|
|
|
ClsiManager._postToClsi project_id, req, (error, response) ->
|
2014-02-12 10:23:40 +00:00
|
|
|
return callback(error) if error?
|
2014-05-06 11:54:26 +00:00
|
|
|
logger.log project_id: project_id, response: response, "received compile response from CLSI"
|
|
|
|
callback(
|
|
|
|
null
|
2014-05-19 12:46:22 +00:00
|
|
|
response?.compile?.status
|
2014-05-06 11:54:26 +00:00
|
|
|
ClsiManager._parseOutputFiles(project_id, response?.compile?.outputFiles)
|
|
|
|
)
|
2014-02-12 10:23:40 +00:00
|
|
|
|
|
|
|
getLogLines: (project_id, callback = (error, lines) ->) ->
|
|
|
|
request "#{Settings.apis.clsi.url}/project/#{project_id}/output/output.log", (error, response, body) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
callback null, body?.split("\n") or []
|
|
|
|
|
2014-05-19 15:10:41 +00:00
|
|
|
deleteAuxFiles: (project_id, callback = (error) ->) ->
|
|
|
|
request.del "#{Settings.apis.clsi.url}/project/#{project_id}", callback
|
|
|
|
|
2014-02-12 10:23:40 +00:00
|
|
|
_postToClsi: (project_id, req, callback = (error, response) ->) ->
|
|
|
|
request.post {
|
|
|
|
url: "#{Settings.apis.clsi.url}/project/#{project_id}/compile"
|
|
|
|
json: req
|
|
|
|
jar: false
|
|
|
|
}, (error, response, body) ->
|
2014-05-19 12:46:22 +00:00
|
|
|
return callback(error) if error?
|
|
|
|
if 200 <= response.statusCode < 300
|
|
|
|
callback null, body
|
|
|
|
else
|
|
|
|
error = new Error("CLSI returned non-success code: #{response.statusCode}")
|
|
|
|
logger.error err: error, project_id: project_id, "CLSI returned failure code"
|
|
|
|
callback error, body
|
2014-02-12 10:23:40 +00:00
|
|
|
|
|
|
|
_parseOutputFiles: (project_id, rawOutputFiles = []) ->
|
|
|
|
outputFiles = []
|
|
|
|
for file in rawOutputFiles
|
|
|
|
outputFiles.push
|
|
|
|
path: url.parse(file.url).path.replace("/project/#{project_id}/output/", "")
|
|
|
|
type: file.type
|
|
|
|
return outputFiles
|
|
|
|
|
|
|
|
VALID_COMPILERS: ["pdflatex", "latex", "xelatex", "lualatex"]
|
2014-06-01 14:43:52 +00:00
|
|
|
_buildRequest: (project_id, settingsOverride={}, callback = (error, request) ->) ->
|
2014-05-06 11:54:26 +00:00
|
|
|
Project.findById project_id, {compiler: 1, rootDoc_id: 1}, (error, project) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
return callback(new Errors.NotFoundError("project does not exist: #{project_id}")) if !project?
|
2014-02-12 10:23:40 +00:00
|
|
|
|
2014-05-06 11:54:26 +00:00
|
|
|
if project.compiler not in ClsiManager.VALID_COMPILERS
|
|
|
|
project.compiler = "pdflatex"
|
2014-02-12 10:23:40 +00:00
|
|
|
|
2014-05-06 11:54:26 +00:00
|
|
|
ProjectEntityHandler.getAllDocs project_id, (error, docs = {}) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
ProjectEntityHandler.getAllFiles project_id, (error, files = {}) ->
|
|
|
|
return callback(error) if error?
|
2014-02-12 10:23:40 +00:00
|
|
|
|
2014-05-06 11:54:26 +00:00
|
|
|
resources = []
|
|
|
|
rootResourcePath = null
|
2014-06-01 14:43:52 +00:00
|
|
|
rootResourcePathOverride = null
|
2014-02-12 10:23:40 +00:00
|
|
|
|
2014-05-06 11:54:26 +00:00
|
|
|
for path, doc of docs
|
|
|
|
path = path.replace(/^\//, "") # Remove leading /
|
|
|
|
resources.push
|
|
|
|
path: path
|
|
|
|
content: doc.lines.join("\n")
|
|
|
|
if project.rootDoc_id? and doc._id.toString() == project.rootDoc_id.toString()
|
|
|
|
rootResourcePath = path
|
2014-06-01 14:43:52 +00:00
|
|
|
if settingsOverride.rootDoc_id? and doc._id.toString() == settingsOverride.rootDoc_id.toString()
|
|
|
|
rootResourcePathOverride = path
|
|
|
|
|
|
|
|
rootResourcePath = rootResourcePathOverride if rootResourcePathOverride?
|
2014-02-12 10:23:40 +00:00
|
|
|
|
2014-05-06 11:54:26 +00:00
|
|
|
for path, file of files
|
|
|
|
path = path.replace(/^\//, "") # Remove leading /
|
|
|
|
resources.push
|
|
|
|
path: path
|
|
|
|
url: "#{Settings.apis.filestore.url}/project/#{project._id}/file/#{file._id}"
|
|
|
|
modified: file.created?.getTime()
|
2014-02-12 10:23:40 +00:00
|
|
|
|
2014-05-06 11:54:26 +00:00
|
|
|
if !rootResourcePath?
|
|
|
|
callback new Error("no root document exists")
|
|
|
|
else
|
|
|
|
callback null, {
|
|
|
|
compile:
|
|
|
|
options:
|
|
|
|
compiler: project.compiler
|
|
|
|
rootResourcePath: rootResourcePath
|
|
|
|
resources: resources
|
|
|
|
}
|
2014-02-12 10:23:40 +00:00
|
|
|
|