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"
|
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
|
|
|
|
AuthenticationController.getLoggedInUserId req, (error, user_id) ->
|
|
|
|
return next(error) if error?
|
2014-11-28 09:26:21 -05:00
|
|
|
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
|
|
|
|
logger.log {options, project_id}, "got compile request"
|
2014-12-12 11:47:43 -05:00
|
|
|
CompileManager.compile project_id, user_id, options, (error, status, outputFiles, output, limits) ->
|
2014-11-28 09:26:21 -05:00
|
|
|
return next(error) if error?
|
|
|
|
res.contentType("application/json")
|
|
|
|
res.send 200, JSON.stringify {
|
|
|
|
status: status
|
|
|
|
outputFiles: outputFiles
|
2014-12-12 11:47:43 -05:00
|
|
|
compileGroup: limits.compileGroup
|
2014-11-28 09:26:21 -05:00
|
|
|
}
|
2014-05-19 10:28:35 -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
|
|
|
|
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")
|
2014-11-28 09:26:21 -05:00
|
|
|
CompileController.proxyToClsi(project_id, "/project/#{project_id}/output/output.pdf", 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
|
2014-12-01 07:19:01 -05:00
|
|
|
CompileManager.deleteAuxFiles project_id, (error) ->
|
2014-05-19 11:10:41 -04:00
|
|
|
return next(error) if error?
|
|
|
|
res.send(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"
|
|
|
|
res.send 500
|
|
|
|
url = "/project/#{project_id}/output/output.pdf"
|
2014-11-28 09:26:21 -05:00
|
|
|
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
|
|
|
|
CompileController.proxyToClsi(project_id, "/project/#{project_id}/output/#{req.params.file}", req, res, next)
|
2014-02-12 05:23:40 -05:00
|
|
|
|
2014-04-08 11:49:21 -04:00
|
|
|
proxySync: (req, res, next = (error) ->) ->
|
2014-11-28 09:26:21 -05:00
|
|
|
CompileController.proxyToClsi(req.params.Project_id, req.url, 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) ->) ->
|
|
|
|
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
|
|
|
|
# pass through If-* and Range headers for byte serving pdfs
|
|
|
|
# do not send any others, potential proxying loop if Host: is passed!
|
|
|
|
if req.query?.pdfng
|
2014-12-12 10:43:36 -05:00
|
|
|
newHeaders = {}
|
|
|
|
for h, v of req.headers
|
|
|
|
newHeaders[h] = req.headers[h] if h.match /^(If-|Range)/i
|
|
|
|
proxy = request(url: url, method: req.method, timeout: oneMinute, headers: newHeaders)
|
2014-12-12 11:47:43 -05:00
|
|
|
else
|
|
|
|
proxy = request(url: url, method: req.method, timeout: oneMinute)
|
|
|
|
proxy.pipe(res)
|
|
|
|
proxy.on "error", (error) ->
|
|
|
|
logger.warn err: error, url: url, "CLSI proxy error"
|