2019-05-29 05:21:06 -04:00
|
|
|
/* eslint-disable
|
|
|
|
camelcase,
|
2020-12-15 05:23:54 -05:00
|
|
|
node/handle-callback-err,
|
2019-05-29 05:21:06 -04:00
|
|
|
max-len,
|
|
|
|
no-undef,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS101: Remove unnecessary use of Array.from
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
let ProjectZipStreamManager
|
|
|
|
const archiver = require('archiver')
|
|
|
|
const async = require('async')
|
|
|
|
const logger = require('logger-sharelatex')
|
|
|
|
const ProjectEntityHandler = require('../Project/ProjectEntityHandler')
|
|
|
|
const ProjectGetter = require('../Project/ProjectGetter')
|
|
|
|
const FileStoreHandler = require('../FileStore/FileStoreHandler')
|
|
|
|
|
|
|
|
module.exports = ProjectZipStreamManager = {
|
|
|
|
createZipStreamForMultipleProjects(project_ids, callback) {
|
|
|
|
// We'll build up a zip file that contains multiple zip files
|
|
|
|
|
|
|
|
if (callback == null) {
|
2021-04-14 09:17:21 -04:00
|
|
|
callback = function (error, stream) {}
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
const archive = archiver('zip')
|
|
|
|
archive.on('error', err =>
|
|
|
|
logger.err(
|
|
|
|
{ err, project_ids },
|
|
|
|
'something went wrong building archive of project'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
callback(null, archive)
|
|
|
|
|
|
|
|
const jobs = []
|
2021-05-05 09:05:04 -04:00
|
|
|
for (const project_id of Array.from(project_ids || [])) {
|
2019-05-29 05:21:06 -04:00
|
|
|
;(project_id =>
|
|
|
|
jobs.push(callback =>
|
2021-04-14 09:17:21 -04:00
|
|
|
ProjectGetter.getProject(
|
|
|
|
project_id,
|
|
|
|
{ name: true },
|
|
|
|
function (error, project) {
|
|
|
|
if (error != null) {
|
|
|
|
return callback(error)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2021-04-14 09:17:21 -04:00
|
|
|
logger.log(
|
|
|
|
{ project_id, name: project.name },
|
|
|
|
'appending project to zip stream'
|
|
|
|
)
|
|
|
|
return ProjectZipStreamManager.createZipStreamForProject(
|
|
|
|
project_id,
|
|
|
|
function (error, stream) {
|
|
|
|
if (error != null) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
archive.append(stream, { name: `${project.name}.zip` })
|
|
|
|
return stream.on('end', function () {
|
|
|
|
logger.log(
|
|
|
|
{ project_id, name: project.name },
|
|
|
|
'zip stream ended'
|
|
|
|
)
|
|
|
|
return callback()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
2019-05-29 05:21:06 -04:00
|
|
|
))(project_id)
|
|
|
|
}
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
return async.series(jobs, function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
logger.log(
|
|
|
|
{ project_ids },
|
|
|
|
'finished creating zip stream of multiple projects'
|
|
|
|
)
|
|
|
|
return archive.finalize()
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
createZipStreamForProject(project_id, callback) {
|
|
|
|
if (callback == null) {
|
2021-04-14 09:17:21 -04:00
|
|
|
callback = function (error, stream) {}
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
const archive = archiver('zip')
|
|
|
|
// return stream immediately before we start adding things to it
|
|
|
|
archive.on('error', err =>
|
|
|
|
logger.err(
|
|
|
|
{ err, project_id },
|
|
|
|
'something went wrong building archive of project'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
callback(null, archive)
|
|
|
|
return this.addAllDocsToArchive(project_id, archive, error => {
|
|
|
|
if (error != null) {
|
|
|
|
logger.error(
|
|
|
|
{ err: error, project_id },
|
|
|
|
'error adding docs to zip stream'
|
|
|
|
)
|
|
|
|
}
|
|
|
|
return this.addAllFilesToArchive(project_id, archive, error => {
|
|
|
|
if (error != null) {
|
|
|
|
logger.error(
|
|
|
|
{ err: error, project_id },
|
|
|
|
'error adding files to zip stream'
|
|
|
|
)
|
|
|
|
}
|
|
|
|
return archive.finalize()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
addAllDocsToArchive(project_id, archive, callback) {
|
|
|
|
if (callback == null) {
|
2021-04-14 09:17:21 -04:00
|
|
|
callback = function (error) {}
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2021-04-14 09:17:21 -04:00
|
|
|
return ProjectEntityHandler.getAllDocs(project_id, function (error, docs) {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (error != null) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
const jobs = []
|
2021-05-05 09:05:04 -04:00
|
|
|
for (const path in docs) {
|
2019-05-29 05:21:06 -04:00
|
|
|
const doc = docs[path]
|
2021-04-14 09:17:21 -04:00
|
|
|
;(function (path, doc) {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (path[0] === '/') {
|
|
|
|
path = path.slice(1)
|
|
|
|
}
|
2021-04-14 09:17:21 -04:00
|
|
|
return jobs.push(function (callback) {
|
2019-05-29 05:21:06 -04:00
|
|
|
logger.log({ project_id }, 'Adding doc')
|
|
|
|
archive.append(doc.lines.join('\n'), { name: path })
|
|
|
|
return callback()
|
|
|
|
})
|
|
|
|
})(path, doc)
|
|
|
|
}
|
|
|
|
return async.series(jobs, callback)
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
addAllFilesToArchive(project_id, archive, callback) {
|
|
|
|
if (callback == null) {
|
2021-04-14 09:17:21 -04:00
|
|
|
callback = function (error) {}
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2021-04-14 09:17:21 -04:00
|
|
|
return ProjectEntityHandler.getAllFiles(
|
|
|
|
project_id,
|
|
|
|
function (error, files) {
|
|
|
|
if (error != null) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
const jobs = []
|
2021-05-05 09:05:04 -04:00
|
|
|
for (const path in files) {
|
2021-04-14 09:17:21 -04:00
|
|
|
const file = files[path]
|
|
|
|
;((path, file) =>
|
|
|
|
jobs.push(callback =>
|
|
|
|
FileStoreHandler.getFileStream(
|
|
|
|
project_id,
|
|
|
|
file._id,
|
|
|
|
{},
|
|
|
|
function (error, stream) {
|
|
|
|
if (error != null) {
|
|
|
|
logger.warn(
|
|
|
|
{ err: error, project_id, file_id: file._id },
|
|
|
|
'something went wrong adding file to zip archive'
|
|
|
|
)
|
|
|
|
return callback(err)
|
|
|
|
}
|
|
|
|
if (path[0] === '/') {
|
|
|
|
path = path.slice(1)
|
|
|
|
}
|
|
|
|
archive.append(stream, { name: path })
|
|
|
|
return stream.on('end', () => callback())
|
|
|
|
}
|
|
|
|
)
|
|
|
|
))(path, file)
|
|
|
|
}
|
|
|
|
return async.parallelLimit(jobs, 5, callback)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2021-04-14 09:17:21 -04:00
|
|
|
)
|
2021-04-27 03:52:58 -04:00
|
|
|
},
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|