2014-02-12 05:23:40 -05:00
|
|
|
Metrics = require "../../infrastructure/Metrics"
|
|
|
|
Project = require("../../models/Project").Project
|
|
|
|
CompileManager = require("./CompileManager")
|
2014-05-19 11:10:41 -04:00
|
|
|
ClsiManager = require("./ClsiManager")
|
2014-02-12 05:23:40 -05:00
|
|
|
logger = require "logger-sharelatex"
|
|
|
|
request = require "request"
|
|
|
|
Settings = require "settings-sharelatex"
|
2014-05-19 10:28:35 -04:00
|
|
|
AuthenticationController = require "../Authentication/AuthenticationController"
|
2014-10-16 11:34:21 -04:00
|
|
|
UserGetter = require "../User/UserGetter"
|
2015-10-30 07:35:11 -04:00
|
|
|
RateLimiter = require("../../infrastructure/RateLimiter")
|
2016-04-20 10:06:39 -04:00
|
|
|
ClsiCookieManager = require("./ClsiCookieManager")
|
2016-05-20 11:32:51 -04:00
|
|
|
Path = require("path")
|
2014-02-12 05:23:40 -05:00
|
|
|
|
|
|
|
module.exports = CompileController =
|
2014-05-19 10:28:35 -04:00
|
|
|
compile: (req, res, next = (error) ->) ->
|
2014-10-17 08:23:29 -04:00
|
|
|
res.setTimeout(5 * 60 * 1000)
|
2014-05-19 10:28:35 -04:00
|
|
|
project_id = req.params.Project_id
|
|
|
|
isAutoCompile = !!req.query?.auto_compile
|
2016-09-05 10:58:31 -04:00
|
|
|
user_id = AuthenticationController.getLoggedInUserId req
|
|
|
|
options = {
|
|
|
|
isAutoCompile: isAutoCompile
|
|
|
|
}
|
|
|
|
if req.body?.rootDoc_id?
|
|
|
|
options.rootDoc_id = req.body.rootDoc_id
|
|
|
|
else if req.body?.settingsOverride?.rootDoc_id? # Can be removed after deploy
|
|
|
|
options.rootDoc_id = req.body.settingsOverride.rootDoc_id
|
|
|
|
if req.body?.compiler
|
|
|
|
options.compiler = req.body.compiler
|
|
|
|
if req.body?.draft
|
|
|
|
options.draft = req.body.draft
|
|
|
|
if req.body?.check in ['validate', 'error', 'silent']
|
|
|
|
options.check = req.body.check
|
|
|
|
logger.log {options:options, project_id:project_id, user_id:user_id}, "got compile request"
|
|
|
|
CompileManager.compile project_id, user_id, options, (error, status, outputFiles, clsiServerId, limits, validationProblems) ->
|
2014-05-19 10:28:35 -04:00
|
|
|
return next(error) if error?
|
2016-09-05 10:58:31 -04:00
|
|
|
res.contentType("application/json")
|
|
|
|
res.status(200).send JSON.stringify {
|
|
|
|
status: status
|
|
|
|
outputFiles: outputFiles
|
|
|
|
compileGroup: limits?.compileGroup
|
|
|
|
clsiServerId:clsiServerId
|
|
|
|
validationProblems:validationProblems
|
2014-11-28 09:26:21 -05:00
|
|
|
}
|
2014-05-19 10:28:35 -04:00
|
|
|
|
2016-07-14 09:48:46 -04:00
|
|
|
stopCompile: (req, res, next = (error) ->) ->
|
|
|
|
project_id = req.params.Project_id
|
2016-09-05 10:58:31 -04:00
|
|
|
user_id = AuthenticationController.getLoggedInUserId req
|
|
|
|
logger.log {project_id:project_id, user_id:user_id}, "stop compile request"
|
|
|
|
CompileManager.stopCompile project_id, user_id, (error) ->
|
2016-07-14 09:48:46 -04:00
|
|
|
return next(error) if error?
|
2016-09-05 10:58:31 -04:00
|
|
|
res.status(200).send()
|
2016-07-14 09:48:46 -04:00
|
|
|
|
2016-05-31 11:20:24 -04:00
|
|
|
_compileAsUser: (req, callback) ->
|
2016-06-30 04:43:09 -04:00
|
|
|
# callback with user_id if per-user, undefined otherwise
|
|
|
|
if not Settings.disablePerUserCompiles
|
2016-09-05 10:58:31 -04:00
|
|
|
user_id = AuthenticationController.getLoggedInUserId req
|
|
|
|
return callback(null, user_id)
|
2016-05-31 11:20:24 -04:00
|
|
|
else
|
2016-06-16 08:59:24 -04:00
|
|
|
callback() # do a per-project compile, not per-user
|
|
|
|
|
|
|
|
_downloadAsUser: (req, callback) ->
|
2016-06-30 04:43:09 -04:00
|
|
|
# callback with user_id if per-user, undefined otherwise
|
|
|
|
if not Settings.disablePerUserCompiles
|
2016-09-05 10:58:31 -04:00
|
|
|
user_id = AuthenticationController.getLoggedInUserId req
|
|
|
|
return callback(null, user_id)
|
2016-06-16 08:59:24 -04:00
|
|
|
else
|
|
|
|
callback() # do a per-project compile, not per-user
|
2016-05-31 11:20:24 -04:00
|
|
|
|
2014-02-12 05:23:40 -05:00
|
|
|
downloadPdf: (req, res, next = (error) ->)->
|
|
|
|
Metrics.inc "pdf-downloads"
|
|
|
|
project_id = req.params.Project_id
|
2015-10-30 07:35:11 -04:00
|
|
|
isPdfjsPartialDownload = req.query?.pdfng
|
2015-11-10 11:23:55 -05:00
|
|
|
rateLimit = (callback)->
|
|
|
|
if isPdfjsPartialDownload
|
|
|
|
callback null, true
|
|
|
|
else
|
|
|
|
rateLimitOpts =
|
|
|
|
endpointName: "full-pdf-download"
|
2015-11-25 06:07:53 -05:00
|
|
|
throttle: 1000
|
2015-11-10 11:23:55 -05:00
|
|
|
subjectName : req.ip
|
2015-11-25 06:07:53 -05:00
|
|
|
timeInterval : 60 * 60
|
2015-11-10 11:23:55 -05:00
|
|
|
RateLimiter.addCount rateLimitOpts, callback
|
|
|
|
|
2014-02-12 05:23:40 -05:00
|
|
|
Project.findById project_id, {name: 1}, (err, project)->
|
|
|
|
res.contentType("application/pdf")
|
|
|
|
if !!req.query.popupDownload
|
|
|
|
logger.log project_id: project_id, "download pdf as popup download"
|
|
|
|
res.header('Content-Disposition', "attachment; filename=#{project.getSafeProjectName()}.pdf")
|
|
|
|
else
|
|
|
|
logger.log project_id: project_id, "download pdf to embed in browser"
|
|
|
|
res.header('Content-Disposition', "filename=#{project.getSafeProjectName()}.pdf")
|
2015-10-30 07:35:11 -04:00
|
|
|
|
2015-11-10 11:23:55 -05:00
|
|
|
rateLimit (err, canContinue)->
|
2015-10-30 07:35:11 -04:00
|
|
|
if err?
|
|
|
|
logger.err err:err, "error checking rate limit for pdf download"
|
|
|
|
return res.send 500
|
|
|
|
else if !canContinue
|
|
|
|
logger.log project_id:project_id, ip:req.ip, "rate limit hit downloading pdf"
|
|
|
|
res.send 500
|
|
|
|
else
|
2016-06-16 08:59:24 -04:00
|
|
|
CompileController._downloadAsUser req, (error, user_id) ->
|
|
|
|
url = CompileController._getFileUrl project_id, user_id, req.params.build_id, "output.pdf"
|
|
|
|
CompileController.proxyToClsi(project_id, url, req, res, next)
|
2014-02-12 05:23:40 -05:00
|
|
|
|
2014-03-27 13:00:41 -04:00
|
|
|
deleteAuxFiles: (req, res, next) ->
|
|
|
|
project_id = req.params.Project_id
|
2016-05-31 11:20:24 -04:00
|
|
|
CompileController._compileAsUser req, (error, user_id) ->
|
2014-05-19 11:10:41 -04:00
|
|
|
return next(error) if error?
|
2016-05-31 11:20:24 -04:00
|
|
|
CompileManager.deleteAuxFiles project_id, user_id, (error) ->
|
|
|
|
return next(error) if error?
|
|
|
|
res.sendStatus(200)
|
2014-02-12 05:23:40 -05:00
|
|
|
|
2016-06-16 08:59:24 -04:00
|
|
|
# this is only used by templates, so is not called with a user_id
|
2014-02-12 05:23:40 -05:00
|
|
|
compileAndDownloadPdf: (req, res, next)->
|
|
|
|
project_id = req.params.project_id
|
2016-06-16 08:59:24 -04:00
|
|
|
# pass user_id as null, since templates are an "anonymous" compile
|
|
|
|
CompileManager.compile project_id, null, {}, (err)->
|
|
|
|
if err?
|
|
|
|
logger.err err:err, project_id:project_id, "something went wrong compile and downloading pdf"
|
|
|
|
res.sendStatus 500
|
|
|
|
url = "/project/#{project_id}/output/output.pdf"
|
|
|
|
CompileController.proxyToClsi project_id, url, req, res, next
|
2014-02-12 05:23:40 -05:00
|
|
|
|
|
|
|
getFileFromClsi: (req, res, next = (error) ->) ->
|
2014-11-28 09:26:21 -05:00
|
|
|
project_id = req.params.Project_id
|
2016-06-16 08:59:24 -04:00
|
|
|
CompileController._downloadAsUser req, (error, user_id) ->
|
|
|
|
return next(error) if error?
|
|
|
|
url = CompileController._getFileUrl project_id, user_id, req.params.build_id, req.params.file
|
|
|
|
CompileController.proxyToClsi(project_id, url, req, res, next)
|
|
|
|
|
|
|
|
# compute a GET file url for a given project, user (optional), build (optional) and file
|
|
|
|
_getFileUrl: (project_id, user_id, build_id, file) ->
|
2016-06-02 12:03:07 -04:00
|
|
|
if user_id? and build_id?
|
2016-06-16 08:59:24 -04:00
|
|
|
url = "/project/#{project_id}/user/#{user_id}/build/#{build_id}/output/#{file}"
|
|
|
|
else if user_id?
|
|
|
|
url = "/project/#{project_id}/user/#{user_id}/output/#{file}"
|
2016-06-02 12:03:07 -04:00
|
|
|
else if build_id?
|
2016-06-16 08:59:24 -04:00
|
|
|
url = "/project/#{project_id}/build/#{build_id}/output/#{file}"
|
2016-05-13 05:33:38 -04:00
|
|
|
else
|
2016-06-16 08:59:24 -04:00
|
|
|
url = "/project/#{project_id}/output/#{file}"
|
|
|
|
return url
|
2014-02-12 05:23:40 -05:00
|
|
|
|
2016-06-16 08:59:24 -04:00
|
|
|
# compute a POST url for a project, user (optional) and action
|
2016-05-31 11:20:24 -04:00
|
|
|
_getUrl: (project_id, user_id, action) ->
|
|
|
|
path = "/project/#{project_id}"
|
|
|
|
path += "/user/#{user_id}" if user_id?
|
|
|
|
return "#{path}/#{action}"
|
|
|
|
|
2016-05-20 11:32:51 -04:00
|
|
|
proxySyncPdf: (req, res, next = (error) ->) ->
|
|
|
|
project_id = req.params.Project_id
|
|
|
|
{page, h, v} = req.query
|
|
|
|
if not page?.match(/^\d+$/)
|
|
|
|
return next(new Error("invalid page parameter"))
|
2016-08-30 11:45:21 -04:00
|
|
|
if not h?.match(/^-?\d+\.\d+$/)
|
2016-05-20 11:32:51 -04:00
|
|
|
return next(new Error("invalid h parameter"))
|
2016-08-30 11:45:21 -04:00
|
|
|
if not v?.match(/^-?\d+\.\d+$/)
|
2016-05-20 11:32:51 -04:00
|
|
|
return next(new Error("invalid v parameter"))
|
2016-05-31 11:20:24 -04:00
|
|
|
# whether this request is going to a per-user container
|
|
|
|
CompileController._compileAsUser req, (error, user_id) ->
|
|
|
|
return next(error) if error?
|
2016-06-14 03:40:15 -04:00
|
|
|
url = CompileController._getUrl(project_id, user_id, "sync/pdf")
|
|
|
|
destination = {url: url, qs: {page, h, v}}
|
2016-05-31 11:20:24 -04:00
|
|
|
CompileController.proxyToClsi(project_id, destination, req, res, next)
|
2016-05-20 11:32:51 -04:00
|
|
|
|
|
|
|
proxySyncCode: (req, res, next = (error) ->) ->
|
|
|
|
project_id = req.params.Project_id
|
|
|
|
{file, line, column} = req.query
|
2016-07-18 12:00:34 -04:00
|
|
|
if not file?
|
|
|
|
return next(new Error("missing file parameter"))
|
|
|
|
# Check that we are dealing with a simple file path (this is not
|
|
|
|
# strictly needed because synctex uses this parameter as a label
|
|
|
|
# to look up in the synctex output, and does not open the file
|
|
|
|
# itself). Since we have valid synctex paths like foo/./bar we
|
|
|
|
# allow those by replacing /./ with /
|
|
|
|
testPath = file.replace '/./', '/'
|
|
|
|
if Path.resolve("/", testPath) isnt "/#{testPath}"
|
2016-05-20 11:32:51 -04:00
|
|
|
return next(new Error("invalid file parameter"))
|
|
|
|
if not line?.match(/^\d+$/)
|
|
|
|
return next(new Error("invalid line parameter"))
|
|
|
|
if not column?.match(/^\d+$/)
|
|
|
|
return next(new Error("invalid column parameter"))
|
2016-05-31 11:20:24 -04:00
|
|
|
CompileController._compileAsUser req, (error, user_id) ->
|
|
|
|
return next(error) if error?
|
2016-06-14 03:40:15 -04:00
|
|
|
url = CompileController._getUrl(project_id, user_id, "sync/code")
|
|
|
|
destination = {url:url, qs: {file, line, column}}
|
2016-05-31 11:20:24 -04:00
|
|
|
CompileController.proxyToClsi(project_id, destination, req, res, next)
|
2014-04-08 11:49:21 -04:00
|
|
|
|
2014-11-28 09:26:21 -05:00
|
|
|
proxyToClsi: (project_id, url, req, res, next = (error) ->) ->
|
2014-12-12 11:47:43 -05:00
|
|
|
if req.query?.compileGroup
|
|
|
|
CompileController.proxyToClsiWithLimits(project_id, url, {compileGroup: req.query.compileGroup}, req, res, next)
|
|
|
|
else
|
|
|
|
CompileManager.getProjectCompileLimits project_id, (error, limits) ->
|
|
|
|
return next(error) if error?
|
|
|
|
CompileController.proxyToClsiWithLimits(project_id, url, limits, req, res, next)
|
|
|
|
|
|
|
|
proxyToClsiWithLimits: (project_id, url, limits, req, res, next = (error) ->) ->
|
2016-04-20 10:06:39 -04:00
|
|
|
ClsiCookieManager.getCookieJar project_id, (err, jar)->
|
2016-04-19 11:48:51 -04:00
|
|
|
if err?
|
|
|
|
logger.err err:err, "error getting cookie jar for clsi request"
|
|
|
|
return callback(err)
|
2016-05-20 11:31:16 -04:00
|
|
|
# expand any url parameter passed in as {url:..., qs:...}
|
|
|
|
if typeof url is "object"
|
|
|
|
{url, qs} = url
|
2016-06-02 08:11:20 -04:00
|
|
|
compilerUrl = Settings.apis.clsi.url
|
2016-04-19 11:48:51 -04:00
|
|
|
url = "#{compilerUrl}#{url}"
|
|
|
|
logger.log url: url, "proxying to CLSI"
|
|
|
|
oneMinute = 60 * 1000
|
|
|
|
# the base request
|
|
|
|
options = { url: url, method: req.method, timeout: oneMinute, jar : jar }
|
2016-05-20 11:31:16 -04:00
|
|
|
# add any provided query string
|
|
|
|
options.qs = qs if qs?
|
2016-04-19 11:48:51 -04:00
|
|
|
# if we have a build parameter, pass it through to the clsi
|
|
|
|
if req.query?.pdfng && req.query?.build? # only for new pdf viewer
|
2016-05-20 11:31:16 -04:00
|
|
|
options.qs ?= {}
|
2016-04-19 11:48:51 -04:00
|
|
|
options.qs.build = req.query.build
|
|
|
|
# if we are byte serving pdfs, pass through If-* and Range headers
|
|
|
|
# do not send any others, there's a proxying loop if Host: is passed!
|
|
|
|
if req.query?.pdfng
|
|
|
|
newHeaders = {}
|
|
|
|
for h, v of req.headers
|
|
|
|
newHeaders[h] = req.headers[h] if h.match /^(If-|Range)/i
|
|
|
|
options.headers = newHeaders
|
|
|
|
proxy = request(options)
|
|
|
|
proxy.pipe(res)
|
|
|
|
proxy.on "error", (error) ->
|
|
|
|
logger.warn err: error, url: url, "CLSI proxy error"
|
2015-09-10 11:41:48 -04:00
|
|
|
|
|
|
|
wordCount: (req, res, next) ->
|
|
|
|
project_id = req.params.Project_id
|
2015-09-11 08:53:06 -04:00
|
|
|
file = req.query.file || false
|
2016-05-31 11:20:24 -04:00
|
|
|
CompileController._compileAsUser req, (error, user_id) ->
|
2015-09-10 11:41:48 -04:00
|
|
|
return next(error) if error?
|
2016-05-31 11:20:24 -04:00
|
|
|
CompileManager.wordCount project_id, user_id, file, (error, body) ->
|
|
|
|
return next(error) if error?
|
|
|
|
res.contentType("application/json")
|
|
|
|
res.send body
|