LinkedFilesController.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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('settings-sharelatex')
  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 (!LinkedFilesController.Agents.hasOwnProperty(provider)) {
  50. return null
  51. }
  52. if (!Array.from(Settings.enabledLinkedFileTypes).includes(provider)) {
  53. return null
  54. }
  55. return LinkedFilesController.Agents[provider]
  56. },
  57. createLinkedFile(req, res, next) {
  58. const { project_id } = req.params
  59. const { name, provider, data, parent_folder_id } = req.body
  60. const user_id = AuthenticationController.getLoggedInUserId(req)
  61. const Agent = LinkedFilesController._getAgent(provider)
  62. if (Agent == null) {
  63. return res.sendStatus(400)
  64. }
  65. data.provider = provider
  66. return Agent.createLinkedFile(
  67. project_id,
  68. data,
  69. name,
  70. parent_folder_id,
  71. user_id,
  72. function(err, newFileId) {
  73. if (err != null) {
  74. return LinkedFilesController.handleError(err, req, res, next)
  75. }
  76. return res.json({ new_file_id: newFileId })
  77. }
  78. )
  79. },
  80. refreshLinkedFile(req, res, next) {
  81. const { project_id, file_id } = req.params
  82. const user_id = AuthenticationController.getLoggedInUserId(req)
  83. return LinkedFilesHandler.getFileById(project_id, file_id, function(
  84. err,
  85. file,
  86. path,
  87. parentFolder
  88. ) {
  89. if (err != null) {
  90. return next(err)
  91. }
  92. if (file == null) {
  93. return res.sendStatus(404)
  94. }
  95. const { name } = file
  96. const { linkedFileData } = file
  97. if (
  98. linkedFileData == null ||
  99. (linkedFileData != null ? linkedFileData.provider : undefined) == null
  100. ) {
  101. return res.send(409)
  102. }
  103. const { provider } = linkedFileData
  104. const parent_folder_id = parentFolder._id
  105. const Agent = LinkedFilesController._getAgent(provider)
  106. if (Agent == null) {
  107. return res.sendStatus(400)
  108. }
  109. return Agent.refreshLinkedFile(
  110. project_id,
  111. linkedFileData,
  112. name,
  113. parent_folder_id,
  114. user_id,
  115. function(err, newFileId) {
  116. if (err != null) {
  117. return LinkedFilesController.handleError(err, req, res, next)
  118. }
  119. return res.json({ new_file_id: newFileId })
  120. }
  121. )
  122. })
  123. },
  124. handleError(error, req, res, next) {
  125. if (error instanceof BadDataError) {
  126. return res.status(400).send('The submitted data is not valid')
  127. } else if (error instanceof AccessDeniedError) {
  128. return res.status(403).send('You do not have access to this project')
  129. } else if (error instanceof BadDataError) {
  130. return res.status(400).send('The submitted data is not valid')
  131. } else if (error instanceof BadEntityTypeError) {
  132. return res.status(400).send('The file is the wrong type')
  133. } else if (error instanceof SourceFileNotFoundError) {
  134. return res.status(404).send('Source file not found')
  135. } else if (error instanceof ProjectNotFoundError) {
  136. return res.status(404).send('Project not found')
  137. } else if (error instanceof V1ProjectNotFoundError) {
  138. return res
  139. .status(409)
  140. .send(
  141. 'Sorry, the source project is not yet imported to Overleaf v2. Please import it to Overleaf v2 to refresh this file'
  142. )
  143. } else if (error instanceof OutputFileFetchFailedError) {
  144. return res.status(404).send('Could not get output file')
  145. } else if (error instanceof UrlFetchFailedError) {
  146. return res
  147. .status(422)
  148. .send(
  149. `Your URL could not be reached (${
  150. error.statusCode
  151. } 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 {
  168. return next(error)
  169. }
  170. }
  171. }