overleaf/services/clsi/app/coffee/CompileController.coffee

120 lines
4.1 KiB
CoffeeScript
Raw Normal View History

2014-02-12 12:27:43 -05:00
RequestParser = require "./RequestParser"
CompileManager = require "./CompileManager"
Settings = require "settings-sharelatex"
Metrics = require "./Metrics"
ProjectPersistenceManager = require "./ProjectPersistenceManager"
logger = require "logger-sharelatex"
2017-08-03 10:56:59 -04:00
Errors = require "./Errors"
2014-02-12 12:27:43 -05:00
module.exports = CompileController =
compile: (req, res, next = (error) ->) ->
timer = new Metrics.Timer("compile-request")
RequestParser.parse req.body, (error, request) ->
return next(error) if error?
request.project_id = req.params.project_id
2016-05-27 10:29:26 -04:00
request.user_id = req.params.user_id if req.params.user_id?
2014-02-12 12:27:43 -05:00
ProjectPersistenceManager.markProjectAsJustAccessed request.project_id, (error) ->
return next(error) if error?
2017-09-22 11:19:33 -04:00
CompileManager.doCompileWithLock request, (error, outputFiles = []) ->
if error instanceof Errors.AlreadyCompilingError
2017-09-25 11:06:45 -04:00
code = 423 # Http 423 Locked
2017-09-22 11:19:33 -04:00
status = "compile-in-progress"
else if error instanceof Errors.FilesOutOfSyncError
2017-08-01 09:35:55 -04:00
code = 409 # Http 409 Conflict
status = "retry"
else if error?.terminated
2016-07-14 09:47:36 -04:00
status = "terminated"
else if error?.validate
status = "validation-#{error.validate}"
else if error?.timedout
status = "timedout"
logger.log err: error, project_id: request.project_id, "timeout running compile"
2016-07-14 09:47:36 -04:00
else if error?
status = "error"
code = 500
logger.warn err: error, project_id: request.project_id, "error running compile"
2014-02-12 12:27:43 -05:00
else
status = "failure"
for file in outputFiles
if file.path?.match(/output\.pdf$/)
2014-02-12 12:27:43 -05:00
status = "success"
if status == "failure"
logger.warn project_id: request.project_id, outputFiles:outputFiles, "project failed to compile successfully, no output.pdf generated"
2014-02-12 12:27:43 -05:00
# log an error if any core files are found
for file in outputFiles
if file.path is "core"
logger.error project_id:request.project_id, req:req, outputFiles:outputFiles, "core file found in output"
2014-02-12 12:27:43 -05:00
if error?
outputFiles = error.outputFiles || []
2014-02-12 12:27:43 -05:00
timer.done()
res.status(code or 200).send {
2014-02-12 12:27:43 -05:00
compile:
status: status
2014-05-19 07:18:57 -04:00
error: error?.message or error
2014-02-12 12:27:43 -05:00
outputFiles: outputFiles.map (file) ->
url:
"#{Settings.apis.clsi.url}/project/#{request.project_id}" +
2016-05-27 10:29:26 -04:00
(if request.user_id? then "/user/#{request.user_id}" else "") +
(if file.build? then "/build/#{file.build}" else "") +
"/output/#{file.path}"
path: file.path
2014-02-12 12:27:43 -05:00
type: file.type
build: file.build
2014-02-12 12:27:43 -05:00
}
2016-07-14 09:47:36 -04:00
stopCompile: (req, res, next) ->
2016-07-18 06:05:45 -04:00
{project_id, user_id} = req.params
2016-07-14 09:47:36 -04:00
CompileManager.stopCompile project_id, user_id, (error) ->
return next(error) if error?
res.sendStatus(204)
2014-02-12 12:27:43 -05:00
clearCache: (req, res, next = (error) ->) ->
ProjectPersistenceManager.clearProject req.params.project_id, req.params.user_id, (error) ->
2014-02-12 12:27:43 -05:00
return next(error) if error?
res.sendStatus(204) # No content
2014-04-08 10:18:56 -04:00
syncFromCode: (req, res, next = (error) ->) ->
file = req.query.file
line = parseInt(req.query.line, 10)
column = parseInt(req.query.column, 10)
project_id = req.params.project_id
2016-05-27 10:29:26 -04:00
user_id = req.params.user_id
CompileManager.syncFromCode project_id, user_id, file, line, column, (error, pdfPositions) ->
2014-04-08 10:18:56 -04:00
return next(error) if error?
res.json {
2014-04-08 10:18:56 -04:00
pdf: pdfPositions
}
syncFromPdf: (req, res, next = (error) ->) ->
page = parseInt(req.query.page, 10)
h = parseFloat(req.query.h)
v = parseFloat(req.query.v)
project_id = req.params.project_id
2016-05-27 10:29:26 -04:00
user_id = req.params.user_id
CompileManager.syncFromPdf project_id, user_id, page, h, v, (error, codePositions) ->
2014-04-08 10:18:56 -04:00
return next(error) if error?
res.json {
2014-04-08 10:18:56 -04:00
code: codePositions
}
2015-06-08 17:35:24 -04:00
wordcount: (req, res, next = (error) ->) ->
file = req.query.file || "main.tex"
2015-06-08 17:35:24 -04:00
project_id = req.params.project_id
2016-05-27 10:29:26 -04:00
user_id = req.params.user_id
image = req.query.image
logger.log {image, file, project_id}, "word count request"
2015-06-08 17:35:24 -04:00
2016-05-27 10:29:26 -04:00
CompileManager.wordcount project_id, user_id, file, image, (error, result) ->
2015-06-08 17:35:24 -04:00
return next(error) if error?
res.json {
2015-06-08 17:35:24 -04:00
texcount: result
}
status: (req, res, next = (error)-> )->
res.send("OK")