2014-02-12 05:23:40 -05:00
|
|
|
path = require "path"
|
|
|
|
rimraf = require "rimraf"
|
2018-12-03 06:06:05 -05:00
|
|
|
async = require "async"
|
2014-02-12 05:23:40 -05:00
|
|
|
ArchiveManager = require "./ArchiveManager"
|
|
|
|
FileSystemImportManager = require "./FileSystemImportManager"
|
|
|
|
ProjectCreationHandler = require "../Project/ProjectCreationHandler"
|
|
|
|
ProjectRootDocManager = require "../Project/ProjectRootDocManager"
|
2018-10-12 07:29:37 -04:00
|
|
|
ProjectDetailsHandler = require "../Project/ProjectDetailsHandler"
|
2018-12-03 06:06:05 -05:00
|
|
|
DocumentHelper = require "../Documents/DocumentHelper"
|
2014-02-12 05:23:40 -05:00
|
|
|
|
|
|
|
module.exports = ProjectUploadHandler =
|
2018-12-03 06:06:05 -05:00
|
|
|
createProjectFromZipArchive: (owner_id, defaultName, zipPath, callback = (error, project) ->) ->
|
|
|
|
destination = @_getDestinationDirectory zipPath
|
|
|
|
docPath = null
|
|
|
|
project = null
|
|
|
|
|
|
|
|
async.waterfall([
|
|
|
|
(cb) ->
|
|
|
|
ArchiveManager.extractZipArchive zipPath, destination, cb
|
|
|
|
(cb) ->
|
|
|
|
ProjectRootDocManager.findRootDocFileFromDirectory destination, (error, _docPath, docContents) ->
|
|
|
|
cb(error, _docPath, docContents)
|
|
|
|
(_docPath, docContents, cb) ->
|
|
|
|
docPath = _docPath
|
2018-12-05 07:01:54 -05:00
|
|
|
proposedName = ProjectDetailsHandler.fixProjectName(DocumentHelper.getTitleFromTexContent(docContents || '') || defaultName)
|
2018-12-03 06:06:05 -05:00
|
|
|
ProjectDetailsHandler.generateUniqueName owner_id, proposedName, (error, name) ->
|
|
|
|
cb(error, name)
|
|
|
|
(name, cb) ->
|
|
|
|
ProjectCreationHandler.createBlankProject owner_id, name, (error, _project) ->
|
|
|
|
cb(error, _project)
|
|
|
|
(_project, cb) =>
|
|
|
|
project = _project
|
|
|
|
@_insertZipContentsIntoFolder owner_id, project._id, project.rootFolder[0]._id, destination, cb
|
|
|
|
(cb) ->
|
|
|
|
if docPath?
|
|
|
|
ProjectRootDocManager.setRootDocFromName project._id, docPath, (error) ->
|
|
|
|
cb(error)
|
|
|
|
else
|
|
|
|
cb(null)
|
|
|
|
(cb) ->
|
|
|
|
cb(null, project)
|
|
|
|
], callback)
|
|
|
|
|
|
|
|
createProjectFromZipArchiveWithName: (owner_id, proposedName, zipPath, callback = (error, project) ->) ->
|
2018-12-05 07:01:54 -05:00
|
|
|
ProjectDetailsHandler.generateUniqueName owner_id, ProjectDetailsHandler.fixProjectName(proposedName), (error, name) =>
|
2014-02-12 05:23:40 -05:00
|
|
|
return callback(error) if error?
|
2018-10-12 07:29:37 -04:00
|
|
|
ProjectCreationHandler.createBlankProject owner_id, name, (error, project) =>
|
2014-02-12 05:23:40 -05:00
|
|
|
return callback(error) if error?
|
2018-10-12 07:29:37 -04:00
|
|
|
@insertZipArchiveIntoFolder owner_id, project._id, project.rootFolder[0]._id, zipPath, (error) ->
|
2014-02-12 05:23:40 -05:00
|
|
|
return callback(error) if error?
|
2018-10-12 07:29:37 -04:00
|
|
|
ProjectRootDocManager.setRootDocAutomatically project._id, (error) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
callback(error, project)
|
2014-02-12 05:23:40 -05:00
|
|
|
|
2018-12-03 06:06:05 -05:00
|
|
|
insertZipArchiveIntoFolder: (owner_id, project_id, folder_id, zipPath, callback = (error) ->) ->
|
|
|
|
destination = @_getDestinationDirectory zipPath
|
|
|
|
ArchiveManager.extractZipArchive zipPath, destination, (error) =>
|
2014-02-12 05:23:40 -05:00
|
|
|
return callback(error) if error?
|
2018-12-03 06:06:05 -05:00
|
|
|
|
|
|
|
@_insertZipContentsIntoFolder owner_id, project_id, folder_id, destination, callback
|
|
|
|
|
|
|
|
_insertZipContentsIntoFolder: (owner_id, project_id, folder_id, destination, callback = (error) ->) ->
|
2016-02-24 12:21:20 -05:00
|
|
|
ArchiveManager.findTopLevelDirectory destination, (error, topLevelDestination) ->
|
2014-02-12 05:23:40 -05:00
|
|
|
return callback(error) if error?
|
2016-02-24 12:21:20 -05:00
|
|
|
FileSystemImportManager.addFolderContents owner_id, project_id, folder_id, topLevelDestination, false, (error) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
rimraf(destination, callback)
|
2014-02-12 05:23:40 -05:00
|
|
|
|
|
|
|
_getDestinationDirectory: (source) ->
|
|
|
|
return path.join(path.dirname(source), "#{path.basename(source, ".zip")}-#{Date.now()}")
|
|
|
|
|