From b1ae8be927fdccc423422412ca9410cdd7f4ffc5 Mon Sep 17 00:00:00 2001 From: Christopher Hoskin Date: Wed, 20 Sep 2023 20:34:31 +0100 Subject: [PATCH] Merge pull request #14923 from overleaf/csh-2023-09-20-double-revert Csh 2023 09 20 double revert GitOrigin-RevId: 6446cbb999aa892cd2fa32fc66d231654bbdf12a --- .../LinkedFiles/LinkedFilesController.js | 14 +--- .../app/src/Features/LinkedFiles/UrlAgent.js | 84 ++++++++++--------- 2 files changed, 50 insertions(+), 48 deletions(-) diff --git a/services/web/app/src/Features/LinkedFiles/LinkedFilesController.js b/services/web/app/src/Features/LinkedFiles/LinkedFilesController.js index 3179be0109..9b4fe6dcd1 100644 --- a/services/web/app/src/Features/LinkedFiles/LinkedFilesController.js +++ b/services/web/app/src/Features/LinkedFiles/LinkedFilesController.js @@ -177,16 +177,10 @@ module.exports = LinkedFilesController = { 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.` - ) - } + plainTextResponse( + res, + `Your URL could not be reached (${error.statusCode} status code). Please check it and try again.` + ) } else if (error instanceof InvalidUrlError) { res.status(422) plainTextResponse( diff --git a/services/web/app/src/Features/LinkedFiles/UrlAgent.js b/services/web/app/src/Features/LinkedFiles/UrlAgent.js index 1ba52b40fd..325896c116 100644 --- a/services/web/app/src/Features/LinkedFiles/UrlAgent.js +++ b/services/web/app/src/Features/LinkedFiles/UrlAgent.js @@ -1,61 +1,71 @@ const logger = require('@overleaf/logger') +const request = require('request') +const _ = require('underscore') const urlValidator = require('valid-url') const { InvalidUrlError, UrlFetchFailedError } = require('./LinkedFilesErrors') const LinkedFilesHandler = require('./LinkedFilesHandler') const UrlHelper = require('../Helpers/UrlHelper') -const { fetchStream, RequestFailedError } = require('@overleaf/fetch-utils') -const { callbackify } = require('../../util/promises') -const { FileTooLargeError } = require('../Errors/Errors') -async function createLinkedFile( +function createLinkedFile( projectId, linkedFileData, name, parentFolderId, - userId + userId, + callback ) { logger.info( { projectId, userId, url: linkedFileData.url }, 'create linked file' ) linkedFileData = _sanitizeData(linkedFileData) - const fetchUrl = _getUrl(projectId, linkedFileData, userId) - try { - const readStream = await fetchStream(fetchUrl) - const file = await LinkedFilesHandler.promises.importFromStream( - projectId, - readStream, - linkedFileData, - name, - parentFolderId, - userId - ) - return file._id - } catch (error) { - if (error instanceof RequestFailedError && /too large/.test(error.body)) { - throw new FileTooLargeError('file too large', { - url: linkedFileData.url, - }).withCause(error) + _getUrlStream(projectId, linkedFileData, userId, (err, readStream) => { + if (err) { + return callback(err) } - throw new UrlFetchFailedError('url fetch failed', { - url: linkedFileData.url, - }).withCause(error) - } + readStream.on('error', callback) + readStream.on('response', response => { + if (response.statusCode >= 200 && response.statusCode < 300) { + LinkedFilesHandler.importFromStream( + projectId, + readStream, + linkedFileData, + name, + parentFolderId, + userId, + (err, file) => { + if (err) { + return callback(err) + } + callback(null, file._id) + } + ) // Created + } else { + const error = new UrlFetchFailedError( + `url fetch failed: ${linkedFileData.url}` + ) + error.statusCode = response.statusCode + callback(error) + } + }) + }) } -async function refreshLinkedFile( +function refreshLinkedFile( projectId, linkedFileData, name, parentFolderId, - userId + userId, + callback ) { - return await createLinkedFile( + createLinkedFile( projectId, linkedFileData, name, parentFolderId, - userId + userId, + callback ) } @@ -66,17 +76,15 @@ function _sanitizeData(data) { } } -function _getUrl(projectId, data, currentUserId) { +function _getUrlStream(projectId, data, currentUserId, callback) { + callback = _.once(callback) let { url } = data if (!urlValidator.isWebUri(url)) { - throw new InvalidUrlError(`invalid url: ${url}`) + return callback(new InvalidUrlError(`invalid url: ${url}`)) } url = UrlHelper.wrapUrlWithProxy(url) - return url + const readStream = request.get(url) + callback(null, readStream) } -module.exports = { - createLinkedFile: callbackify(createLinkedFile), - refreshLinkedFile: callbackify(refreshLinkedFile), - promises: { createLinkedFile, refreshLinkedFile }, -} +module.exports = { createLinkedFile, refreshLinkedFile }