2018-05-31 06:12:31 -04:00
|
|
|
path = require('path')
|
|
|
|
Project = require('../../../js/models/Project').Project
|
|
|
|
ProjectUploadManager = require('../../../js/Features/Uploads/ProjectUploadManager')
|
2018-05-31 07:15:42 -04:00
|
|
|
ProjectOptionsHandler = require('../../../js/Features/Project/ProjectOptionsHandler')
|
2018-09-24 10:26:11 -04:00
|
|
|
ProjectRootDocManager = require('../../../js/Features/Project/ProjectRootDocManager')
|
2018-09-25 06:15:32 -04:00
|
|
|
ProjectDetailsHandler = require('../../../js/Features/Project/ProjectDetailsHandler')
|
2018-05-31 06:12:31 -04:00
|
|
|
AuthenticationController = require('../../../js/Features/Authentication/AuthenticationController')
|
|
|
|
settings = require('settings-sharelatex')
|
|
|
|
fs = require('fs')
|
|
|
|
request = require('request')
|
|
|
|
uuid = require('uuid')
|
|
|
|
logger = require('logger-sharelatex')
|
|
|
|
async = require("async")
|
|
|
|
|
2018-10-04 11:42:03 -04:00
|
|
|
ENGINE_TO_COMPILER_MAP = {
|
|
|
|
latex_dvipdf: "latex"
|
|
|
|
pdflatex: "pdflatex"
|
|
|
|
xelatex: "xelatex"
|
|
|
|
lualatex: "lualatex"
|
|
|
|
}
|
2018-05-31 06:12:31 -04:00
|
|
|
|
|
|
|
module.exports = TemplatesController =
|
|
|
|
|
|
|
|
getV1Template: (req, res)->
|
|
|
|
templateVersionId = req.params.Template_version_id
|
|
|
|
templateId = req.query.id
|
|
|
|
if !/^[0-9]+$/.test(templateVersionId) || !/^[0-9]+$/.test(templateId)
|
|
|
|
logger.err templateVersionId:templateVersionId, templateId: templateId, "invalid template id or version"
|
|
|
|
return res.sendStatus 400
|
|
|
|
data = {}
|
|
|
|
data.templateVersionId = templateVersionId
|
|
|
|
data.templateId = templateId
|
|
|
|
data.name = req.query.templateName
|
2018-10-04 11:42:03 -04:00
|
|
|
data.compiler = ENGINE_TO_COMPILER_MAP[req.query.latexEngine]
|
2018-09-24 06:59:55 -04:00
|
|
|
data.mainFile = req.query.mainFile
|
2018-05-31 06:12:31 -04:00
|
|
|
res.render path.resolve(__dirname, "../../../views/project/editor/new_from_template"), data
|
|
|
|
|
|
|
|
createProjectFromV1Template: (req, res)->
|
|
|
|
currentUserId = AuthenticationController.getLoggedInUserId(req)
|
|
|
|
zipUrl = "#{settings.apis.v1.url}/api/v1/sharelatex/templates/#{req.body.templateVersionId}"
|
|
|
|
zipReq = request(zipUrl, {
|
|
|
|
'auth': {
|
|
|
|
'user': settings.apis.v1.user,
|
|
|
|
'pass': settings.apis.v1.pass
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
TemplatesController.createFromZip(
|
|
|
|
zipReq,
|
|
|
|
{
|
|
|
|
templateName: req.body.templateName,
|
|
|
|
currentUserId: currentUserId,
|
|
|
|
compiler: req.body.compiler
|
|
|
|
docId: req.body.docId
|
2018-09-24 06:59:55 -04:00
|
|
|
mainFile: req.body.mainFile
|
2018-05-31 06:12:31 -04:00
|
|
|
templateId: req.body.templateId
|
|
|
|
templateVersionId: req.body.templateVersionId
|
2018-09-23 07:38:28 -04:00
|
|
|
image: 'wl_texlive:2018.1'
|
2018-05-31 06:12:31 -04:00
|
|
|
},
|
|
|
|
req,
|
|
|
|
res
|
|
|
|
)
|
|
|
|
|
|
|
|
createFromZip: (zipReq, options, req, res)->
|
2018-09-25 06:15:32 -04:00
|
|
|
# remove any invalid characters from template name
|
|
|
|
projectName = ProjectDetailsHandler.fixProjectName(options.templateName)
|
2018-05-31 06:12:31 -04:00
|
|
|
dumpPath = "#{settings.path.dumpFolder}/#{uuid.v4()}"
|
|
|
|
writeStream = fs.createWriteStream(dumpPath)
|
|
|
|
|
|
|
|
zipReq.on "error", (error) ->
|
|
|
|
logger.error err: error, "error getting zip from template API"
|
|
|
|
zipReq.pipe(writeStream)
|
|
|
|
writeStream.on 'close', ->
|
2018-09-25 06:15:32 -04:00
|
|
|
ProjectUploadManager.createProjectFromZipArchive options.currentUserId, projectName, dumpPath, (err, project)->
|
2018-05-31 06:12:31 -04:00
|
|
|
if err?
|
|
|
|
logger.err err:err, zipReq:zipReq, "problem building project from zip"
|
|
|
|
return res.sendStatus 500
|
|
|
|
setCompiler project._id, options.compiler, ->
|
2018-09-23 07:38:28 -04:00
|
|
|
setImage project._id, options.image, ->
|
2018-09-25 04:32:14 -04:00
|
|
|
setMainFile project._id, options.mainFile, ->
|
|
|
|
fs.unlink dumpPath, ->
|
|
|
|
delete req.session.templateData
|
|
|
|
conditions = {_id:project._id}
|
|
|
|
update = {
|
|
|
|
fromV1TemplateId:options.templateId,
|
|
|
|
fromV1TemplateVersionId:options.templateVersionId
|
|
|
|
}
|
|
|
|
Project.update conditions, update, {}, (err)->
|
|
|
|
res.redirect "/project/#{project._id}"
|
2018-05-31 06:12:31 -04:00
|
|
|
|
|
|
|
setCompiler = (project_id, compiler, callback)->
|
|
|
|
if compiler?
|
|
|
|
ProjectOptionsHandler.setCompiler project_id, compiler, callback
|
|
|
|
else
|
|
|
|
callback()
|
2018-09-23 07:38:28 -04:00
|
|
|
|
|
|
|
setImage = (project_id, imageName, callback)->
|
|
|
|
if imageName?
|
|
|
|
ProjectOptionsHandler.setImageName project_id, imageName, callback
|
|
|
|
else
|
|
|
|
callback()
|
2018-09-25 04:32:14 -04:00
|
|
|
|
2018-09-24 06:59:55 -04:00
|
|
|
setMainFile = (project_id, mainFile, callback) ->
|
|
|
|
if mainFile?
|
|
|
|
ProjectRootDocManager.setRootDocFromName project_id, mainFile, callback
|
|
|
|
else
|
2018-09-25 04:32:14 -04:00
|
|
|
callback()
|