2014-02-12 05:23:40 -05:00
|
|
|
projectCreationHandler = require('./ProjectCreationHandler')
|
|
|
|
projectEntityHandler = require('./ProjectEntityHandler')
|
|
|
|
projectLocator = require('./ProjectLocator')
|
|
|
|
projectOptionsHandler = require('./ProjectOptionsHandler')
|
|
|
|
DocumentUpdaterHandler = require("../DocumentUpdater/DocumentUpdaterHandler")
|
2014-05-06 07:36:00 -04:00
|
|
|
DocstoreManager = require "../Docstore/DocstoreManager"
|
2016-02-26 03:32:00 -05:00
|
|
|
ProjectGetter = require("./ProjectGetter")
|
2014-02-12 05:23:40 -05:00
|
|
|
_ = require('underscore')
|
|
|
|
async = require('async')
|
2016-03-02 12:34:21 -05:00
|
|
|
logger = require("logger-sharelatex")
|
|
|
|
|
2014-02-12 05:23:40 -05:00
|
|
|
|
2016-03-10 06:58:05 -05:00
|
|
|
module.exports = ProjectDuplicator =
|
|
|
|
|
2017-11-23 10:23:29 -05:00
|
|
|
_copyDocs: (owner_id, newProject, originalRootDoc, originalFolder, desFolder, docContents, callback)->
|
2016-03-10 06:58:05 -05:00
|
|
|
setRootDoc = _.once (doc_id)->
|
2016-03-10 07:42:21 -05:00
|
|
|
projectEntityHandler.setRootDoc newProject._id, doc_id
|
2017-03-17 09:21:30 -04:00
|
|
|
docs = originalFolder.docs or []
|
|
|
|
jobs = docs.map (doc)->
|
2016-03-10 07:43:36 -05:00
|
|
|
return (cb)->
|
2017-03-17 09:21:30 -04:00
|
|
|
if !doc?._id?
|
|
|
|
return callback()
|
2016-03-10 06:58:05 -05:00
|
|
|
content = docContents[doc._id.toString()]
|
2017-11-23 10:23:29 -05:00
|
|
|
projectEntityHandler.addDocWithProject newProject, desFolder._id, doc.name, content.lines, owner_id, (err, newDoc)->
|
2016-03-10 06:58:05 -05:00
|
|
|
if err?
|
|
|
|
logger.err err:err, "error copying doc"
|
|
|
|
return callback(err)
|
|
|
|
if originalRootDoc? and newDoc.name == originalRootDoc.name
|
|
|
|
setRootDoc newDoc._id
|
2016-03-10 07:43:36 -05:00
|
|
|
cb()
|
2016-03-10 06:58:05 -05:00
|
|
|
|
|
|
|
async.series jobs, callback
|
|
|
|
|
2017-11-24 06:37:19 -05:00
|
|
|
_copyFiles: (owner_id, newProject, originalProject_id, originalFolder, desFolder, callback)->
|
2017-03-17 09:21:30 -04:00
|
|
|
fileRefs = originalFolder.fileRefs or []
|
|
|
|
jobs = fileRefs.map (file)->
|
2016-03-10 06:58:05 -05:00
|
|
|
return (cb)->
|
2017-11-24 06:37:19 -05:00
|
|
|
projectEntityHandler.copyFileFromExistingProjectWithProject newProject, desFolder._id, originalProject_id, file, owner_id, cb
|
2016-03-10 07:42:21 -05:00
|
|
|
async.parallelLimit jobs, 5, callback
|
2016-03-10 06:58:05 -05:00
|
|
|
|
|
|
|
|
2017-11-23 10:23:29 -05:00
|
|
|
_copyFolderRecursivly: (owner_id, newProject_id, originalProject_id, originalRootDoc, originalFolder, desFolder, docContents, callback)->
|
2016-03-10 07:42:21 -05:00
|
|
|
ProjectGetter.getProject newProject_id, {rootFolder:true, name:true}, (err, newProject)->
|
2016-03-10 06:58:05 -05:00
|
|
|
if err?
|
2016-03-10 07:42:21 -05:00
|
|
|
logger.err project_id:newProject_id, "could not get project"
|
2017-03-17 09:21:30 -04:00
|
|
|
return callback(err)
|
|
|
|
|
|
|
|
folders = originalFolder.folders or []
|
2016-03-10 06:58:05 -05:00
|
|
|
|
2017-03-17 09:21:30 -04:00
|
|
|
jobs = folders.map (childFolder)->
|
2016-03-10 06:58:05 -05:00
|
|
|
return (cb)->
|
2017-03-17 09:21:30 -04:00
|
|
|
if !childFolder?._id?
|
|
|
|
return cb()
|
2016-03-10 06:58:05 -05:00
|
|
|
projectEntityHandler.addFolderWithProject newProject, desFolder?._id, childFolder.name, (err, newFolder)->
|
|
|
|
return cb(err) if err?
|
2017-11-23 10:23:29 -05:00
|
|
|
ProjectDuplicator._copyFolderRecursivly owner_id, newProject_id, originalProject_id, originalRootDoc, childFolder, newFolder, docContents, cb
|
2016-03-10 06:58:05 -05:00
|
|
|
|
|
|
|
jobs.push (cb)->
|
2017-11-24 06:37:19 -05:00
|
|
|
ProjectDuplicator._copyFiles owner_id, newProject, originalProject_id, originalFolder, desFolder, cb
|
2016-03-10 06:58:05 -05:00
|
|
|
jobs.push (cb)->
|
2017-11-23 10:23:29 -05:00
|
|
|
ProjectDuplicator._copyDocs owner_id, newProject, originalRootDoc, originalFolder, desFolder, docContents, cb
|
2016-03-10 06:58:05 -05:00
|
|
|
|
|
|
|
async.series jobs, callback
|
|
|
|
|
|
|
|
duplicate: (owner, originalProject_id, newProjectName, callback)->
|
|
|
|
|
|
|
|
jobs =
|
|
|
|
flush: (cb)->
|
|
|
|
DocumentUpdaterHandler.flushProjectToMongo originalProject_id, cb
|
|
|
|
originalProject: (cb)->
|
|
|
|
ProjectGetter.getProject originalProject_id, {compiler:true, rootFolder:true, rootDoc_id:true}, cb
|
|
|
|
newProject: (cb)->
|
|
|
|
projectCreationHandler.createBlankProject owner._id, newProjectName, cb
|
|
|
|
originalRootDoc: (cb)->
|
|
|
|
projectLocator.findRootDoc {project_id:originalProject_id}, cb
|
|
|
|
docContentsArray: (cb)->
|
|
|
|
DocstoreManager.getAllDocs originalProject_id, cb
|
|
|
|
|
|
|
|
async.series jobs, (err, results)->
|
|
|
|
if err?
|
|
|
|
logger.err err:err, originalProject_id:originalProject_id, "error duplicating project"
|
|
|
|
return callback(err)
|
|
|
|
{originalProject, newProject, originalRootDoc, docContentsArray} = results
|
2016-03-10 07:42:21 -05:00
|
|
|
|
2016-05-23 09:58:28 -04:00
|
|
|
originalRootDoc = originalRootDoc?[0]
|
2016-03-10 07:42:21 -05:00
|
|
|
|
2016-03-10 06:58:05 -05:00
|
|
|
docContents = {}
|
|
|
|
for docContent in docContentsArray
|
|
|
|
docContents[docContent._id] = docContent
|
|
|
|
|
|
|
|
projectOptionsHandler.setCompiler newProject._id, originalProject.compiler, ->
|
2016-03-10 07:42:21 -05:00
|
|
|
|
2017-11-23 10:23:29 -05:00
|
|
|
ProjectDuplicator._copyFolderRecursivly owner._id, newProject._id, originalProject_id, originalRootDoc, originalProject.rootFolder[0], newProject.rootFolder[0], docContents, ->
|
2016-03-10 06:58:05 -05:00
|
|
|
if err?
|
|
|
|
logger.err err:err, originalProject_id:originalProject_id, newProjectName:newProjectName, "error cloning project"
|
2016-03-10 07:42:21 -05:00
|
|
|
callback(err, newProject)
|