LinkedFilesController.mjs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /* eslint-disable
  2. max-len,
  3. */
  4. // TODO: This file was created by bulk-decaffeinate.
  5. // Fix any style issues and re-enable lint.
  6. /*
  7. * decaffeinate suggestions:
  8. * DS101: Remove unnecessary use of Array.from
  9. * DS102: Remove unnecessary code created because of implicit returns
  10. * DS207: Consider shorter variations of null checks
  11. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  12. */
  13. import SessionManager from '../Authentication/SessionManager.js'
  14. import Settings from '@overleaf/settings'
  15. import _ from 'lodash'
  16. import AnalyticsManager from '../../../../app/src/Features/Analytics/AnalyticsManager.js'
  17. import LinkedFilesHandler from './LinkedFilesHandler.mjs'
  18. import {
  19. CompileFailedError,
  20. UrlFetchFailedError,
  21. InvalidUrlError,
  22. AccessDeniedError,
  23. BadEntityTypeError,
  24. BadDataError,
  25. ProjectNotFoundError,
  26. V1ProjectNotFoundError,
  27. SourceFileNotFoundError,
  28. NotOriginalImporterError,
  29. FeatureNotAvailableError,
  30. RemoteServiceError,
  31. FileCannotRefreshError,
  32. } from './LinkedFilesErrors.js'
  33. import {
  34. OutputFileFetchFailedError,
  35. FileTooLargeError,
  36. OError,
  37. } from '../Errors/Errors.js'
  38. import Modules from '../../infrastructure/Modules.js'
  39. import { plainTextResponse } from '../../infrastructure/Response.js'
  40. import { z, zz, validateReq } from '../../infrastructure/Validation.js'
  41. import ReferencesHandler from '../References/ReferencesHandler.mjs'
  42. import EditorRealTimeController from '../Editor/EditorRealTimeController.js'
  43. import { expressify } from '@overleaf/promise-utils'
  44. import ProjectOutputFileAgent from './ProjectOutputFileAgent.mjs'
  45. import ProjectFileAgent from './ProjectFileAgent.mjs'
  46. import UrlAgent from './UrlAgent.mjs'
  47. let LinkedFilesController
  48. const createLinkedFileSchema = z.object({
  49. params: z.object({
  50. project_id: zz.objectId(),
  51. }),
  52. body: z.object({
  53. name: z.string(),
  54. provider: z.string(),
  55. data: z.object({}).passthrough(),
  56. parent_folder_id: zz.objectId(),
  57. }),
  58. })
  59. async function createLinkedFile(req, res, next) {
  60. const { params, body } = validateReq(req, createLinkedFileSchema)
  61. const { project_id: projectId } = params
  62. const { name, provider, data, parent_folder_id: parentFolderId } = body
  63. const userId = SessionManager.getLoggedInUserId(req.session)
  64. const Agent = await LinkedFilesController._getAgent(provider)
  65. if (Agent == null) {
  66. return res.sendStatus(400)
  67. }
  68. data.provider = provider
  69. data.importedAt = new Date().toISOString()
  70. try {
  71. const newFileId = await Agent.promises.createLinkedFile(
  72. projectId,
  73. data,
  74. name,
  75. parentFolderId,
  76. userId
  77. )
  78. if (name.endsWith('.bib')) {
  79. AnalyticsManager.recordEventForUserInBackground(
  80. userId,
  81. 'linked-bib-file',
  82. {
  83. integration: provider,
  84. }
  85. )
  86. }
  87. return res.json({ new_file_id: newFileId })
  88. } catch (err) {
  89. return LinkedFilesController.handleError(err, req, res, next)
  90. }
  91. }
  92. async function refreshLinkedFile(req, res, next) {
  93. const { project_id: projectId, file_id: fileId } = req.params
  94. const { clientId } = req.body
  95. const userId = SessionManager.getLoggedInUserId(req.session)
  96. const { file, parentFolder } = await LinkedFilesHandler.promises.getFileById(
  97. projectId,
  98. fileId
  99. )
  100. if (file == null) {
  101. return res.sendStatus(404)
  102. }
  103. const { name } = file
  104. const { linkedFileData } = file
  105. if (
  106. linkedFileData == null ||
  107. (linkedFileData != null ? linkedFileData.provider : undefined) == null
  108. ) {
  109. return res.sendStatus(409)
  110. }
  111. const { provider } = linkedFileData
  112. const parentFolderId = parentFolder._id
  113. const Agent = await LinkedFilesController._getAgent(provider)
  114. if (Agent == null) {
  115. return res.sendStatus(400)
  116. }
  117. linkedFileData.importedAt = new Date().toISOString()
  118. let newFileId
  119. try {
  120. newFileId = await Agent.promises.refreshLinkedFile(
  121. projectId,
  122. linkedFileData,
  123. name,
  124. parentFolderId,
  125. userId
  126. )
  127. } catch (err) {
  128. return LinkedFilesController.handleError(err, req, res, next)
  129. }
  130. if (req.body.shouldReindexReferences) {
  131. let data
  132. try {
  133. data = await ReferencesHandler.promises.indexAll(projectId)
  134. } catch (error) {
  135. OError.tag(error, 'failed to index references', {
  136. projectId,
  137. })
  138. return next(error)
  139. }
  140. EditorRealTimeController.emitToRoom(
  141. projectId,
  142. 'references:keys:updated',
  143. data.keys,
  144. true,
  145. clientId
  146. )
  147. res.json({ new_file_id: newFileId })
  148. } else {
  149. res.json({ new_file_id: newFileId })
  150. }
  151. }
  152. export default LinkedFilesController = {
  153. Agents: null,
  154. async _cacheAgents() {
  155. if (!LinkedFilesController.Agents) {
  156. LinkedFilesController.Agents = _.extend(
  157. {
  158. url: UrlAgent,
  159. project_file: ProjectFileAgent,
  160. project_output_file: ProjectOutputFileAgent,
  161. },
  162. await Modules.linkedFileAgentsIncludes()
  163. )
  164. }
  165. },
  166. async _getAgent(provider) {
  167. await LinkedFilesController._cacheAgents()
  168. if (
  169. !Object.prototype.hasOwnProperty.call(
  170. LinkedFilesController.Agents,
  171. provider
  172. )
  173. ) {
  174. return null
  175. }
  176. if (!Array.from(Settings.enabledLinkedFileTypes).includes(provider)) {
  177. return null
  178. }
  179. return LinkedFilesController.Agents[provider]
  180. },
  181. createLinkedFile: expressify(createLinkedFile),
  182. refreshLinkedFile: expressify(refreshLinkedFile),
  183. handleError(error, req, res, next) {
  184. if (error instanceof AccessDeniedError) {
  185. res.status(403)
  186. plainTextResponse(
  187. res,
  188. res.locals.translate(
  189. 'the_project_that_contains_this_file_is_not_shared_with_you'
  190. )
  191. )
  192. } else if (error instanceof BadDataError) {
  193. res.status(400)
  194. plainTextResponse(res, 'The submitted data is not valid')
  195. } else if (error instanceof BadEntityTypeError) {
  196. res.status(400)
  197. plainTextResponse(res, 'The file is the wrong type')
  198. } else if (error instanceof SourceFileNotFoundError) {
  199. res.status(404)
  200. plainTextResponse(res, 'Source file not found')
  201. } else if (error instanceof ProjectNotFoundError) {
  202. res.status(404)
  203. plainTextResponse(res, 'Project not found')
  204. } else if (error instanceof V1ProjectNotFoundError) {
  205. res.status(409)
  206. plainTextResponse(
  207. res,
  208. 'Sorry, the source project is not yet imported to Overleaf v2. Please import it to Overleaf v2 to refresh this file'
  209. )
  210. } else if (error instanceof CompileFailedError) {
  211. res.status(422)
  212. plainTextResponse(
  213. res,
  214. res.locals.translate('generic_linked_file_compile_error')
  215. )
  216. } else if (error instanceof OutputFileFetchFailedError) {
  217. res.status(404)
  218. plainTextResponse(res, 'Could not get output file')
  219. } else if (error instanceof UrlFetchFailedError) {
  220. res.status(422)
  221. if (error.cause instanceof FileTooLargeError) {
  222. plainTextResponse(res, 'File too large')
  223. } else {
  224. plainTextResponse(
  225. res,
  226. `Your URL could not be reached (${
  227. error.info?.status || error.cause?.info?.status
  228. } status code). Please check it and try again.`
  229. )
  230. }
  231. } else if (error instanceof InvalidUrlError) {
  232. res.status(422)
  233. plainTextResponse(
  234. res,
  235. 'Your URL is not valid. Please check it and try again.'
  236. )
  237. } else if (error instanceof NotOriginalImporterError) {
  238. res.status(400)
  239. plainTextResponse(
  240. res,
  241. 'You are not the user who originally imported this file'
  242. )
  243. } else if (error instanceof FeatureNotAvailableError) {
  244. res.status(400)
  245. plainTextResponse(res, 'This feature is not enabled on your account')
  246. } else if (error instanceof RemoteServiceError) {
  247. if (error.info?.statusCode === 403) {
  248. res.status(400).json({ relink: true })
  249. } else {
  250. res.status(502)
  251. plainTextResponse(res, 'The remote service produced an error')
  252. }
  253. } else if (error instanceof FileCannotRefreshError) {
  254. res.status(400)
  255. plainTextResponse(res, 'This file cannot be refreshed')
  256. } else if (error.message === 'project_has_too_many_files') {
  257. res.status(400)
  258. plainTextResponse(res, 'too many files')
  259. } else if (/\bECONNREFUSED\b/.test(error.message)) {
  260. res.status(500)
  261. plainTextResponse(res, 'Importing references is not currently available')
  262. } else if (error instanceof FileTooLargeError) {
  263. res.status(422)
  264. plainTextResponse(res, 'File too large')
  265. } else {
  266. next(error)
  267. }
  268. },
  269. }