overleaf/services/web/app/coffee/Features/Uploads/ProjectUploadManager.coffee

34 lines
1.7 KiB
CoffeeScript
Raw Normal View History

2014-02-12 05:23:40 -05:00
path = require "path"
rimraf = require "rimraf"
ArchiveManager = require "./ArchiveManager"
FileSystemImportManager = require "./FileSystemImportManager"
ProjectCreationHandler = require "../Project/ProjectCreationHandler"
ProjectRootDocManager = require "../Project/ProjectRootDocManager"
ProjectDetailsHandler = require "../Project/ProjectDetailsHandler"
2014-02-12 05:23:40 -05:00
module.exports = ProjectUploadHandler =
createProjectFromZipArchive: (owner_id, proposedName, zipPath, callback = (error, project) ->) ->
ProjectDetailsHandler.generateUniqueName owner_id, proposedName, (error, name) =>
2014-02-12 05:23:40 -05:00
return callback(error) if error?
ProjectCreationHandler.createBlankProject owner_id, name, (error, project) =>
2014-02-12 05:23:40 -05:00
return callback(error) if error?
@insertZipArchiveIntoFolder owner_id, project._id, project.rootFolder[0]._id, zipPath, (error) ->
2014-02-12 05:23:40 -05:00
return callback(error) if error?
ProjectRootDocManager.setRootDocAutomatically project._id, (error) ->
return callback(error) if error?
callback(error, project)
2014-02-12 05:23:40 -05:00
2016-02-23 11:46:14 -05:00
insertZipArchiveIntoFolder: (owner_id, project_id, folder_id, path, callback = (error) ->) ->
2014-02-12 05:23:40 -05:00
destination = @_getDestinationDirectory path
ArchiveManager.extractZipArchive path, destination, (error) ->
return callback(error) if error?
ArchiveManager.findTopLevelDirectory destination, (error, topLevelDestination) ->
2014-02-12 05:23:40 -05:00
return callback(error) if error?
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()}")