LinkedFilesController.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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('lodash')
  21. const AnalyticsManager = require('../../../../app/src/Features/Analytics/AnalyticsManager')
  22. const LinkedFilesHandler = require('./LinkedFilesHandler')
  23. const {
  24. CompileFailedError,
  25. UrlFetchFailedError,
  26. InvalidUrlError,
  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 {
  39. OutputFileFetchFailedError,
  40. FileTooLargeError,
  41. OError,
  42. } = require('../Errors/Errors')
  43. const Modules = require('../../infrastructure/Modules')
  44. const { plainTextResponse } = require('../../infrastructure/Response')
  45. const ReferencesHandler = require('../References/ReferencesHandler')
  46. const EditorRealTimeController = require('../Editor/EditorRealTimeController')
  47. function createLinkedFile(req, res, next) {
  48. const { project_id: projectId } = req.params
  49. const { name, provider, data, parent_folder_id: parentFolderId } = req.body
  50. const userId = SessionManager.getLoggedInUserId(req.session)
  51. const Agent = LinkedFilesController._getAgent(provider)
  52. if (Agent == null) {
  53. return res.sendStatus(400)
  54. }
  55. data.provider = provider
  56. data.importedAt = new Date().toISOString()
  57. return Agent.createLinkedFile(
  58. projectId,
  59. data,
  60. name,
  61. parentFolderId,
  62. userId,
  63. function (err, newFileId) {
  64. if (err != null) {
  65. return LinkedFilesController.handleError(err, req, res, next)
  66. }
  67. if (name.endsWith('.bib')) {
  68. AnalyticsManager.recordEventForUserInBackground(
  69. userId,
  70. 'linked-bib-file',
  71. {
  72. integration: provider,
  73. }
  74. )
  75. }
  76. return res.json({ new_file_id: newFileId })
  77. }
  78. )
  79. }
  80. function refreshLinkedFile(req, res, next) {
  81. const { project_id: projectId, file_id: fileId } = req.params
  82. const userId = SessionManager.getLoggedInUserId(req.session)
  83. return LinkedFilesHandler.getFileById(
  84. projectId,
  85. fileId,
  86. function (err, file, path, parentFolder) {
  87. if (err != null) {
  88. return next(err)
  89. }
  90. if (file == null) {
  91. return res.sendStatus(404)
  92. }
  93. const { name } = file
  94. const { linkedFileData } = file
  95. if (
  96. linkedFileData == null ||
  97. (linkedFileData != null ? linkedFileData.provider : undefined) == null
  98. ) {
  99. return res.sendStatus(409)
  100. }
  101. const { provider } = linkedFileData
  102. const parentFolderId = parentFolder._id
  103. const Agent = LinkedFilesController._getAgent(provider)
  104. if (Agent == null) {
  105. return res.sendStatus(400)
  106. }
  107. linkedFileData.importedAt = new Date().toISOString()
  108. Agent.refreshLinkedFile(
  109. projectId,
  110. linkedFileData,
  111. name,
  112. parentFolderId,
  113. userId,
  114. function (err, newFileId) {
  115. if (err != null) {
  116. return LinkedFilesController.handleError(err, req, res, next)
  117. }
  118. if (req.body.shouldReindexReferences) {
  119. ReferencesHandler.indexAll(projectId, function (error, data) {
  120. if (error) {
  121. OError.tag(error, 'failed to index references', {
  122. projectId,
  123. })
  124. return next(error)
  125. }
  126. EditorRealTimeController.emitToRoom(
  127. projectId,
  128. 'references:keys:updated',
  129. data.keys,
  130. true
  131. )
  132. res.json({ new_file_id: newFileId })
  133. })
  134. } else {
  135. res.json({ new_file_id: newFileId })
  136. }
  137. }
  138. )
  139. }
  140. )
  141. }
  142. module.exports = LinkedFilesController = {
  143. Agents: _.extend(
  144. {
  145. url: require('./UrlAgent'),
  146. project_file: require('./ProjectFileAgent'),
  147. project_output_file: require('./ProjectOutputFileAgent'),
  148. },
  149. Modules.linkedFileAgentsIncludes()
  150. ),
  151. _getAgent(provider) {
  152. if (
  153. !Object.prototype.hasOwnProperty.call(
  154. LinkedFilesController.Agents,
  155. provider
  156. )
  157. ) {
  158. return null
  159. }
  160. if (!Array.from(Settings.enabledLinkedFileTypes).includes(provider)) {
  161. return null
  162. }
  163. return LinkedFilesController.Agents[provider]
  164. },
  165. createLinkedFile,
  166. refreshLinkedFile,
  167. handleError(error, req, res, next) {
  168. if (error instanceof AccessDeniedError) {
  169. res.status(403)
  170. plainTextResponse(
  171. res,
  172. res.locals.translate(
  173. 'the_project_that_contains_this_file_is_not_shared_with_you'
  174. )
  175. )
  176. } else if (error instanceof BadDataError) {
  177. res.status(400)
  178. plainTextResponse(res, 'The submitted data is not valid')
  179. } else if (error instanceof BadEntityTypeError) {
  180. res.status(400)
  181. plainTextResponse(res, 'The file is the wrong type')
  182. } else if (error instanceof SourceFileNotFoundError) {
  183. res.status(404)
  184. plainTextResponse(res, 'Source file not found')
  185. } else if (error instanceof ProjectNotFoundError) {
  186. res.status(404)
  187. plainTextResponse(res, 'Project not found')
  188. } else if (error instanceof V1ProjectNotFoundError) {
  189. res.status(409)
  190. plainTextResponse(
  191. res,
  192. 'Sorry, the source project is not yet imported to Overleaf v2. Please import it to Overleaf v2 to refresh this file'
  193. )
  194. } else if (error instanceof CompileFailedError) {
  195. res.status(422)
  196. plainTextResponse(
  197. res,
  198. res.locals.translate('generic_linked_file_compile_error')
  199. )
  200. } else if (error instanceof OutputFileFetchFailedError) {
  201. res.status(404)
  202. plainTextResponse(res, 'Could not get output file')
  203. } else if (error instanceof UrlFetchFailedError) {
  204. res.status(422)
  205. if (error.cause instanceof FileTooLargeError) {
  206. plainTextResponse(res, 'File too large')
  207. } else {
  208. plainTextResponse(
  209. res,
  210. `Your URL could not be reached (${
  211. error.info?.status || error.cause?.info?.status
  212. } status code). Please check it and try again.`
  213. )
  214. }
  215. } else if (error instanceof InvalidUrlError) {
  216. res.status(422)
  217. plainTextResponse(
  218. res,
  219. 'Your URL is not valid. Please check it and try again.'
  220. )
  221. } else if (error instanceof NotOriginalImporterError) {
  222. res.status(400)
  223. plainTextResponse(
  224. res,
  225. 'You are not the user who originally imported this file'
  226. )
  227. } else if (error instanceof FeatureNotAvailableError) {
  228. res.status(400)
  229. plainTextResponse(res, 'This feature is not enabled on your account')
  230. } else if (error instanceof RemoteServiceError) {
  231. if (error.info?.statusCode === 403) {
  232. res.status(400).json({ relink: true })
  233. } else {
  234. res.status(502)
  235. plainTextResponse(res, 'The remote service produced an error')
  236. }
  237. } else if (error instanceof FileCannotRefreshError) {
  238. res.status(400)
  239. plainTextResponse(res, 'This file cannot be refreshed')
  240. } else if (error.message === 'project_has_too_many_files') {
  241. res.status(400)
  242. plainTextResponse(res, 'too many files')
  243. } else if (/\bECONNREFUSED\b/.test(error.message)) {
  244. res.status(500)
  245. plainTextResponse(res, 'Importing references is not currently available')
  246. } else if (error instanceof FileTooLargeError) {
  247. res.status(422)
  248. plainTextResponse(res, 'File too large')
  249. } else {
  250. next(error)
  251. }
  252. },
  253. }