ProjectDownloadsController.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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('logger-sharelatex')
  16. const Metrics = require('@overleaf/metrics')
  17. const ProjectGetter = require('../Project/ProjectGetter')
  18. const ProjectZipStreamManager = require('./ProjectZipStreamManager')
  19. const DocumentUpdaterHandler = require('../DocumentUpdater/DocumentUpdaterHandler')
  20. module.exports = ProjectDownloadsController = {
  21. downloadProject(req, res, next) {
  22. const project_id = req.params.Project_id
  23. Metrics.inc('zip-downloads')
  24. return DocumentUpdaterHandler.flushProjectToMongo(
  25. project_id,
  26. function (error) {
  27. if (error != null) {
  28. return next(error)
  29. }
  30. return ProjectGetter.getProject(
  31. project_id,
  32. { name: true },
  33. function (error, project) {
  34. if (error != null) {
  35. return next(error)
  36. }
  37. return ProjectZipStreamManager.createZipStreamForProject(
  38. project_id,
  39. function (error, stream) {
  40. if (error != null) {
  41. return next(error)
  42. }
  43. res.setContentDisposition('attachment', {
  44. filename: `${project.name}.zip`,
  45. })
  46. res.contentType('application/zip')
  47. return stream.pipe(res)
  48. }
  49. )
  50. }
  51. )
  52. }
  53. )
  54. },
  55. downloadMultipleProjects(req, res, next) {
  56. const project_ids = req.query.project_ids.split(',')
  57. Metrics.inc('zip-downloads-multiple')
  58. return DocumentUpdaterHandler.flushMultipleProjectsToMongo(
  59. project_ids,
  60. function (error) {
  61. if (error != null) {
  62. return next(error)
  63. }
  64. return ProjectZipStreamManager.createZipStreamForMultipleProjects(
  65. project_ids,
  66. function (error, stream) {
  67. if (error != null) {
  68. return next(error)
  69. }
  70. res.setContentDisposition('attachment', {
  71. filename: `Overleaf Projects (${project_ids.length} items).zip`,
  72. })
  73. res.contentType('application/zip')
  74. return stream.pipe(res)
  75. }
  76. )
  77. }
  78. )
  79. },
  80. }