LinkedFilesController.js 6.2 KB

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