ProjectDownloadsController.js 2.5 KB

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