overleaf/services/web/app/coffee/Features/Compile/CompileController.coffee

154 lines
5.7 KiB
CoffeeScript
Raw Normal View History

2014-02-12 05:23:40 -05:00
Metrics = require "../../infrastructure/Metrics"
Project = require("../../models/Project").Project
CompileManager = require("./CompileManager")
ClsiManager = require("./ClsiManager")
2014-02-12 05:23:40 -05:00
logger = require "logger-sharelatex"
request = require "request"
Settings = require "settings-sharelatex"
AuthenticationController = require "../Authentication/AuthenticationController"
UserGetter = require "../User/UserGetter"
2015-10-30 07:35:11 -04:00
RateLimiter = require("../../infrastructure/RateLimiter")
ClsiCookieManager = require("./ClsiCookieManager")
2016-04-19 11:48:51 -04:00
2014-02-12 05:23:40 -05:00
module.exports = CompileController =
compile: (req, res, next = (error) ->) ->
res.setTimeout(5 * 60 * 1000)
project_id = req.params.Project_id
isAutoCompile = !!req.query?.auto_compile
AuthenticationController.getLoggedInUserId req, (error, user_id) ->
return next(error) if error?
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
2016-02-02 09:50:48 -05:00
if req.body?.draft
options.draft = req.body.draft
logger.log {options, project_id}, "got compile request"
CompileManager.compile project_id, user_id, options, (error, status, outputFiles, clsiServerId, limits) ->
return next(error) if error?
res.contentType("application/json")
res.status(200).send JSON.stringify {
status: status
outputFiles: outputFiles
compileGroup: limits?.compileGroup
clsiServerId:clsiServerId
}
2014-02-12 05:23:40 -05:00
downloadPdf: (req, res, next = (error) ->)->
2015-10-30 07:35:11 -04:00
2014-02-12 05:23:40 -05:00
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
CompileController.proxyToClsi(project_id, "/project/#{project_id}/output/output.pdf", req, res, next)
2014-02-12 05:23:40 -05:00
deleteAuxFiles: (req, res, next) ->
project_id = req.params.Project_id
CompileManager.deleteAuxFiles project_id, (error) ->
return next(error) if error?
2015-07-08 11:56:38 -04:00
res.sendStatus(200)
2014-02-12 05:23:40 -05:00
compileAndDownloadPdf: (req, res, next)->
project_id = req.params.project_id
CompileManager.compile project_id, null, {}, (err)->
if err?
logger.err err:err, project_id:project_id, "something went wrong compile and downloading pdf"
2015-07-08 11:56:38 -04:00
res.sendStatus 500
2014-02-12 05:23:40 -05:00
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) ->) ->
project_id = req.params.Project_id
build = req.params.build
if build?
url = "/project/#{project_id}/build/#{build}/output/#{req.params.file}"
else
url = "/project/#{project_id}/output/#{req.params.file}"
CompileController.proxyToClsi(project_id, url, req, res, next)
2014-02-12 05:23:40 -05:00
2014-04-08 11:49:21 -04:00
proxySync: (req, res, next = (error) ->) ->
CompileController.proxyToClsi(req.params.Project_id, req.url, req, res, next)
2014-04-08 11:49:21 -04:00
proxyToClsi: (project_id, url, req, res, next = (error) ->) ->
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) ->) ->
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)
if limits.compileGroup == "priority"
compilerUrl = Settings.apis.clsi_priority.url
else
compilerUrl = Settings.apis.clsi.url
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 }
# if we have a build parameter, pass it through to the clsi
if req.query?.pdfng && req.query?.build? # only for new pdf viewer
options.qs = {}
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
CompileManager.wordCount project_id, file, (error, body) ->
2015-09-10 11:41:48 -04:00
return next(error) if error?
res.contentType("application/json")
2015-09-14 10:28:19 -04:00
res.send body