2020-02-19 06:16:07 -05:00
|
|
|
/* eslint-disable
|
|
|
|
camelcase,
|
|
|
|
handle-callback-err,
|
|
|
|
no-unused-vars,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
2020-02-19 06:16:00 -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:16:14 -05:00
|
|
|
let Client
|
|
|
|
const request = require('request')
|
|
|
|
const fs = require('fs')
|
2021-07-12 12:47:21 -04:00
|
|
|
const Settings = require('@overleaf/settings')
|
2014-02-12 12:27:43 -05:00
|
|
|
|
2020-02-19 06:16:14 -05:00
|
|
|
const host = 'localhost'
|
2014-02-12 12:27:43 -05:00
|
|
|
|
2020-02-19 06:16:14 -05:00
|
|
|
module.exports = Client = {
|
|
|
|
host: Settings.apis.clsi.url,
|
2014-02-12 12:27:43 -05:00
|
|
|
|
2020-02-19 06:16:14 -05:00
|
|
|
randomId() {
|
2020-08-10 12:01:11 -04:00
|
|
|
return Math.random().toString(16).slice(2)
|
2020-02-19 06:16:14 -05:00
|
|
|
},
|
2014-02-12 12:27:43 -05:00
|
|
|
|
2020-02-19 06:16:14 -05:00
|
|
|
compile(project_id, data, callback) {
|
|
|
|
if (callback == null) {
|
2020-08-10 12:01:11 -04:00
|
|
|
callback = function (error, res, body) {}
|
2020-02-19 06:16:14 -05:00
|
|
|
}
|
2021-05-13 09:07:54 -04:00
|
|
|
if (data) {
|
|
|
|
// Enable pdf caching unless disabled explicitly.
|
|
|
|
data.options = Object.assign({}, { enablePdfCaching: true }, data.options)
|
|
|
|
}
|
2020-02-19 06:16:14 -05:00
|
|
|
return request.post(
|
|
|
|
{
|
|
|
|
url: `${this.host}/project/${project_id}/compile`,
|
|
|
|
json: {
|
2021-07-13 07:04:48 -04:00
|
|
|
compile: data,
|
|
|
|
},
|
2020-02-19 06:16:14 -05:00
|
|
|
},
|
|
|
|
callback
|
|
|
|
)
|
|
|
|
},
|
2014-02-12 12:27:43 -05:00
|
|
|
|
2020-02-19 06:16:14 -05:00
|
|
|
clearCache(project_id, callback) {
|
|
|
|
if (callback == null) {
|
2020-08-10 12:01:11 -04:00
|
|
|
callback = function (error, res, body) {}
|
2020-02-19 06:16:14 -05:00
|
|
|
}
|
|
|
|
return request.del(`${this.host}/project/${project_id}`, callback)
|
|
|
|
},
|
2014-02-12 12:27:43 -05:00
|
|
|
|
2020-02-19 06:16:14 -05:00
|
|
|
getOutputFile(response, type) {
|
|
|
|
for (const file of Array.from(response.compile.outputFiles)) {
|
|
|
|
if (file.type === type && file.url.match(`output.${type}`)) {
|
|
|
|
return file
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null
|
|
|
|
},
|
2014-02-12 12:27:43 -05:00
|
|
|
|
2020-02-19 06:16:14 -05:00
|
|
|
runServer(port, directory) {
|
|
|
|
const express = require('express')
|
|
|
|
const app = express()
|
|
|
|
app.use(express.static(directory))
|
|
|
|
console.log('starting test server on', port, host)
|
2021-07-13 07:04:48 -04:00
|
|
|
return app.listen(port, host).on('error', error => {
|
2020-02-19 06:16:14 -05:00
|
|
|
console.error('error starting server:', error.message)
|
|
|
|
return process.exit(1)
|
|
|
|
})
|
|
|
|
},
|
2017-10-20 10:16:35 -04:00
|
|
|
|
2020-02-19 06:16:14 -05:00
|
|
|
syncFromCode(project_id, file, line, column, callback) {
|
2021-03-30 08:22:11 -04:00
|
|
|
Client.syncFromCodeWithImage(project_id, file, line, column, '', callback)
|
|
|
|
},
|
|
|
|
|
|
|
|
syncFromCodeWithImage(project_id, file, line, column, imageName, callback) {
|
2020-02-19 06:16:14 -05:00
|
|
|
if (callback == null) {
|
2020-08-10 12:01:11 -04:00
|
|
|
callback = function (error, pdfPositions) {}
|
2020-02-19 06:16:14 -05:00
|
|
|
}
|
|
|
|
return request.get(
|
|
|
|
{
|
|
|
|
url: `${this.host}/project/${project_id}/sync/code`,
|
|
|
|
qs: {
|
2021-03-30 08:22:11 -04:00
|
|
|
imageName,
|
2020-02-19 06:16:14 -05:00
|
|
|
file,
|
|
|
|
line,
|
2021-07-13 07:04:48 -04:00
|
|
|
column,
|
2020-06-12 10:15:51 -04:00
|
|
|
},
|
2021-07-13 07:04:48 -04:00
|
|
|
json: true,
|
2020-02-19 06:16:14 -05:00
|
|
|
},
|
|
|
|
(error, response, body) => {
|
|
|
|
if (error != null) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
2021-03-30 08:22:11 -04:00
|
|
|
if (response.statusCode !== 200) {
|
|
|
|
return callback(new Error(`statusCode=${response.statusCode}`), body)
|
|
|
|
}
|
2020-06-12 10:15:51 -04:00
|
|
|
return callback(null, body)
|
2020-02-19 06:16:14 -05:00
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
2014-02-12 12:27:43 -05:00
|
|
|
|
2020-02-19 06:16:14 -05:00
|
|
|
syncFromPdf(project_id, page, h, v, callback) {
|
2021-03-30 08:22:11 -04:00
|
|
|
Client.syncFromPdfWithImage(project_id, page, h, v, '', callback)
|
|
|
|
},
|
|
|
|
|
|
|
|
syncFromPdfWithImage(project_id, page, h, v, imageName, callback) {
|
2020-02-19 06:16:14 -05:00
|
|
|
if (callback == null) {
|
2020-08-10 12:01:11 -04:00
|
|
|
callback = function (error, pdfPositions) {}
|
2020-02-19 06:16:14 -05:00
|
|
|
}
|
|
|
|
return request.get(
|
|
|
|
{
|
|
|
|
url: `${this.host}/project/${project_id}/sync/pdf`,
|
|
|
|
qs: {
|
2021-03-30 08:22:11 -04:00
|
|
|
imageName,
|
2020-02-19 06:16:14 -05:00
|
|
|
page,
|
|
|
|
h,
|
2021-07-13 07:04:48 -04:00
|
|
|
v,
|
2020-06-12 10:15:51 -04:00
|
|
|
},
|
2021-07-13 07:04:48 -04:00
|
|
|
json: true,
|
2020-02-19 06:16:14 -05:00
|
|
|
},
|
|
|
|
(error, response, body) => {
|
|
|
|
if (error != null) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
2021-03-30 08:22:11 -04:00
|
|
|
if (response.statusCode !== 200) {
|
|
|
|
return callback(new Error(`statusCode=${response.statusCode}`), body)
|
|
|
|
}
|
2020-06-12 10:15:51 -04:00
|
|
|
return callback(null, body)
|
2020-02-19 06:16:14 -05:00
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
2014-04-08 10:18:56 -04:00
|
|
|
|
2020-02-19 06:16:14 -05:00
|
|
|
compileDirectory(project_id, baseDirectory, directory, serverPort, callback) {
|
|
|
|
if (callback == null) {
|
2020-08-10 12:01:11 -04:00
|
|
|
callback = function (error, res, body) {}
|
2020-02-19 06:16:14 -05:00
|
|
|
}
|
|
|
|
const resources = []
|
|
|
|
let entities = fs.readdirSync(`${baseDirectory}/${directory}`)
|
|
|
|
let rootResourcePath = 'main.tex'
|
|
|
|
while (entities.length > 0) {
|
|
|
|
var entity = entities.pop()
|
|
|
|
const stat = fs.statSync(`${baseDirectory}/${directory}/${entity}`)
|
|
|
|
if (stat.isDirectory()) {
|
|
|
|
entities = entities.concat(
|
|
|
|
fs
|
|
|
|
.readdirSync(`${baseDirectory}/${directory}/${entity}`)
|
2021-07-13 07:04:48 -04:00
|
|
|
.map(subEntity => {
|
2020-02-19 06:16:14 -05:00
|
|
|
if (subEntity === 'main.tex') {
|
|
|
|
rootResourcePath = `${entity}/${subEntity}`
|
|
|
|
}
|
|
|
|
return `${entity}/${subEntity}`
|
|
|
|
})
|
|
|
|
)
|
|
|
|
} else if (stat.isFile() && entity !== 'output.pdf') {
|
|
|
|
const extension = entity.split('.').pop()
|
|
|
|
if (
|
|
|
|
[
|
|
|
|
'tex',
|
|
|
|
'bib',
|
|
|
|
'cls',
|
|
|
|
'sty',
|
|
|
|
'pdf_tex',
|
|
|
|
'Rtex',
|
|
|
|
'ist',
|
|
|
|
'md',
|
2021-07-13 07:04:48 -04:00
|
|
|
'Rmd',
|
2020-02-19 06:16:14 -05:00
|
|
|
].indexOf(extension) > -1
|
|
|
|
) {
|
|
|
|
resources.push({
|
|
|
|
path: entity,
|
|
|
|
content: fs
|
|
|
|
.readFileSync(`${baseDirectory}/${directory}/${entity}`)
|
2021-07-13 07:04:48 -04:00
|
|
|
.toString(),
|
2020-02-19 06:16:14 -05:00
|
|
|
})
|
|
|
|
} else if (
|
|
|
|
['eps', 'ttf', 'png', 'jpg', 'pdf', 'jpeg'].indexOf(extension) > -1
|
|
|
|
) {
|
|
|
|
resources.push({
|
|
|
|
path: entity,
|
|
|
|
url: `http://${host}:${serverPort}/${directory}/${entity}`,
|
2021-07-13 07:04:48 -04:00
|
|
|
modified: stat.mtime,
|
2020-02-19 06:16:14 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-04-08 10:18:56 -04:00
|
|
|
|
2020-02-19 06:16:14 -05:00
|
|
|
return fs.readFile(
|
|
|
|
`${baseDirectory}/${directory}/options.json`,
|
|
|
|
(error, body) => {
|
|
|
|
const req = {
|
|
|
|
resources,
|
2021-07-13 07:04:48 -04:00
|
|
|
rootResourcePath,
|
2020-02-19 06:16:14 -05:00
|
|
|
}
|
2014-02-12 12:27:43 -05:00
|
|
|
|
2020-02-19 06:16:14 -05:00
|
|
|
if (error == null) {
|
|
|
|
body = JSON.parse(body)
|
|
|
|
req.options = body
|
|
|
|
}
|
2014-02-12 12:27:43 -05:00
|
|
|
|
2020-02-19 06:16:14 -05:00
|
|
|
return this.compile(project_id, req, callback)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
2014-02-12 12:27:43 -05:00
|
|
|
|
2020-02-19 06:16:14 -05:00
|
|
|
wordcount(project_id, file, callback) {
|
2020-06-26 08:17:45 -04:00
|
|
|
const image = undefined
|
|
|
|
Client.wordcountWithImage(project_id, file, image, callback)
|
|
|
|
},
|
|
|
|
|
|
|
|
wordcountWithImage(project_id, file, image, callback) {
|
2020-02-19 06:16:14 -05:00
|
|
|
if (callback == null) {
|
2020-08-10 12:01:11 -04:00
|
|
|
callback = function (error, pdfPositions) {}
|
2020-02-19 06:16:14 -05:00
|
|
|
}
|
|
|
|
return request.get(
|
|
|
|
{
|
|
|
|
url: `${this.host}/project/${project_id}/wordcount`,
|
|
|
|
qs: {
|
2020-06-26 08:17:45 -04:00
|
|
|
image,
|
2021-07-13 07:04:48 -04:00
|
|
|
file,
|
|
|
|
},
|
2020-02-19 06:16:14 -05:00
|
|
|
},
|
|
|
|
(error, response, body) => {
|
|
|
|
if (error != null) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
2020-06-26 08:17:45 -04:00
|
|
|
if (response.statusCode !== 200) {
|
2021-03-30 08:22:11 -04:00
|
|
|
return callback(new Error(`statusCode=${response.statusCode}`), body)
|
2020-06-26 08:17:45 -04:00
|
|
|
}
|
2020-02-19 06:16:14 -05:00
|
|
|
return callback(null, JSON.parse(body))
|
|
|
|
}
|
|
|
|
)
|
2021-07-13 07:04:48 -04:00
|
|
|
},
|
2020-02-19 06:16:14 -05:00
|
|
|
}
|