| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207 |
- /* eslint-disable
- camelcase,
- handle-callback-err,
- max-len,
- no-unused-vars,
- */
- // TODO: This file was created by bulk-decaffeinate.
- // Fix any style issues and re-enable lint.
- /*
- * decaffeinate suggestions:
- * DS102: Remove unnecessary code created because of implicit returns
- * DS207: Consider shorter variations of null checks
- * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
- */
- const path = require('path')
- const rimraf = require('rimraf')
- const { promisify, callbackify } = require('util')
- const ArchiveManager = require('./ArchiveManager')
- const FileSystemImportManager = require('./FileSystemImportManager')
- const ProjectCreationHandler = require('../Project/ProjectCreationHandler')
- const ProjectRootDocManager = require('../Project/ProjectRootDocManager')
- const ProjectDetailsHandler = require('../Project/ProjectDetailsHandler')
- const DocumentHelper = require('../Documents/DocumentHelper')
- const ProjectUploadManager = {
- createProjectFromZipArchive(ownerId, defaultName, zipPath, callback) {
- callbackify(ProjectUploadManager.promises.createProjectFromZipArchive)(
- ownerId,
- defaultName,
- zipPath,
- callback
- )
- },
- createProjectFromZipArchiveWithName(
- ownerId,
- proposedName,
- zipPath,
- attributes,
- callback
- ) {
- if (callback == null) {
- callback = function(error, project) {}
- }
- if (arguments.length === 4) {
- callback = attributes
- attributes = {}
- }
- callbackify(
- ProjectUploadManager.promises.createProjectFromZipArchiveWithName
- )(ownerId, proposedName, zipPath, attributes, callback)
- },
- insertZipArchiveIntoFolder(
- owner_id,
- project_id,
- folder_id,
- zipPath,
- callback
- ) {
- if (callback == null) {
- callback = function(error) {}
- }
- const destination = ProjectUploadManager._getDestinationDirectory(zipPath)
- return ArchiveManager.extractZipArchive(zipPath, destination, error => {
- if (error != null) {
- return callback(error)
- }
- return ProjectUploadManager._insertZipContentsIntoFolder(
- owner_id,
- project_id,
- folder_id,
- destination,
- callback
- )
- })
- },
- _insertZipContentsIntoFolder(
- owner_id,
- project_id,
- folder_id,
- destination,
- callback
- ) {
- if (callback == null) {
- callback = function(error) {}
- }
- return ArchiveManager.findTopLevelDirectory(destination, function(
- error,
- topLevelDestination
- ) {
- if (error != null) {
- return callback(error)
- }
- return FileSystemImportManager.addFolderContents(
- owner_id,
- project_id,
- folder_id,
- topLevelDestination,
- false,
- function(error) {
- if (error != null) {
- return callback(error)
- }
- return rimraf(destination, callback)
- }
- )
- })
- },
- _getDestinationDirectory(source) {
- return path.join(
- path.dirname(source),
- `${path.basename(source, '.zip')}-${Date.now()}`
- )
- }
- }
- const promises = {
- async createProjectFromZipArchive(ownerId, defaultName, zipPath) {
- const destination = ProjectUploadManager._getDestinationDirectory(zipPath)
- await ArchiveManager.promises.extractZipArchive(zipPath, destination)
- const {
- path,
- content
- } = await ProjectRootDocManager.promises.findRootDocFileFromDirectory(
- destination
- )
- const projectName =
- DocumentHelper.getTitleFromTexContent(content || '') || defaultName
- const proposedName = ProjectDetailsHandler.fixProjectName(projectName)
- const uniqueName = await ProjectDetailsHandler.promises.generateUniqueName(
- ownerId,
- proposedName
- )
- const project = await ProjectCreationHandler.promises.createBlankProject(
- ownerId,
- uniqueName
- )
- await ProjectUploadManager.promises._insertZipContentsIntoFolder(
- ownerId,
- project._id,
- project.rootFolder[0]._id,
- destination
- )
- if (path) {
- await ProjectRootDocManager.promises.setRootDocFromName(project._id, path)
- }
- return project
- },
- async createProjectFromZipArchiveWithName(
- ownerId,
- proposedName,
- zipPath,
- attributes
- ) {
- attributes = attributes || {}
- const fixedProjectName = ProjectDetailsHandler.fixProjectName(proposedName)
- const projectName = await ProjectDetailsHandler.promises.generateUniqueName(
- ownerId,
- fixedProjectName
- )
- const project = await ProjectCreationHandler.promises.createBlankProject(
- ownerId,
- projectName,
- attributes
- )
- await ProjectUploadManager.promises.insertZipArchiveIntoFolder(
- ownerId,
- project._id,
- project.rootFolder[0]._id,
- zipPath
- )
- await ProjectRootDocManager.promises.setRootDocAutomatically(project._id)
- return project
- },
- _insertZipContentsIntoFolder: promisify(
- ProjectUploadManager._insertZipContentsIntoFolder
- ),
- insertZipArchiveIntoFolder(ownerId, projectId, folderId, zipPath) {
- return promisify(ProjectUploadManager.insertZipArchiveIntoFolder)(
- ownerId,
- projectId,
- folderId,
- zipPath
- )
- }
- }
- ProjectUploadManager.promises = promises
- module.exports = ProjectUploadManager
|