ProjectDownloadsController.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /* eslint-disable
  2. max-len,
  3. no-unused-vars,
  4. */
  5. // TODO: This file was created by bulk-decaffeinate.
  6. // Fix any style issues and re-enable lint.
  7. /*
  8. * decaffeinate suggestions:
  9. * DS102: Remove unnecessary code created because of implicit returns
  10. * DS207: Consider shorter variations of null checks
  11. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  12. */
  13. let ProjectDownloadsController
  14. const logger = require('@overleaf/logger')
  15. const Metrics = require('@overleaf/metrics')
  16. const ProjectGetter = require('../Project/ProjectGetter')
  17. const ProjectZipStreamManager = require('./ProjectZipStreamManager')
  18. const DocumentUpdaterHandler = require('../DocumentUpdater/DocumentUpdaterHandler')
  19. const { prepareZipAttachment } = require('../../infrastructure/Response')
  20. module.exports = ProjectDownloadsController = {
  21. downloadProject(req, res, next) {
  22. const projectId = req.params.Project_id
  23. Metrics.inc('zip-downloads')
  24. return DocumentUpdaterHandler.flushProjectToMongo(
  25. projectId,
  26. function (error) {
  27. if (error != null) {
  28. return next(error)
  29. }
  30. return ProjectGetter.getProject(
  31. projectId,
  32. { name: true },
  33. function (error, project) {
  34. if (error != null) {
  35. return next(error)
  36. }
  37. return ProjectZipStreamManager.createZipStreamForProject(
  38. projectId,
  39. function (error, stream) {
  40. if (error != null) {
  41. return next(error)
  42. }
  43. prepareZipAttachment(res, `${project.name}.zip`)
  44. return stream.pipe(res)
  45. }
  46. )
  47. }
  48. )
  49. }
  50. )
  51. },
  52. downloadMultipleProjects(req, res, next) {
  53. const projectIds = req.query.project_ids.split(',')
  54. Metrics.inc('zip-downloads-multiple')
  55. return DocumentUpdaterHandler.flushMultipleProjectsToMongo(
  56. projectIds,
  57. function (error) {
  58. if (error != null) {
  59. return next(error)
  60. }
  61. return ProjectZipStreamManager.createZipStreamForMultipleProjects(
  62. projectIds,
  63. function (error, stream) {
  64. if (error != null) {
  65. return next(error)
  66. }
  67. prepareZipAttachment(
  68. res,
  69. `Overleaf Projects (${projectIds.length} items).zip`
  70. )
  71. return stream.pipe(res)
  72. }
  73. )
  74. }
  75. )
  76. },
  77. }