ProjectZipStreamManager.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /* eslint-disable
  2. camelcase,
  3. handle-callback-err,
  4. max-len,
  5. no-undef,
  6. */
  7. // TODO: This file was created by bulk-decaffeinate.
  8. // Fix any style issues and re-enable lint.
  9. /*
  10. * decaffeinate suggestions:
  11. * DS101: Remove unnecessary use of Array.from
  12. * DS102: Remove unnecessary code created because of implicit returns
  13. * DS207: Consider shorter variations of null checks
  14. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  15. */
  16. let ProjectZipStreamManager
  17. const archiver = require('archiver')
  18. const async = require('async')
  19. const logger = require('logger-sharelatex')
  20. const ProjectEntityHandler = require('../Project/ProjectEntityHandler')
  21. const ProjectGetter = require('../Project/ProjectGetter')
  22. const FileStoreHandler = require('../FileStore/FileStoreHandler')
  23. module.exports = ProjectZipStreamManager = {
  24. createZipStreamForMultipleProjects(project_ids, callback) {
  25. // We'll build up a zip file that contains multiple zip files
  26. if (callback == null) {
  27. callback = function(error, stream) {}
  28. }
  29. const archive = archiver('zip')
  30. archive.on('error', err =>
  31. logger.err(
  32. { err, project_ids },
  33. 'something went wrong building archive of project'
  34. )
  35. )
  36. callback(null, archive)
  37. const jobs = []
  38. for (let project_id of Array.from(project_ids || [])) {
  39. ;(project_id =>
  40. jobs.push(callback =>
  41. ProjectGetter.getProject(project_id, { name: true }, function(
  42. error,
  43. project
  44. ) {
  45. if (error != null) {
  46. return callback(error)
  47. }
  48. logger.log(
  49. { project_id, name: project.name },
  50. 'appending project to zip stream'
  51. )
  52. return ProjectZipStreamManager.createZipStreamForProject(
  53. project_id,
  54. function(error, stream) {
  55. if (error != null) {
  56. return callback(error)
  57. }
  58. archive.append(stream, { name: `${project.name}.zip` })
  59. return stream.on('end', function() {
  60. logger.log(
  61. { project_id, name: project.name },
  62. 'zip stream ended'
  63. )
  64. return callback()
  65. })
  66. }
  67. )
  68. })
  69. ))(project_id)
  70. }
  71. return async.series(jobs, function() {
  72. logger.log(
  73. { project_ids },
  74. 'finished creating zip stream of multiple projects'
  75. )
  76. return archive.finalize()
  77. })
  78. },
  79. createZipStreamForProject(project_id, callback) {
  80. if (callback == null) {
  81. callback = function(error, stream) {}
  82. }
  83. const archive = archiver('zip')
  84. // return stream immediately before we start adding things to it
  85. archive.on('error', err =>
  86. logger.err(
  87. { err, project_id },
  88. 'something went wrong building archive of project'
  89. )
  90. )
  91. callback(null, archive)
  92. return this.addAllDocsToArchive(project_id, archive, error => {
  93. if (error != null) {
  94. logger.error(
  95. { err: error, project_id },
  96. 'error adding docs to zip stream'
  97. )
  98. }
  99. return this.addAllFilesToArchive(project_id, archive, error => {
  100. if (error != null) {
  101. logger.error(
  102. { err: error, project_id },
  103. 'error adding files to zip stream'
  104. )
  105. }
  106. return archive.finalize()
  107. })
  108. })
  109. },
  110. addAllDocsToArchive(project_id, archive, callback) {
  111. if (callback == null) {
  112. callback = function(error) {}
  113. }
  114. return ProjectEntityHandler.getAllDocs(project_id, function(error, docs) {
  115. if (error != null) {
  116. return callback(error)
  117. }
  118. const jobs = []
  119. for (let path in docs) {
  120. const doc = docs[path]
  121. ;(function(path, doc) {
  122. if (path[0] === '/') {
  123. path = path.slice(1)
  124. }
  125. return jobs.push(function(callback) {
  126. logger.log({ project_id }, 'Adding doc')
  127. archive.append(doc.lines.join('\n'), { name: path })
  128. return callback()
  129. })
  130. })(path, doc)
  131. }
  132. return async.series(jobs, callback)
  133. })
  134. },
  135. addAllFilesToArchive(project_id, archive, callback) {
  136. if (callback == null) {
  137. callback = function(error) {}
  138. }
  139. return ProjectEntityHandler.getAllFiles(project_id, function(error, files) {
  140. if (error != null) {
  141. return callback(error)
  142. }
  143. const jobs = []
  144. for (let path in files) {
  145. const file = files[path]
  146. ;((path, file) =>
  147. jobs.push(callback =>
  148. FileStoreHandler.getFileStream(project_id, file._id, {}, function(
  149. error,
  150. stream
  151. ) {
  152. if (error != null) {
  153. logger.warn(
  154. { err: error, project_id, file_id: file._id },
  155. 'something went wrong adding file to zip archive'
  156. )
  157. return callback(err)
  158. }
  159. if (path[0] === '/') {
  160. path = path.slice(1)
  161. }
  162. archive.append(stream, { name: path })
  163. return stream.on('end', () => callback())
  164. })
  165. ))(path, file)
  166. }
  167. return async.parallelLimit(jobs, 5, callback)
  168. })
  169. }
  170. }