ProjectUploadController.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 ProjectUploadController
  15. const logger = require('@overleaf/logger')
  16. const metrics = require('@overleaf/metrics')
  17. const fs = require('fs')
  18. const Path = require('path')
  19. const FileSystemImportManager = require('./FileSystemImportManager')
  20. const ProjectUploadManager = require('./ProjectUploadManager')
  21. const SessionManager = require('../Authentication/SessionManager')
  22. const Settings = require('@overleaf/settings')
  23. const { InvalidZipFileError } = require('./ArchiveErrors')
  24. const multer = require('multer')
  25. const upload = multer({
  26. dest: Settings.path.uploadFolder,
  27. limits: {
  28. fileSize: Settings.maxUploadSize,
  29. },
  30. })
  31. module.exports = ProjectUploadController = {
  32. uploadProject(req, res, next) {
  33. const timer = new metrics.Timer('project-upload')
  34. const user_id = SessionManager.getLoggedInUserId(req.session)
  35. const { originalname, path } = req.file
  36. const name = Path.basename(originalname, '.zip')
  37. return ProjectUploadManager.createProjectFromZipArchive(
  38. user_id,
  39. name,
  40. path,
  41. function (error, project) {
  42. fs.unlink(path, function () {})
  43. timer.done()
  44. if (error != null) {
  45. logger.error(
  46. { err: error, filePath: path, fileName: name },
  47. 'error uploading project'
  48. )
  49. if (error instanceof InvalidZipFileError) {
  50. return res.status(422).json({
  51. success: false,
  52. error: req.i18n.translate(error.message),
  53. })
  54. } else {
  55. return res.status(500).json({
  56. success: false,
  57. error: req.i18n.translate('upload_failed'),
  58. })
  59. }
  60. } else {
  61. return res.json({ success: true, project_id: project._id })
  62. }
  63. }
  64. )
  65. },
  66. uploadFile(req, res, next) {
  67. const timer = new metrics.Timer('file-upload')
  68. const name = req.file != null ? req.file.originalname : undefined
  69. const path = req.file != null ? req.file.path : undefined
  70. const project_id = req.params.Project_id
  71. const { folder_id } = req.query
  72. if (name == null || name.length === 0 || name.length > 150) {
  73. logger.err(
  74. { projectId: project_id, fileName: name },
  75. 'bad name when trying to upload file'
  76. )
  77. return res.status(422).json({
  78. success: false,
  79. error: 'invalid_filename',
  80. })
  81. }
  82. const user_id = SessionManager.getLoggedInUserId(req.session)
  83. return FileSystemImportManager.addEntity(
  84. user_id,
  85. project_id,
  86. folder_id,
  87. name,
  88. path,
  89. true,
  90. function (error, entity) {
  91. fs.unlink(path, function () {})
  92. timer.done()
  93. if (error != null) {
  94. logger.error(
  95. {
  96. err: error,
  97. projectId: project_id,
  98. filePath: path,
  99. fileName: name,
  100. folderId: folder_id,
  101. },
  102. 'error uploading file'
  103. )
  104. if (error.name === 'InvalidNameError') {
  105. return res.status(422).json({
  106. success: false,
  107. error: 'invalid_filename',
  108. })
  109. } else if (error.message === 'project_has_too_many_files') {
  110. return res.status(422).json({
  111. success: false,
  112. error: 'project_has_too_many_files',
  113. })
  114. } else {
  115. return res.status(422).json({ success: false })
  116. }
  117. } else {
  118. return res.json({
  119. success: true,
  120. entity_id: entity != null ? entity._id : undefined,
  121. entity_type: entity != null ? entity.type : undefined,
  122. })
  123. }
  124. }
  125. )
  126. },
  127. multerMiddleware(req, res, next) {
  128. if (upload == null) {
  129. return res
  130. .status(500)
  131. .json({ success: false, error: req.i18n.translate('upload_failed') })
  132. }
  133. return upload.single('qqfile')(req, res, function (err) {
  134. if (err instanceof multer.MulterError && err.code === 'LIMIT_FILE_SIZE') {
  135. return res
  136. .status(422)
  137. .json({ success: false, error: req.i18n.translate('file_too_large') })
  138. }
  139. return next(err)
  140. })
  141. },
  142. }