ProjectUploadController.js 4.5 KB

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