ProjectDownloadsController.js 2.4 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('logger-sharelatex')
  16. const Metrics = require('metrics-sharelatex')
  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(project_id, function(
  25. error
  26. ) {
  27. if (error != null) {
  28. return next(error)
  29. }
  30. return ProjectGetter.getProject(project_id, { name: true }, function(
  31. error,
  32. project
  33. ) {
  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. 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. res.setContentDisposition('attachment', {
  69. filename: `Overleaf Projects (${project_ids.length} items).zip`
  70. })
  71. res.contentType('application/zip')
  72. return stream.pipe(res)
  73. }
  74. )
  75. }
  76. )
  77. }
  78. }