ProjectUploadManager.coffee 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. path = require "path"
  2. rimraf = require "rimraf"
  3. async = require "async"
  4. ArchiveManager = require "./ArchiveManager"
  5. FileSystemImportManager = require "./FileSystemImportManager"
  6. ProjectCreationHandler = require "../Project/ProjectCreationHandler"
  7. ProjectRootDocManager = require "../Project/ProjectRootDocManager"
  8. ProjectDetailsHandler = require "../Project/ProjectDetailsHandler"
  9. DocumentHelper = require "../Documents/DocumentHelper"
  10. module.exports = ProjectUploadHandler =
  11. createProjectFromZipArchive: (owner_id, defaultName, zipPath, callback = (error, project) ->) ->
  12. destination = @_getDestinationDirectory zipPath
  13. docPath = null
  14. project = null
  15. async.waterfall([
  16. (cb) ->
  17. ArchiveManager.extractZipArchive zipPath, destination, cb
  18. (cb) ->
  19. ProjectRootDocManager.findRootDocFileFromDirectory destination, (error, _docPath, docContents) ->
  20. cb(error, _docPath, docContents)
  21. (_docPath, docContents, cb) ->
  22. docPath = _docPath
  23. proposedName = DocumentHelper.getTitleFromTexContent(docContents || '') || defaultName
  24. ProjectDetailsHandler.generateUniqueName owner_id, proposedName, (error, name) ->
  25. cb(error, name)
  26. (name, cb) ->
  27. ProjectCreationHandler.createBlankProject owner_id, name, (error, _project) ->
  28. cb(error, _project)
  29. (_project, cb) =>
  30. project = _project
  31. @_insertZipContentsIntoFolder owner_id, project._id, project.rootFolder[0]._id, destination, cb
  32. (cb) ->
  33. if docPath?
  34. ProjectRootDocManager.setRootDocFromName project._id, docPath, (error) ->
  35. cb(error)
  36. else
  37. cb(null)
  38. (cb) ->
  39. cb(null, project)
  40. ], callback)
  41. createProjectFromZipArchiveWithName: (owner_id, proposedName, zipPath, callback = (error, project) ->) ->
  42. ProjectDetailsHandler.generateUniqueName owner_id, proposedName, (error, name) =>
  43. return callback(error) if error?
  44. ProjectCreationHandler.createBlankProject owner_id, name, (error, project) =>
  45. return callback(error) if error?
  46. @insertZipArchiveIntoFolder owner_id, project._id, project.rootFolder[0]._id, zipPath, (error) ->
  47. return callback(error) if error?
  48. ProjectRootDocManager.setRootDocAutomatically project._id, (error) ->
  49. return callback(error) if error?
  50. callback(error, project)
  51. insertZipArchiveIntoFolder: (owner_id, project_id, folder_id, zipPath, callback = (error) ->) ->
  52. destination = @_getDestinationDirectory zipPath
  53. ArchiveManager.extractZipArchive zipPath, destination, (error) =>
  54. return callback(error) if error?
  55. @_insertZipContentsIntoFolder owner_id, project_id, folder_id, destination, callback
  56. _insertZipContentsIntoFolder: (owner_id, project_id, folder_id, destination, callback = (error) ->) ->
  57. ArchiveManager.findTopLevelDirectory destination, (error, topLevelDestination) ->
  58. return callback(error) if error?
  59. FileSystemImportManager.addFolderContents owner_id, project_id, folder_id, topLevelDestination, false, (error) ->
  60. return callback(error) if error?
  61. rimraf(destination, callback)
  62. _getDestinationDirectory: (source) ->
  63. return path.join(path.dirname(source), "#{path.basename(source, ".zip")}-#{Date.now()}")