ProjectUploadManager.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /* eslint-disable
  2. camelcase,
  3. handle-callback-err,
  4. max-len,
  5. no-unused-vars,
  6. */
  7. // TODO: This file was created by bulk-decaffeinate.
  8. // Fix any style issues and re-enable lint.
  9. /*
  10. * decaffeinate suggestions:
  11. * DS102: Remove unnecessary code created because of implicit returns
  12. * DS207: Consider shorter variations of null checks
  13. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  14. */
  15. const path = require('path')
  16. const rimraf = require('rimraf')
  17. const { promisify, callbackify } = require('util')
  18. const ArchiveManager = require('./ArchiveManager')
  19. const FileSystemImportManager = require('./FileSystemImportManager')
  20. const ProjectCreationHandler = require('../Project/ProjectCreationHandler')
  21. const ProjectRootDocManager = require('../Project/ProjectRootDocManager')
  22. const ProjectDetailsHandler = require('../Project/ProjectDetailsHandler')
  23. const DocumentHelper = require('../Documents/DocumentHelper')
  24. const ProjectUploadManager = {
  25. createProjectFromZipArchive(ownerId, defaultName, zipPath, callback) {
  26. callbackify(ProjectUploadManager.promises.createProjectFromZipArchive)(
  27. ownerId,
  28. defaultName,
  29. zipPath,
  30. callback
  31. )
  32. },
  33. createProjectFromZipArchiveWithName(
  34. ownerId,
  35. proposedName,
  36. zipPath,
  37. attributes,
  38. callback
  39. ) {
  40. if (callback == null) {
  41. callback = function(error, project) {}
  42. }
  43. if (arguments.length === 4) {
  44. callback = attributes
  45. attributes = {}
  46. }
  47. callbackify(
  48. ProjectUploadManager.promises.createProjectFromZipArchiveWithName
  49. )(ownerId, proposedName, zipPath, attributes, callback)
  50. },
  51. insertZipArchiveIntoFolder(
  52. owner_id,
  53. project_id,
  54. folder_id,
  55. zipPath,
  56. callback
  57. ) {
  58. if (callback == null) {
  59. callback = function(error) {}
  60. }
  61. const destination = ProjectUploadManager._getDestinationDirectory(zipPath)
  62. return ArchiveManager.extractZipArchive(zipPath, destination, error => {
  63. if (error != null) {
  64. return callback(error)
  65. }
  66. return ProjectUploadManager._insertZipContentsIntoFolder(
  67. owner_id,
  68. project_id,
  69. folder_id,
  70. destination,
  71. callback
  72. )
  73. })
  74. },
  75. _insertZipContentsIntoFolder(
  76. owner_id,
  77. project_id,
  78. folder_id,
  79. destination,
  80. callback
  81. ) {
  82. if (callback == null) {
  83. callback = function(error) {}
  84. }
  85. return ArchiveManager.findTopLevelDirectory(destination, function(
  86. error,
  87. topLevelDestination
  88. ) {
  89. if (error != null) {
  90. return callback(error)
  91. }
  92. return FileSystemImportManager.addFolderContents(
  93. owner_id,
  94. project_id,
  95. folder_id,
  96. topLevelDestination,
  97. false,
  98. function(error) {
  99. if (error != null) {
  100. return callback(error)
  101. }
  102. return rimraf(destination, callback)
  103. }
  104. )
  105. })
  106. },
  107. _getDestinationDirectory(source) {
  108. return path.join(
  109. path.dirname(source),
  110. `${path.basename(source, '.zip')}-${Date.now()}`
  111. )
  112. }
  113. }
  114. const promises = {
  115. async createProjectFromZipArchive(ownerId, defaultName, zipPath) {
  116. const destination = ProjectUploadManager._getDestinationDirectory(zipPath)
  117. await ArchiveManager.promises.extractZipArchive(zipPath, destination)
  118. const {
  119. path,
  120. content
  121. } = await ProjectRootDocManager.promises.findRootDocFileFromDirectory(
  122. destination
  123. )
  124. const projectName =
  125. DocumentHelper.getTitleFromTexContent(content || '') || defaultName
  126. const proposedName = ProjectDetailsHandler.fixProjectName(projectName)
  127. const uniqueName = await ProjectDetailsHandler.promises.generateUniqueName(
  128. ownerId,
  129. proposedName
  130. )
  131. const project = await ProjectCreationHandler.promises.createBlankProject(
  132. ownerId,
  133. uniqueName
  134. )
  135. await ProjectUploadManager.promises._insertZipContentsIntoFolder(
  136. ownerId,
  137. project._id,
  138. project.rootFolder[0]._id,
  139. destination
  140. )
  141. if (path) {
  142. await ProjectRootDocManager.promises.setRootDocFromName(project._id, path)
  143. }
  144. return project
  145. },
  146. async createProjectFromZipArchiveWithName(
  147. ownerId,
  148. proposedName,
  149. zipPath,
  150. attributes
  151. ) {
  152. attributes = attributes || {}
  153. const fixedProjectName = ProjectDetailsHandler.fixProjectName(proposedName)
  154. const projectName = await ProjectDetailsHandler.promises.generateUniqueName(
  155. ownerId,
  156. fixedProjectName
  157. )
  158. const project = await ProjectCreationHandler.promises.createBlankProject(
  159. ownerId,
  160. projectName,
  161. attributes
  162. )
  163. await ProjectUploadManager.promises.insertZipArchiveIntoFolder(
  164. ownerId,
  165. project._id,
  166. project.rootFolder[0]._id,
  167. zipPath
  168. )
  169. await ProjectRootDocManager.promises.setRootDocAutomatically(project._id)
  170. return project
  171. },
  172. _insertZipContentsIntoFolder: promisify(
  173. ProjectUploadManager._insertZipContentsIntoFolder
  174. ),
  175. insertZipArchiveIntoFolder(ownerId, projectId, folderId, zipPath) {
  176. return promisify(ProjectUploadManager.insertZipArchiveIntoFolder)(
  177. ownerId,
  178. projectId,
  179. folderId,
  180. zipPath
  181. )
  182. }
  183. }
  184. ProjectUploadManager.promises = promises
  185. module.exports = ProjectUploadManager