| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263 |
- /* eslint-disable
- max-len,
- no-unused-vars,
- */
- // TODO: This file was created by bulk-decaffeinate.
- // Fix any style issues and re-enable lint.
- /*
- * decaffeinate suggestions:
- * DS101: Remove unnecessary use of Array.from
- * DS102: Remove unnecessary code created because of implicit returns
- * DS207: Consider shorter variations of null checks
- * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
- */
- let LinkedFilesController
- const SessionManager = require('../Authentication/SessionManager')
- const EditorController = require('../Editor/EditorController')
- const ProjectLocator = require('../Project/ProjectLocator')
- const Settings = require('@overleaf/settings')
- const logger = require('@overleaf/logger')
- const _ = require('lodash')
- const AnalyticsManager = require('../../../../app/src/Features/Analytics/AnalyticsManager')
- const LinkedFilesHandler = require('./LinkedFilesHandler')
- const {
- CompileFailedError,
- UrlFetchFailedError,
- InvalidUrlError,
- AccessDeniedError,
- BadEntityTypeError,
- BadDataError,
- ProjectNotFoundError,
- V1ProjectNotFoundError,
- SourceFileNotFoundError,
- NotOriginalImporterError,
- FeatureNotAvailableError,
- RemoteServiceError,
- FileCannotRefreshError,
- } = require('./LinkedFilesErrors')
- const {
- OutputFileFetchFailedError,
- FileTooLargeError,
- OError,
- } = require('../Errors/Errors')
- const Modules = require('../../infrastructure/Modules')
- const { plainTextResponse } = require('../../infrastructure/Response')
- const ReferencesHandler = require('../References/ReferencesHandler')
- const EditorRealTimeController = require('../Editor/EditorRealTimeController')
- module.exports = LinkedFilesController = {
- Agents: _.extend(
- {
- url: require('./UrlAgent'),
- project_file: require('./ProjectFileAgent'),
- project_output_file: require('./ProjectOutputFileAgent'),
- },
- Modules.linkedFileAgentsIncludes()
- ),
- _getAgent(provider) {
- if (
- !Object.prototype.hasOwnProperty.call(
- LinkedFilesController.Agents,
- provider
- )
- ) {
- return null
- }
- if (!Array.from(Settings.enabledLinkedFileTypes).includes(provider)) {
- return null
- }
- return LinkedFilesController.Agents[provider]
- },
- createLinkedFile(req, res, next) {
- const { project_id: projectId } = req.params
- const { name, provider, data, parent_folder_id: parentFolderId } = req.body
- const userId = SessionManager.getLoggedInUserId(req.session)
- const Agent = LinkedFilesController._getAgent(provider)
- if (Agent == null) {
- return res.sendStatus(400)
- }
- data.provider = provider
- data.importedAt = new Date().toISOString()
- return Agent.createLinkedFile(
- projectId,
- data,
- name,
- parentFolderId,
- userId,
- function (err, newFileId) {
- if (err != null) {
- return LinkedFilesController.handleError(err, req, res, next)
- }
- if (name.endsWith('.bib')) {
- AnalyticsManager.recordEventForUserInBackground(
- userId,
- 'linked-bib-file',
- {
- integration: provider,
- }
- )
- }
- return res.json({ new_file_id: newFileId })
- }
- )
- },
- refreshLinkedFile(req, res, next) {
- const { project_id: projectId, file_id: fileId } = req.params
- const userId = SessionManager.getLoggedInUserId(req.session)
- return LinkedFilesHandler.getFileById(
- projectId,
- fileId,
- function (err, file, path, parentFolder) {
- if (err != null) {
- return next(err)
- }
- if (file == null) {
- return res.sendStatus(404)
- }
- const { name } = file
- const { linkedFileData } = file
- if (
- linkedFileData == null ||
- (linkedFileData != null ? linkedFileData.provider : undefined) == null
- ) {
- return res.sendStatus(409)
- }
- const { provider } = linkedFileData
- const parentFolderId = parentFolder._id
- const Agent = LinkedFilesController._getAgent(provider)
- if (Agent == null) {
- return res.sendStatus(400)
- }
- linkedFileData.importedAt = new Date().toISOString()
- Agent.refreshLinkedFile(
- projectId,
- linkedFileData,
- name,
- parentFolderId,
- userId,
- function (err, newFileId) {
- if (err != null) {
- return LinkedFilesController.handleError(err, req, res, next)
- }
- if (req.body.shouldReindexReferences) {
- ReferencesHandler.indexAll(projectId, function (error, data) {
- if (error) {
- OError.tag(error, 'failed to index references', {
- projectId,
- })
- return next(error)
- }
- EditorRealTimeController.emitToRoom(
- projectId,
- 'references:keys:updated',
- data.keys,
- true
- )
- res.json({ new_file_id: newFileId })
- })
- } else {
- res.json({ new_file_id: newFileId })
- }
- }
- )
- }
- )
- },
- handleError(error, req, res, next) {
- if (error instanceof AccessDeniedError) {
- res.status(403)
- plainTextResponse(
- res,
- res.locals.translate(
- 'the_project_that_contains_this_file_is_not_shared_with_you'
- )
- )
- } else if (error instanceof BadDataError) {
- res.status(400)
- plainTextResponse(res, 'The submitted data is not valid')
- } else if (error instanceof BadEntityTypeError) {
- res.status(400)
- plainTextResponse(res, 'The file is the wrong type')
- } else if (error instanceof SourceFileNotFoundError) {
- res.status(404)
- plainTextResponse(res, 'Source file not found')
- } else if (error instanceof ProjectNotFoundError) {
- res.status(404)
- plainTextResponse(res, 'Project not found')
- } else if (error instanceof V1ProjectNotFoundError) {
- res.status(409)
- plainTextResponse(
- res,
- 'Sorry, the source project is not yet imported to Overleaf v2. Please import it to Overleaf v2 to refresh this file'
- )
- } else if (error instanceof CompileFailedError) {
- res.status(422)
- plainTextResponse(
- res,
- res.locals.translate('generic_linked_file_compile_error')
- )
- } else if (error instanceof OutputFileFetchFailedError) {
- res.status(404)
- plainTextResponse(res, 'Could not get output file')
- } else if (error instanceof UrlFetchFailedError) {
- res.status(422)
- if (error.cause instanceof FileTooLargeError) {
- plainTextResponse(res, 'File too large')
- } else {
- plainTextResponse(
- res,
- `Your URL could not be reached (${
- error.info?.status || error.cause?.info?.status
- } status code). Please check it and try again.`
- )
- }
- } else if (error instanceof InvalidUrlError) {
- res.status(422)
- plainTextResponse(
- res,
- 'Your URL is not valid. Please check it and try again.'
- )
- } else if (error instanceof NotOriginalImporterError) {
- res.status(400)
- plainTextResponse(
- res,
- 'You are not the user who originally imported this file'
- )
- } else if (error instanceof FeatureNotAvailableError) {
- res.status(400)
- plainTextResponse(res, 'This feature is not enabled on your account')
- } else if (error instanceof RemoteServiceError) {
- if (error.info?.statusCode === 403) {
- res.status(400).json({ relink: true })
- } else {
- res.status(502)
- plainTextResponse(res, 'The remote service produced an error')
- }
- } else if (error instanceof FileCannotRefreshError) {
- res.status(400)
- plainTextResponse(res, 'This file cannot be refreshed')
- } else if (error.message === 'project_has_too_many_files') {
- res.status(400)
- plainTextResponse(res, 'too many files')
- } else if (/\bECONNREFUSED\b/.test(error.message)) {
- res.status(500)
- plainTextResponse(res, 'Importing references is not currently available')
- } else if (error instanceof FileTooLargeError) {
- res.status(422)
- plainTextResponse(res, 'File too large')
- } else {
- next(error)
- }
- },
- }
|