LinkedFilesController.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. * DS101: Remove unnecessary use of Array.from
  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 LinkedFilesController
  15. const SessionManager = require('../Authentication/SessionManager')
  16. const EditorController = require('../Editor/EditorController')
  17. const ProjectLocator = require('../Project/ProjectLocator')
  18. const Settings = require('@overleaf/settings')
  19. const logger = require('@overleaf/logger')
  20. const _ = require('underscore')
  21. const LinkedFilesHandler = require('./LinkedFilesHandler')
  22. const {
  23. CompileFailedError,
  24. UrlFetchFailedError,
  25. InvalidUrlError,
  26. AccessDeniedError,
  27. BadEntityTypeError,
  28. BadDataError,
  29. ProjectNotFoundError,
  30. V1ProjectNotFoundError,
  31. SourceFileNotFoundError,
  32. NotOriginalImporterError,
  33. FeatureNotAvailableError,
  34. RemoteServiceError,
  35. FileCannotRefreshError,
  36. } = require('./LinkedFilesErrors')
  37. const {
  38. OutputFileFetchFailedError,
  39. FileTooLargeError,
  40. } = require('../Errors/Errors')
  41. const Modules = require('../../infrastructure/Modules')
  42. const { plainTextResponse } = require('../../infrastructure/Response')
  43. module.exports = LinkedFilesController = {
  44. Agents: _.extend(
  45. {
  46. url: require('./UrlAgent'),
  47. project_file: require('./ProjectFileAgent'),
  48. project_output_file: require('./ProjectOutputFileAgent'),
  49. },
  50. Modules.linkedFileAgentsIncludes()
  51. ),
  52. _getAgent(provider) {
  53. if (
  54. !Object.prototype.hasOwnProperty.call(
  55. LinkedFilesController.Agents,
  56. provider
  57. )
  58. ) {
  59. return null
  60. }
  61. if (!Array.from(Settings.enabledLinkedFileTypes).includes(provider)) {
  62. return null
  63. }
  64. return LinkedFilesController.Agents[provider]
  65. },
  66. createLinkedFile(req, res, next) {
  67. const { project_id: projectId } = req.params
  68. const { name, provider, data, parent_folder_id: parentFolderId } = req.body
  69. const userId = SessionManager.getLoggedInUserId(req.session)
  70. const Agent = LinkedFilesController._getAgent(provider)
  71. if (Agent == null) {
  72. return res.sendStatus(400)
  73. }
  74. data.provider = provider
  75. return Agent.createLinkedFile(
  76. projectId,
  77. data,
  78. name,
  79. parentFolderId,
  80. userId,
  81. function (err, newFileId) {
  82. if (err != null) {
  83. return LinkedFilesController.handleError(err, req, res, next)
  84. }
  85. return res.json({ new_file_id: newFileId })
  86. }
  87. )
  88. },
  89. refreshLinkedFile(req, res, next) {
  90. const { project_id: projectId, file_id: fileId } = req.params
  91. const userId = SessionManager.getLoggedInUserId(req.session)
  92. return LinkedFilesHandler.getFileById(
  93. projectId,
  94. fileId,
  95. function (err, file, path, parentFolder) {
  96. if (err != null) {
  97. return next(err)
  98. }
  99. if (file == null) {
  100. return res.sendStatus(404)
  101. }
  102. const { name } = file
  103. const { linkedFileData } = file
  104. if (
  105. linkedFileData == null ||
  106. (linkedFileData != null ? linkedFileData.provider : undefined) == null
  107. ) {
  108. return res.sendStatus(409)
  109. }
  110. const { provider } = linkedFileData
  111. const parentFolderId = parentFolder._id
  112. const Agent = LinkedFilesController._getAgent(provider)
  113. if (Agent == null) {
  114. return res.sendStatus(400)
  115. }
  116. return Agent.refreshLinkedFile(
  117. projectId,
  118. linkedFileData,
  119. name,
  120. parentFolderId,
  121. userId,
  122. function (err, newFileId) {
  123. if (err != null) {
  124. return LinkedFilesController.handleError(err, req, res, next)
  125. }
  126. return res.json({ new_file_id: newFileId })
  127. }
  128. )
  129. }
  130. )
  131. },
  132. handleError(error, req, res, next) {
  133. if (error instanceof AccessDeniedError) {
  134. res.status(403)
  135. plainTextResponse(
  136. res,
  137. res.locals.translate(
  138. 'the_project_that_contains_this_file_is_not_shared_with_you'
  139. )
  140. )
  141. } else if (error instanceof BadDataError) {
  142. res.status(400)
  143. plainTextResponse(res, 'The submitted data is not valid')
  144. } else if (error instanceof BadEntityTypeError) {
  145. res.status(400)
  146. plainTextResponse(res, 'The file is the wrong type')
  147. } else if (error instanceof SourceFileNotFoundError) {
  148. res.status(404)
  149. plainTextResponse(res, 'Source file not found')
  150. } else if (error instanceof ProjectNotFoundError) {
  151. res.status(404)
  152. plainTextResponse(res, 'Project not found')
  153. } else if (error instanceof V1ProjectNotFoundError) {
  154. res.status(409)
  155. plainTextResponse(
  156. res,
  157. 'Sorry, the source project is not yet imported to Overleaf v2. Please import it to Overleaf v2 to refresh this file'
  158. )
  159. } else if (error instanceof CompileFailedError) {
  160. res.status(422)
  161. plainTextResponse(
  162. res,
  163. res.locals.translate('generic_linked_file_compile_error')
  164. )
  165. } else if (error instanceof OutputFileFetchFailedError) {
  166. res.status(404)
  167. plainTextResponse(res, 'Could not get output file')
  168. } else if (error instanceof UrlFetchFailedError) {
  169. res.status(422)
  170. plainTextResponse(
  171. res,
  172. `Your URL could not be reached (${error.statusCode} status code). Please check it and try again.`
  173. )
  174. } else if (error instanceof InvalidUrlError) {
  175. res.status(422)
  176. plainTextResponse(
  177. res,
  178. 'Your URL is not valid. Please check it and try again.'
  179. )
  180. } else if (error instanceof NotOriginalImporterError) {
  181. res.status(400)
  182. plainTextResponse(
  183. res,
  184. 'You are not the user who originally imported this file'
  185. )
  186. } else if (error instanceof FeatureNotAvailableError) {
  187. res.status(400)
  188. plainTextResponse(res, 'This feature is not enabled on your account')
  189. } else if (error instanceof RemoteServiceError) {
  190. if (error.info?.statusCode === 403) {
  191. res.status(400).json({ relink: true })
  192. } else {
  193. res.status(502)
  194. plainTextResponse(res, 'The remote service produced an error')
  195. }
  196. } else if (error instanceof FileCannotRefreshError) {
  197. res.status(400)
  198. plainTextResponse(res, 'This file cannot be refreshed')
  199. } else if (error.message === 'project_has_too_many_files') {
  200. res.status(400)
  201. plainTextResponse(res, 'too many files')
  202. } else if (/\bECONNREFUSED\b/.test(error.message)) {
  203. res.status(500)
  204. plainTextResponse(res, 'Importing references is not currently available')
  205. } else if (error instanceof FileTooLargeError) {
  206. res.status(422)
  207. plainTextResponse(res, 'File too large')
  208. } else {
  209. next(error)
  210. }
  211. },
  212. }