2014-02-12 05:23:40 -05:00
|
|
|
Path = require "path"
|
|
|
|
async = require "async"
|
|
|
|
Settings = require "settings-sharelatex"
|
|
|
|
request = require('request')
|
|
|
|
Project = require("../../models/Project").Project
|
2014-05-06 07:54:26 -04:00
|
|
|
ProjectEntityHandler = require("../Project/ProjectEntityHandler")
|
2014-02-12 05:23:40 -05:00
|
|
|
logger = require "logger-sharelatex"
|
|
|
|
url = require("url")
|
2016-04-20 10:06:39 -04:00
|
|
|
ClsiCookieManager = require("./ClsiCookieManager")
|
2016-04-19 11:48:51 -04:00
|
|
|
|
2014-02-12 05:23:40 -05:00
|
|
|
|
|
|
|
module.exports = ClsiManager =
|
2016-04-19 11:48:51 -04:00
|
|
|
|
2014-12-01 05:27:58 -05:00
|
|
|
sendRequest: (project_id, options = {}, callback = (error, success) ->) ->
|
|
|
|
ClsiManager._buildRequest project_id, options, (error, req) ->
|
2014-02-12 05:23:40 -05:00
|
|
|
return callback(error) if error?
|
2014-05-06 07:54:26 -04:00
|
|
|
logger.log project_id: project_id, "sending compile to CLSI"
|
2014-12-01 05:27:58 -05:00
|
|
|
ClsiManager._postToClsi project_id, req, options.compileGroup, (error, response) ->
|
2016-04-20 10:06:39 -04:00
|
|
|
if error?
|
|
|
|
logger.err err:error, project_id:project_id, "error sending request to clsi"
|
|
|
|
return callback(error)
|
2014-05-06 07:54:26 -04:00
|
|
|
logger.log project_id: project_id, response: response, "received compile response from CLSI"
|
2016-05-18 05:09:22 -04:00
|
|
|
ClsiCookieManager._getServerId project_id, (err, clsiServerId)->
|
|
|
|
if err?
|
|
|
|
logger.err err:err, project_id:project_id, "error getting server id"
|
|
|
|
return callback(err)
|
|
|
|
outputFiles = ClsiManager._parseOutputFiles(project_id, response?.compile?.outputFiles, clsiServerId)
|
2016-05-18 07:50:50 -04:00
|
|
|
callback(null, response?.compile?.status, outputFiles, clsiServerId)
|
2014-02-12 05:23:40 -05:00
|
|
|
|
2014-12-01 07:19:01 -05:00
|
|
|
deleteAuxFiles: (project_id, options, callback = (error) ->) ->
|
|
|
|
compilerUrl = @_getCompilerUrl(options?.compileGroup)
|
2016-04-19 11:48:51 -04:00
|
|
|
opts =
|
|
|
|
url:"#{compilerUrl}/project/#{project_id}"
|
|
|
|
method:"DELETE"
|
|
|
|
ClsiManager._makeRequest project_id, opts, callback
|
2014-05-19 11:10:41 -04:00
|
|
|
|
2016-04-20 10:06:39 -04:00
|
|
|
|
|
|
|
_makeRequest: (project_id, opts, callback)->
|
|
|
|
ClsiCookieManager.getCookieJar project_id, (err, jar)->
|
|
|
|
if err?
|
|
|
|
logger.err err:err, "error getting cookie jar for clsi request"
|
|
|
|
return callback(err)
|
|
|
|
opts.jar = jar
|
2016-04-20 11:17:06 -04:00
|
|
|
request opts, (err, response, body)->
|
|
|
|
if err?
|
|
|
|
logger.err err:err, project_id:project_id, url:opts?.url, "error making request to clsi"
|
|
|
|
return callback(err)
|
2016-04-20 12:00:17 -04:00
|
|
|
ClsiCookieManager.setServerId project_id, response, (err)->
|
|
|
|
if err?
|
|
|
|
logger.warn err:err, project_id:project_id, "error setting server id"
|
|
|
|
|
|
|
|
return callback err, response, body
|
2016-04-20 10:06:39 -04:00
|
|
|
|
|
|
|
|
2014-12-01 07:19:01 -05:00
|
|
|
_getCompilerUrl: (compileGroup) ->
|
2016-05-20 07:46:14 -04:00
|
|
|
return Settings.apis.clsi.url
|
2014-12-01 07:19:01 -05:00
|
|
|
|
|
|
|
_postToClsi: (project_id, req, compileGroup, callback = (error, response) ->) ->
|
2016-05-20 07:46:14 -04:00
|
|
|
compilerUrl = Settings.apis.clsi.url
|
2016-04-19 11:48:51 -04:00
|
|
|
opts =
|
2014-10-16 12:52:21 -04:00
|
|
|
url: "#{compilerUrl}/project/#{project_id}/compile"
|
2014-02-12 05:23:40 -05:00
|
|
|
json: req
|
2016-04-19 11:48:51 -04:00
|
|
|
method: "POST"
|
|
|
|
ClsiManager._makeRequest project_id, opts, (error, response, body) ->
|
2014-05-19 08:46:22 -04:00
|
|
|
return callback(error) if error?
|
|
|
|
if 200 <= response.statusCode < 300
|
|
|
|
callback null, body
|
2014-11-27 10:42:37 -05:00
|
|
|
else if response.statusCode == 413
|
2014-11-27 11:22:39 -05:00
|
|
|
callback null, compile:status:"project-too-large"
|
2014-05-19 08:46:22 -04:00
|
|
|
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 05:23:40 -05:00
|
|
|
|
2016-05-18 05:09:22 -04:00
|
|
|
_parseOutputFiles: (project_id, rawOutputFiles = [], clsiServer) ->
|
|
|
|
# console.log rawOutputFiles
|
2014-02-12 05:23:40 -05:00
|
|
|
outputFiles = []
|
|
|
|
for file in rawOutputFiles
|
2016-05-18 05:09:22 -04:00
|
|
|
console.log path
|
|
|
|
path = url.parse(file.url).path
|
|
|
|
path = path.replace("/project/#{project_id}/output/", "")
|
2014-02-12 05:23:40 -05:00
|
|
|
outputFiles.push
|
2016-05-18 05:09:22 -04:00
|
|
|
path: path
|
2014-02-12 05:23:40 -05:00
|
|
|
type: file.type
|
2015-02-25 12:06:27 -05:00
|
|
|
build: file.build
|
2014-02-12 05:23:40 -05:00
|
|
|
return outputFiles
|
|
|
|
|
|
|
|
VALID_COMPILERS: ["pdflatex", "latex", "xelatex", "lualatex"]
|
2016-02-02 09:50:48 -05:00
|
|
|
_buildRequest: (project_id, options={}, callback = (error, request) ->) ->
|
2016-01-14 11:35:42 -05:00
|
|
|
Project.findById project_id, {compiler: 1, rootDoc_id: 1, imageName: 1}, (error, project) ->
|
2014-05-06 07:54:26 -04:00
|
|
|
return callback(error) if error?
|
|
|
|
return callback(new Errors.NotFoundError("project does not exist: #{project_id}")) if !project?
|
2014-02-12 05:23:40 -05:00
|
|
|
|
2014-05-06 07:54:26 -04:00
|
|
|
if project.compiler not in ClsiManager.VALID_COMPILERS
|
|
|
|
project.compiler = "pdflatex"
|
2014-02-12 05:23:40 -05:00
|
|
|
|
2014-05-06 07:54:26 -04: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 05:23:40 -05:00
|
|
|
|
2014-05-06 07:54:26 -04:00
|
|
|
resources = []
|
|
|
|
rootResourcePath = null
|
2014-06-01 10:43:52 -04:00
|
|
|
rootResourcePathOverride = null
|
2014-02-12 05:23:40 -05:00
|
|
|
|
2014-05-06 07:54:26 -04: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
|
2016-02-02 09:50:48 -05:00
|
|
|
if options.rootDoc_id? and doc._id.toString() == options.rootDoc_id.toString()
|
2014-06-01 10:43:52 -04:00
|
|
|
rootResourcePathOverride = path
|
|
|
|
|
|
|
|
rootResourcePath = rootResourcePathOverride if rootResourcePathOverride?
|
2016-03-21 09:28:53 -04:00
|
|
|
if !rootResourcePath?
|
|
|
|
logger.warn {project_id}, "no root document found, setting to main.tex"
|
|
|
|
rootResourcePath = "main.tex"
|
2014-02-12 05:23:40 -05:00
|
|
|
|
2014-05-06 07:54:26 -04: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 05:23:40 -05:00
|
|
|
|
2016-03-21 09:28:53 -04:00
|
|
|
callback null, {
|
|
|
|
compile:
|
|
|
|
options:
|
|
|
|
compiler: project.compiler
|
|
|
|
timeout: options.timeout
|
|
|
|
imageName: project.imageName
|
|
|
|
draft: !!options.draft
|
|
|
|
rootResourcePath: rootResourcePath
|
|
|
|
resources: resources
|
|
|
|
}
|
2015-09-10 11:41:48 -04:00
|
|
|
|
2015-09-11 08:53:06 -04:00
|
|
|
wordCount: (project_id, file, options, callback = (error, response) ->) ->
|
|
|
|
ClsiManager._buildRequest project_id, options, (error, req) ->
|
|
|
|
compilerUrl = ClsiManager._getCompilerUrl(options?.compileGroup)
|
2015-10-19 17:14:52 -04:00
|
|
|
filename = file || req?.compile?.rootResourcePath
|
2016-01-19 09:17:01 -05:00
|
|
|
wordcount_url = "#{compilerUrl}/project/#{project_id}/wordcount?file=#{encodeURIComponent(filename)}"
|
|
|
|
if req.compile.options.imageName?
|
|
|
|
wordcount_url += "&image=#{encodeURIComponent(req.compile.options.imageName)}"
|
2016-04-19 11:48:51 -04:00
|
|
|
opts =
|
2016-01-19 09:17:01 -05:00
|
|
|
url: wordcount_url
|
2016-04-19 11:48:51 -04:00
|
|
|
method: "GET"
|
|
|
|
ClsiManager._makeRequest project_id, opts, (error, response, body) ->
|
2015-09-11 08:53:06 -04: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
|
|
|
|
|