ProjectUploadController.mjs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. import logger from '@overleaf/logger'
  2. import metrics from '@overleaf/metrics'
  3. import fs from 'node:fs'
  4. import Path from 'node:path'
  5. import FileSystemImportManager from './FileSystemImportManager.mjs'
  6. import ProjectUploadManager from './ProjectUploadManager.mjs'
  7. import SessionManager from '../Authentication/SessionManager.mjs'
  8. import EditorController from '../Editor/EditorController.mjs'
  9. import ProjectLocator from '../Project/ProjectLocator.mjs'
  10. import Settings from '@overleaf/settings'
  11. import { InvalidZipFileError } from './ArchiveErrors.mjs'
  12. import multer from 'multer'
  13. import lodash from 'lodash'
  14. import { expressify } from '@overleaf/promise-utils'
  15. import { DuplicateNameError } from '../Errors/Errors.js'
  16. const defaultsDeep = lodash.defaultsDeep
  17. const upload = multer(
  18. defaultsDeep(
  19. {
  20. dest: Settings.path.uploadFolder,
  21. limits: {
  22. fileSize: Settings.maxUploadSize,
  23. },
  24. },
  25. Settings.multerOptions
  26. )
  27. )
  28. function uploadProject(req, res, next) {
  29. const timer = new metrics.Timer('project-upload')
  30. const userId = SessionManager.getLoggedInUserId(req.session)
  31. const { path } = req.file
  32. const name = Path.basename(req.body.name, '.zip')
  33. return ProjectUploadManager.createProjectFromZipArchive(
  34. userId,
  35. name,
  36. path,
  37. function (error, project) {
  38. fs.unlink(path, function () {})
  39. timer.done()
  40. if (error != null) {
  41. logger.error(
  42. { err: error, filePath: path, fileName: name },
  43. 'error uploading project'
  44. )
  45. if (error instanceof InvalidZipFileError) {
  46. return res.status(422).json({
  47. success: false,
  48. error: req.i18n.translate(error.message),
  49. })
  50. } else {
  51. return res.status(500).json({
  52. success: false,
  53. error: req.i18n.translate('upload_failed'),
  54. })
  55. }
  56. } else {
  57. return res.json({ success: true, project_id: project._id })
  58. }
  59. }
  60. )
  61. }
  62. async function uploadFile(req, res, next) {
  63. const timer = new metrics.Timer('file-upload')
  64. const name = req.body.name
  65. const { path } = req.file
  66. const projectId = req.params.Project_id
  67. const userId = SessionManager.getLoggedInUserId(req.session)
  68. let { folder_id: folderId } = req.query
  69. if (name == null || name.length === 0 || name.length > 150) {
  70. fs.unlink(path, function () {})
  71. return res.status(422).json({
  72. success: false,
  73. error: 'invalid_filename',
  74. })
  75. }
  76. // preserve the directory structure from an uploaded folder
  77. const { relativePath } = req.body
  78. // NOTE: Uppy sends a "null" string for `relativePath` when the file is not nested in a folder
  79. if (relativePath && relativePath !== 'null') {
  80. const { path } = await ProjectLocator.promises.findElement({
  81. project_id: projectId,
  82. element_id: folderId,
  83. type: 'folder',
  84. })
  85. const { lastFolder } = await EditorController.promises.mkdirp(
  86. projectId,
  87. Path.dirname(Path.join('/', path.fileSystem, relativePath)),
  88. userId
  89. )
  90. folderId = lastFolder._id
  91. }
  92. return FileSystemImportManager.addEntity(
  93. userId,
  94. projectId,
  95. folderId,
  96. name,
  97. path,
  98. true,
  99. function (error, entity) {
  100. fs.unlink(path, function () {})
  101. timer.done()
  102. if (error != null) {
  103. if (error.name === 'InvalidNameError') {
  104. return res.status(422).json({
  105. success: false,
  106. error: 'invalid_filename',
  107. })
  108. } else if (error instanceof DuplicateNameError) {
  109. return res.status(422).json({
  110. success: false,
  111. error: 'duplicate_file_name',
  112. })
  113. } else if (error.message === 'project_has_too_many_files') {
  114. return res.status(422).json({
  115. success: false,
  116. error: 'project_has_too_many_files',
  117. })
  118. } else if (error.message === 'folder_not_found') {
  119. return res.status(422).json({
  120. success: false,
  121. error: 'folder_not_found',
  122. })
  123. } else {
  124. logger.error(
  125. {
  126. err: error,
  127. projectId,
  128. filePath: path,
  129. fileName: name,
  130. folderId,
  131. },
  132. 'error uploading file'
  133. )
  134. return res.status(422).json({ success: false })
  135. }
  136. } else {
  137. return res.json({
  138. success: true,
  139. entity_id: entity?._id,
  140. entity_type: entity?.type,
  141. hash: entity?.hash,
  142. })
  143. }
  144. }
  145. )
  146. }
  147. function multerMiddleware(req, res, next) {
  148. if (upload == null) {
  149. return res
  150. .status(500)
  151. .json({ success: false, error: req.i18n.translate('upload_failed') })
  152. }
  153. return upload.single('qqfile')(req, res, function (err) {
  154. if (err instanceof multer.MulterError && err.code === 'LIMIT_FILE_SIZE') {
  155. return res
  156. .status(422)
  157. .json({ success: false, error: req.i18n.translate('file_too_large') })
  158. }
  159. if (err) return next(err)
  160. if (!req.file?.path) {
  161. logger.info({ req }, 'missing req.file.path on upload')
  162. return res
  163. .status(400)
  164. .json({ success: false, error: 'invalid_upload_request' })
  165. }
  166. next()
  167. })
  168. }
  169. export default {
  170. uploadProject,
  171. uploadFile: expressify(uploadFile),
  172. multerMiddleware,
  173. }