LinkedFilesController.js 6.0 KB

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