overleaf/services/web/app/coffee/Features/Project/ProjectDuplicator.coffee

70 lines
3.1 KiB
CoffeeScript
Raw Normal View History

2014-02-12 05:23:40 -05:00
projectCreationHandler = require('./ProjectCreationHandler')
projectEntityHandler = require('./ProjectEntityHandler')
projectLocator = require('./ProjectLocator')
projectOptionsHandler = require('./ProjectOptionsHandler')
DocumentUpdaterHandler = require("../DocumentUpdater/DocumentUpdaterHandler")
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')
logger = require("logger-sharelatex")
2014-02-12 05:23:40 -05:00
module.exports =
duplicate: (owner, originalProjectId, newProjectName, callback)->
DocumentUpdaterHandler.flushProjectToMongo originalProjectId, (err) ->
return callback(err) if err?
ProjectGetter.getProject originalProjectId, {compiler:true, rootFolder:true, rootDoc_id:true}, (err, originalProject) ->
2014-02-12 05:23:40 -05:00
return callback(err) if err?
projectCreationHandler.createBlankProject owner._id, newProjectName, (err, newProject)->
return callback(err) if err?
projectLocator.findRootDoc {project:originalProject}, (err, originalRootDoc)->
return callback(err) if err?
DocstoreManager.getAllDocs originalProjectId, (err, docContentsArray) ->
return callback(err) if err?
docContents = {}
for docContent in docContentsArray
docContents[docContent._id] = docContent
projectOptionsHandler.setCompiler newProject._id, originalProject.compiler
setRootDoc = _.once (doc_id)->
projectEntityHandler.setRootDoc newProject, doc_id
copyDocs = (originalFolder, newParentFolder, callback)->
jobs = originalFolder.docs.map (doc)->
return (callback)->
content = docContents[doc._id.toString()]
projectEntityHandler.addDocWithProject newProject, newParentFolder._id, doc.name, content.lines, (err, newDoc)->
if err?
logger.err err:err, originalProjectId:originalProjectId, newProjectName:newProjectName, "error adding doc"
return callback(err)
if originalRootDoc? and newDoc.name == originalRootDoc.name
setRootDoc newDoc._id
callback()
async.series jobs, callback
copyFiles = (originalFolder, newParentFolder, callback)->
jobs = originalFolder.fileRefs.map (file)->
return (callback)->
projectEntityHandler.copyFileFromExistingProjectWithProject newProject, newParentFolder._id, originalProject._id, file, callback
async.parallelLimit jobs, 5, callback
copyFolder = (folder, desFolder, callback)->
jobs = folder.folders.map (childFolder)->
return (callback)->
projectEntityHandler.addFolderWithProject newProject, desFolder._id, childFolder.name, (err, newFolder)->
copyFolder childFolder, newFolder, callback
jobs.push (cb)->
copyDocs folder, desFolder, cb
jobs.push (cb)->
copyFiles folder, desFolder, cb
async.series jobs, callback
copyFolder originalProject.rootFolder[0], newProject.rootFolder[0], (err)->
if err?
logger.err err:err, originalProjectId:originalProjectId, newProjectName:newProjectName, "error cloning project"
callback(err, newProject)
2014-02-12 05:23:40 -05:00