ProjectDownloadsController.mjs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. import Metrics from '@overleaf/metrics'
  14. import ProjectGetter from '../Project/ProjectGetter.mjs'
  15. import ProjectZipStreamManager from './ProjectZipStreamManager.mjs'
  16. import DocumentUpdaterHandler from '../DocumentUpdater/DocumentUpdaterHandler.mjs'
  17. import { prepareZipAttachment } from '../../infrastructure/Response.js'
  18. let ProjectDownloadsController
  19. // Keep in sync with the logic for PDF files in CompileController
  20. function getSafeProjectName(project) {
  21. return project.name.replace(/[^\p{L}\p{Nd}]/gu, '_')
  22. }
  23. export default ProjectDownloadsController = {
  24. downloadProject(req, res, next) {
  25. const projectId = req.params.Project_id
  26. Metrics.inc('zip-downloads')
  27. return DocumentUpdaterHandler.flushProjectToMongo(
  28. projectId,
  29. function (error) {
  30. if (error != null) {
  31. return next(error)
  32. }
  33. return ProjectGetter.getProject(
  34. projectId,
  35. { name: true },
  36. function (error, project) {
  37. if (error != null) {
  38. return next(error)
  39. }
  40. return ProjectZipStreamManager.createZipStreamForProject(
  41. projectId,
  42. function (error, stream) {
  43. if (error != null) {
  44. return next(error)
  45. }
  46. prepareZipAttachment(res, `${getSafeProjectName(project)}.zip`)
  47. return stream.pipe(res)
  48. }
  49. )
  50. }
  51. )
  52. }
  53. )
  54. },
  55. downloadMultipleProjects(req, res, next) {
  56. const projectIds = req.query.project_ids.split(',')
  57. Metrics.inc('zip-downloads-multiple')
  58. return DocumentUpdaterHandler.flushMultipleProjectsToMongo(
  59. projectIds,
  60. function (error) {
  61. if (error != null) {
  62. return next(error)
  63. }
  64. return ProjectZipStreamManager.createZipStreamForMultipleProjects(
  65. projectIds,
  66. function (error, stream) {
  67. if (error != null) {
  68. return next(error)
  69. }
  70. prepareZipAttachment(
  71. res,
  72. `Overleaf Projects (${projectIds.length} items).zip`
  73. )
  74. return stream.pipe(res)
  75. }
  76. )
  77. }
  78. )
  79. },
  80. }