2020-02-19 06:14:28 -05:00
|
|
|
/* eslint-disable
|
|
|
|
camelcase,
|
|
|
|
no-unused-vars,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
2020-02-19 06:14:14 -05:00
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS101: Remove unnecessary use of Array.from
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* DS207: Consider shorter variations of null checks
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
2020-02-19 06:14:37 -05:00
|
|
|
let CompileController
|
|
|
|
const RequestParser = require('./RequestParser')
|
|
|
|
const CompileManager = require('./CompileManager')
|
2021-07-12 12:47:21 -04:00
|
|
|
const Settings = require('@overleaf/settings')
|
2020-02-19 06:14:37 -05:00
|
|
|
const Metrics = require('./Metrics')
|
|
|
|
const ProjectPersistenceManager = require('./ProjectPersistenceManager')
|
2022-03-01 10:09:36 -05:00
|
|
|
const logger = require('@overleaf/logger')
|
2020-02-19 06:14:37 -05:00
|
|
|
const Errors = require('./Errors')
|
2014-02-12 12:27:43 -05:00
|
|
|
|
2021-03-30 08:22:11 -04:00
|
|
|
function isImageNameAllowed(imageName) {
|
|
|
|
const ALLOWED_IMAGES =
|
|
|
|
Settings.clsi && Settings.clsi.docker && Settings.clsi.docker.allowedImages
|
|
|
|
return !ALLOWED_IMAGES || ALLOWED_IMAGES.includes(imageName)
|
|
|
|
}
|
|
|
|
|
2020-02-19 06:14:37 -05:00
|
|
|
module.exports = CompileController = {
|
2022-03-02 07:59:57 -05:00
|
|
|
lastSuccessfulCompile: 0,
|
|
|
|
|
2020-02-19 06:14:37 -05:00
|
|
|
compile(req, res, next) {
|
|
|
|
if (next == null) {
|
2021-10-27 05:49:18 -04:00
|
|
|
next = function () {}
|
2020-02-19 06:14:37 -05:00
|
|
|
}
|
|
|
|
const timer = new Metrics.Timer('compile-request')
|
2020-08-10 12:01:11 -04:00
|
|
|
return RequestParser.parse(req.body, function (error, request) {
|
2020-02-19 06:14:37 -05:00
|
|
|
if (error != null) {
|
|
|
|
return next(error)
|
|
|
|
}
|
2022-03-01 10:09:55 -05:00
|
|
|
timer.opts = request.metricsOpts
|
2020-02-19 06:14:37 -05:00
|
|
|
request.project_id = req.params.project_id
|
|
|
|
if (req.params.user_id != null) {
|
|
|
|
request.user_id = req.params.user_id
|
|
|
|
}
|
|
|
|
return ProjectPersistenceManager.markProjectAsJustAccessed(
|
|
|
|
request.project_id,
|
2020-08-10 12:01:11 -04:00
|
|
|
function (error) {
|
2020-02-19 06:14:37 -05:00
|
|
|
if (error != null) {
|
|
|
|
return next(error)
|
|
|
|
}
|
2021-07-13 07:04:48 -04:00
|
|
|
return CompileManager.doCompileWithLock(
|
|
|
|
request,
|
|
|
|
function (error, outputFiles, stats, timings) {
|
|
|
|
let code, status
|
|
|
|
if (outputFiles == null) {
|
|
|
|
outputFiles = []
|
2020-02-19 06:14:37 -05:00
|
|
|
}
|
2021-07-13 07:04:48 -04:00
|
|
|
if (error instanceof Errors.AlreadyCompilingError) {
|
|
|
|
code = 423 // Http 423 Locked
|
|
|
|
status = 'compile-in-progress'
|
|
|
|
} else if (error instanceof Errors.FilesOutOfSyncError) {
|
|
|
|
code = 409 // Http 409 Conflict
|
|
|
|
status = 'retry'
|
|
|
|
} else if (error && error.code === 'EPIPE') {
|
|
|
|
// docker returns EPIPE when shutting down
|
|
|
|
code = 503 // send 503 Unavailable response
|
|
|
|
status = 'unavailable'
|
|
|
|
} else if (error != null ? error.terminated : undefined) {
|
|
|
|
status = 'terminated'
|
|
|
|
} else if (error != null ? error.validate : undefined) {
|
|
|
|
status = `validation-${error.validate}`
|
|
|
|
} else if (error != null ? error.timedout : undefined) {
|
|
|
|
status = 'timedout'
|
|
|
|
logger.log(
|
|
|
|
{ err: error, project_id: request.project_id },
|
|
|
|
'timeout running compile'
|
|
|
|
)
|
|
|
|
} else if (error != null) {
|
|
|
|
status = 'error'
|
|
|
|
code = 500
|
2020-02-19 06:14:37 -05:00
|
|
|
logger.warn(
|
2021-07-13 07:04:48 -04:00
|
|
|
{ err: error, project_id: request.project_id },
|
|
|
|
'error running compile'
|
2020-02-19 06:14:37 -05:00
|
|
|
)
|
2021-07-13 07:04:48 -04:00
|
|
|
} else {
|
|
|
|
let file
|
|
|
|
status = 'failure'
|
|
|
|
for (file of Array.from(outputFiles)) {
|
|
|
|
if (file.path === 'output.pdf' && file.size > 0) {
|
|
|
|
status = 'success'
|
2022-03-02 07:59:57 -05:00
|
|
|
CompileController.lastSuccessfulCompile = Date.now()
|
2021-07-13 07:04:48 -04:00
|
|
|
}
|
|
|
|
}
|
2014-02-12 12:27:43 -05:00
|
|
|
|
2021-07-13 07:04:48 -04:00
|
|
|
if (status === 'failure') {
|
|
|
|
logger.warn(
|
|
|
|
{ project_id: request.project_id, outputFiles },
|
|
|
|
'project failed to compile successfully, no output.pdf generated'
|
2020-02-19 06:14:37 -05:00
|
|
|
)
|
|
|
|
}
|
2016-07-14 09:47:36 -04:00
|
|
|
|
2021-07-13 07:04:48 -04:00
|
|
|
// log an error if any core files are found
|
|
|
|
for (file of Array.from(outputFiles)) {
|
|
|
|
if (file.path === 'core') {
|
|
|
|
logger.error(
|
|
|
|
{ project_id: request.project_id, req, outputFiles },
|
|
|
|
'core file found in output'
|
|
|
|
)
|
2021-05-13 09:07:54 -04:00
|
|
|
}
|
2021-07-13 07:04:48 -04:00
|
|
|
}
|
2020-02-19 06:14:37 -05:00
|
|
|
}
|
2021-07-13 07:04:48 -04:00
|
|
|
|
|
|
|
if (error != null) {
|
|
|
|
outputFiles = error.outputFiles || []
|
|
|
|
}
|
|
|
|
|
|
|
|
timer.done()
|
|
|
|
return res.status(code || 200).send({
|
|
|
|
compile: {
|
|
|
|
status,
|
|
|
|
error: (error != null ? error.message : undefined) || error,
|
|
|
|
stats,
|
|
|
|
timings,
|
|
|
|
outputFiles: outputFiles.map(file => {
|
|
|
|
return {
|
|
|
|
url:
|
|
|
|
`${Settings.apis.clsi.url}/project/${request.project_id}` +
|
|
|
|
(request.user_id != null
|
|
|
|
? `/user/${request.user_id}`
|
|
|
|
: '') +
|
|
|
|
(file.build != null ? `/build/${file.build}` : '') +
|
|
|
|
`/output/${file.path}`,
|
|
|
|
...file,
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
)
|
2020-02-19 06:14:37 -05:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
},
|
2020-02-19 06:14:14 -05:00
|
|
|
|
2020-02-19 06:14:37 -05:00
|
|
|
stopCompile(req, res, next) {
|
|
|
|
const { project_id, user_id } = req.params
|
2020-08-10 12:01:11 -04:00
|
|
|
return CompileManager.stopCompile(project_id, user_id, function (error) {
|
2020-02-19 06:14:37 -05:00
|
|
|
if (error != null) {
|
|
|
|
return next(error)
|
|
|
|
}
|
|
|
|
return res.sendStatus(204)
|
|
|
|
})
|
|
|
|
},
|
2016-07-14 09:47:36 -04:00
|
|
|
|
2020-02-19 06:14:37 -05:00
|
|
|
clearCache(req, res, next) {
|
|
|
|
if (next == null) {
|
2021-10-27 05:49:18 -04:00
|
|
|
next = function () {}
|
2020-02-19 06:14:37 -05:00
|
|
|
}
|
|
|
|
return ProjectPersistenceManager.clearProject(
|
|
|
|
req.params.project_id,
|
|
|
|
req.params.user_id,
|
2020-08-10 12:01:11 -04:00
|
|
|
function (error) {
|
2020-02-19 06:14:37 -05:00
|
|
|
if (error != null) {
|
|
|
|
return next(error)
|
|
|
|
}
|
|
|
|
return res.sendStatus(204)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}, // No content
|
2014-04-08 10:18:56 -04:00
|
|
|
|
2020-02-19 06:14:37 -05:00
|
|
|
syncFromCode(req, res, next) {
|
|
|
|
if (next == null) {
|
2021-10-27 05:49:18 -04:00
|
|
|
next = function () {}
|
2020-02-19 06:14:37 -05:00
|
|
|
}
|
|
|
|
const { file } = req.query
|
|
|
|
const line = parseInt(req.query.line, 10)
|
|
|
|
const column = parseInt(req.query.column, 10)
|
2021-03-30 08:22:11 -04:00
|
|
|
const { imageName } = req.query
|
2020-02-19 06:14:37 -05:00
|
|
|
const { project_id } = req.params
|
|
|
|
const { user_id } = req.params
|
2021-03-30 08:22:11 -04:00
|
|
|
|
|
|
|
if (imageName && !isImageNameAllowed(imageName)) {
|
|
|
|
return res.status(400).send('invalid image')
|
|
|
|
}
|
|
|
|
|
2020-02-19 06:14:37 -05:00
|
|
|
return CompileManager.syncFromCode(
|
|
|
|
project_id,
|
|
|
|
user_id,
|
|
|
|
file,
|
|
|
|
line,
|
|
|
|
column,
|
2021-03-30 08:22:11 -04:00
|
|
|
imageName,
|
2020-08-10 12:01:11 -04:00
|
|
|
function (error, pdfPositions) {
|
2020-02-19 06:14:37 -05:00
|
|
|
if (error != null) {
|
|
|
|
return next(error)
|
|
|
|
}
|
|
|
|
return res.json({
|
2021-07-13 07:04:48 -04:00
|
|
|
pdf: pdfPositions,
|
2020-02-19 06:14:37 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
2014-04-08 10:18:56 -04:00
|
|
|
|
2020-02-19 06:14:37 -05:00
|
|
|
syncFromPdf(req, res, next) {
|
|
|
|
if (next == null) {
|
2021-10-27 05:49:18 -04:00
|
|
|
next = function () {}
|
2020-02-19 06:14:37 -05:00
|
|
|
}
|
|
|
|
const page = parseInt(req.query.page, 10)
|
|
|
|
const h = parseFloat(req.query.h)
|
|
|
|
const v = parseFloat(req.query.v)
|
2021-03-30 08:22:11 -04:00
|
|
|
const { imageName } = req.query
|
2020-02-19 06:14:37 -05:00
|
|
|
const { project_id } = req.params
|
|
|
|
const { user_id } = req.params
|
2021-03-30 08:22:11 -04:00
|
|
|
|
|
|
|
if (imageName && !isImageNameAllowed(imageName)) {
|
|
|
|
return res.status(400).send('invalid image')
|
|
|
|
}
|
2020-08-10 12:01:11 -04:00
|
|
|
return CompileManager.syncFromPdf(
|
|
|
|
project_id,
|
|
|
|
user_id,
|
|
|
|
page,
|
|
|
|
h,
|
|
|
|
v,
|
2021-03-30 08:22:11 -04:00
|
|
|
imageName,
|
2020-08-10 12:01:11 -04:00
|
|
|
function (error, codePositions) {
|
|
|
|
if (error != null) {
|
|
|
|
return next(error)
|
|
|
|
}
|
|
|
|
return res.json({
|
2021-07-13 07:04:48 -04:00
|
|
|
code: codePositions,
|
2020-08-10 12:01:11 -04:00
|
|
|
})
|
2020-02-19 06:14:37 -05:00
|
|
|
}
|
2020-08-10 12:01:11 -04:00
|
|
|
)
|
2020-02-19 06:14:37 -05:00
|
|
|
},
|
2015-06-08 17:35:24 -04:00
|
|
|
|
2020-02-19 06:14:37 -05:00
|
|
|
wordcount(req, res, next) {
|
|
|
|
if (next == null) {
|
2021-10-27 05:49:18 -04:00
|
|
|
next = function () {}
|
2020-02-19 06:14:37 -05:00
|
|
|
}
|
|
|
|
const file = req.query.file || 'main.tex'
|
|
|
|
const { project_id } = req.params
|
|
|
|
const { user_id } = req.params
|
|
|
|
const { image } = req.query
|
2021-03-30 08:22:11 -04:00
|
|
|
if (image && !isImageNameAllowed(image)) {
|
2020-06-26 08:17:45 -04:00
|
|
|
return res.status(400).send('invalid image')
|
|
|
|
}
|
2020-02-19 06:14:37 -05:00
|
|
|
logger.log({ image, file, project_id }, 'word count request')
|
2015-06-08 17:35:24 -04:00
|
|
|
|
2021-07-13 07:04:48 -04:00
|
|
|
return CompileManager.wordcount(
|
|
|
|
project_id,
|
|
|
|
user_id,
|
|
|
|
file,
|
|
|
|
image,
|
|
|
|
function (error, result) {
|
|
|
|
if (error != null) {
|
|
|
|
return next(error)
|
|
|
|
}
|
|
|
|
return res.json({
|
|
|
|
texcount: result,
|
|
|
|
})
|
2020-02-19 06:14:37 -05:00
|
|
|
}
|
2021-07-13 07:04:48 -04:00
|
|
|
)
|
2020-02-19 06:14:37 -05:00
|
|
|
},
|
2016-04-20 10:38:05 -04:00
|
|
|
|
2020-02-19 06:14:37 -05:00
|
|
|
status(req, res, next) {
|
|
|
|
if (next == null) {
|
2021-10-27 05:49:18 -04:00
|
|
|
next = function () {}
|
2020-02-19 06:14:37 -05:00
|
|
|
}
|
|
|
|
return res.send('OK')
|
2021-07-13 07:04:48 -04:00
|
|
|
},
|
2020-02-19 06:14:37 -05:00
|
|
|
}
|