Merge pull request #5168 from overleaf/em-request-pipe-linked-files

Fix linked files when they refer to empty files

GitOrigin-RevId: 13c751e2679849516e874afdd422cf9e8d16c8d1
This commit is contained in:
Eric Mc Sween 2021-09-22 08:32:16 -04:00 committed by Copybot
parent 59d83c4b88
commit a163efd0a1
2 changed files with 284 additions and 366 deletions

View file

@ -1,21 +1,4 @@
/* eslint-disable
camelcase,
node/handle-callback-err,
max-len,
no-unused-vars,
*/
// TODO: This file was created by bulk-decaffeinate.
// Fix any style issues and re-enable lint.
/*
* decaffeinate suggestions:
* 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 ProjectOutputFileAgent
const AuthorizationManager = require('../Authorization/AuthorizationManager')
const ProjectGetter = require('../Project/ProjectGetter')
const Settings = require('@overleaf/settings')
const CompileManager = require('../Compile/CompileManager')
const ClsiManager = require('../Compile/ClsiManager')
const ProjectFileAgent = require('./ProjectFileAgent')
@ -24,79 +7,60 @@ const {
CompileFailedError,
BadDataError,
AccessDeniedError,
BadEntityTypeError,
OutputFileFetchFailedError,
} = require('./LinkedFilesErrors')
const LinkedFilesHandler = require('./LinkedFilesHandler')
const logger = require('logger-sharelatex')
module.exports = ProjectOutputFileAgent = {
_prepare(project_id, linkedFileData, user_id, callback) {
if (callback == null) {
callback = function (err, linkedFileData) {}
}
return this._checkAuth(
project_id,
linkedFileData,
user_id,
(err, allowed) => {
if (err != null) {
function _prepare(projectId, linkedFileData, userId, callback) {
_checkAuth(projectId, linkedFileData, userId, (err, allowed) => {
if (err) {
return callback(err)
}
if (!allowed) {
return callback(new AccessDeniedError())
}
if (!this._validate(linkedFileData)) {
if (!_validate(linkedFileData)) {
return callback(new BadDataError())
}
return callback(null, linkedFileData)
}
)
},
callback(null, linkedFileData)
})
}
createLinkedFile(
project_id,
function createLinkedFile(
projectId,
linkedFileData,
name,
parent_folder_id,
user_id,
parentFolderId,
userId,
callback
) {
if (!this._canCreate(linkedFileData)) {
) {
if (!ProjectFileAgent._canCreate(linkedFileData)) {
return callback(new AccessDeniedError())
}
linkedFileData = this._sanitizeData(linkedFileData)
return this._prepare(
project_id,
linkedFileData,
user_id,
(err, linkedFileData) => {
if (err != null) {
linkedFileData = _sanitizeData(linkedFileData)
_prepare(projectId, linkedFileData, userId, (err, linkedFileData) => {
if (err) {
return callback(err)
}
return this._getFileStream(
linkedFileData,
user_id,
(err, readStream) => {
if (err != null) {
_getFileStream(linkedFileData, userId, (err, readStream) => {
if (err) {
return callback(err)
}
readStream.on('error', callback)
return readStream.on('response', response => {
readStream.on('response', response => {
if (response.statusCode >= 200 && response.statusCode < 300) {
readStream.resume()
return LinkedFilesHandler.importFromStream(
project_id,
LinkedFilesHandler.importFromStream(
projectId,
readStream,
linkedFileData,
name,
parent_folder_id,
user_id,
function (err, file) {
if (err != null) {
parentFolderId,
userId,
(err, file) => {
if (err) {
return callback(err)
}
return callback(null, file._id)
callback(null, file._id)
}
) // Created
} else {
@ -104,55 +68,48 @@ module.exports = ProjectOutputFileAgent = {
`Output file fetch failed: ${linkedFileData.build_id}, ${linkedFileData.source_output_file_path}`
)
err.statusCode = response.statusCode
return callback(err)
callback(err)
}
})
}
)
}
)
},
})
})
}
refreshLinkedFile(
project_id,
function refreshLinkedFile(
projectId,
linkedFileData,
name,
parent_folder_id,
user_id,
parentFolderId,
userId,
callback
) {
return this._prepare(
project_id,
linkedFileData,
user_id,
(err, linkedFileData) => {
if (err != null) {
) {
_prepare(projectId, linkedFileData, userId, (err, linkedFileData) => {
if (err) {
return callback(err)
}
return this._compileAndGetFileStream(
_compileAndGetFileStream(
linkedFileData,
user_id,
(err, readStream, new_build_id) => {
if (err != null) {
userId,
(err, readStream, newBuildId) => {
if (err) {
return callback(err)
}
readStream.on('error', callback)
return readStream.on('response', response => {
readStream.on('response', response => {
if (response.statusCode >= 200 && response.statusCode < 300) {
readStream.resume()
linkedFileData.build_id = new_build_id
return LinkedFilesHandler.importFromStream(
project_id,
linkedFileData.build_id = newBuildId
LinkedFilesHandler.importFromStream(
projectId,
readStream,
linkedFileData,
name,
parent_folder_id,
user_id,
function (err, file) {
if (err != null) {
parentFolderId,
userId,
(err, file) => {
if (err) {
return callback(err)
}
return callback(null, file._id)
callback(null, file._id)
}
) // Created
} else {
@ -160,112 +117,96 @@ module.exports = ProjectOutputFileAgent = {
`Output file fetch failed: ${linkedFileData.build_id}, ${linkedFileData.source_output_file_path}`
)
err.statusCode = response.statusCode
return callback(err)
callback(err)
}
})
}
)
}
)
},
})
}
_sanitizeData(data) {
function _sanitizeData(data) {
return {
provider: data.provider,
source_project_id: data.source_project_id,
source_output_file_path: data.source_output_file_path,
build_id: data.build_id,
}
},
}
_canCreate: ProjectFileAgent._canCreate,
_getSourceProject: LinkedFilesHandler.getSourceProject,
_validate(data) {
if (data.v1_source_doc_id != null) {
function _validate(data) {
return (
data.v1_source_doc_id != null && data.source_output_file_path != null
)
} else {
return (
data.source_project_id != null &&
(data.v1_source_doc_id != null && data.source_output_file_path != null) ||
(data.source_project_id != null &&
data.source_output_file_path != null &&
data.build_id != null
data.build_id != null)
)
}
},
}
_checkAuth(project_id, data, current_user_id, callback) {
if (callback == null) {
callback = function (err, allowed) {}
}
function _checkAuth(projectId, data, currentUserId, callback) {
callback = _.once(callback)
if (!this._validate(data)) {
if (!_validate(data)) {
return callback(new BadDataError())
}
return this._getSourceProject(data, function (err, project) {
if (err != null) {
LinkedFilesHandler.getSourceProject(data, (err, project) => {
if (err) {
return callback(err)
}
return AuthorizationManager.canUserReadProject(
current_user_id,
AuthorizationManager.canUserReadProject(
currentUserId,
project._id,
null,
function (err, canRead) {
if (err != null) {
(err, canRead) => {
if (err) {
return callback(err)
}
return callback(null, canRead)
callback(null, canRead)
}
)
})
},
}
_getFileStream(linkedFileData, user_id, callback) {
if (callback == null) {
callback = function (err, fileStream) {}
}
function _getFileStream(linkedFileData, userId, callback) {
callback = _.once(callback)
const { source_output_file_path, build_id } = linkedFileData
return this._getSourceProject(linkedFileData, function (err, project) {
if (err != null) {
const {
source_output_file_path: sourceOutputFilePath,
build_id: buildId,
} = linkedFileData
LinkedFilesHandler.getSourceProject(linkedFileData, (err, project) => {
if (err) {
return callback(err)
}
const source_project_id = project._id
return ClsiManager.getOutputFileStream(
source_project_id,
user_id,
build_id,
source_output_file_path,
function (err, readStream) {
if (err != null) {
const sourceProjectId = project._id
ClsiManager.getOutputFileStream(
sourceProjectId,
userId,
buildId,
sourceOutputFilePath,
(err, readStream) => {
if (err) {
return callback(err)
}
readStream.pause()
return callback(null, readStream)
callback(null, readStream)
}
)
})
},
}
_compileAndGetFileStream(linkedFileData, user_id, callback) {
if (callback == null) {
callback = function (err, stream, build_id) {}
}
function _compileAndGetFileStream(linkedFileData, userId, callback) {
callback = _.once(callback)
const { source_output_file_path } = linkedFileData
return this._getSourceProject(linkedFileData, function (err, project) {
if (err != null) {
const { source_output_file_path: sourceOutputFilePath } = linkedFileData
LinkedFilesHandler.getSourceProject(linkedFileData, (err, project) => {
if (err) {
return callback(err)
}
const source_project_id = project._id
return CompileManager.compile(
source_project_id,
user_id,
const sourceProjectId = project._id
CompileManager.compile(
sourceProjectId,
userId,
{},
function (err, status, outputFiles) {
if (err != null) {
(err, status, outputFiles) => {
if (err) {
return callback(err)
}
if (status !== 'success') {
@ -273,27 +214,28 @@ module.exports = ProjectOutputFileAgent = {
}
const outputFile = _.find(
outputFiles,
o => o.path === source_output_file_path
o => o.path === sourceOutputFilePath
)
if (outputFile == null) {
return callback(new OutputFileFetchFailedError())
}
const build_id = outputFile.build
return ClsiManager.getOutputFileStream(
source_project_id,
user_id,
build_id,
source_output_file_path,
function (err, readStream) {
if (err != null) {
const buildId = outputFile.build
ClsiManager.getOutputFileStream(
sourceProjectId,
userId,
buildId,
sourceOutputFilePath,
(err, readStream) => {
if (err) {
return callback(err)
}
readStream.pause()
return callback(null, readStream, build_id)
callback(null, readStream, buildId)
}
)
}
)
})
},
}
module.exports = { createLinkedFile, refreshLinkedFile }

View file

@ -1,18 +1,3 @@
/* eslint-disable
camelcase,
node/handle-callback-err,
max-len,
no-unused-vars,
*/
// TODO: This file was created by bulk-decaffeinate.
// Fix any style issues and re-enable lint.
/*
* decaffeinate suggestions:
* 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 UrlAgent
const request = require('request')
const _ = require('underscore')
const urlValidator = require('valid-url')
@ -20,40 +5,34 @@ const { InvalidUrlError, UrlFetchFailedError } = require('./LinkedFilesErrors')
const LinkedFilesHandler = require('./LinkedFilesHandler')
const UrlHelper = require('../Helpers/UrlHelper')
module.exports = UrlAgent = {
createLinkedFile(
project_id,
function createLinkedFile(
projectId,
linkedFileData,
name,
parent_folder_id,
user_id,
parentFolderId,
userId,
callback
) {
linkedFileData = this._sanitizeData(linkedFileData)
return this._getUrlStream(
project_id,
linkedFileData,
user_id,
function (err, readStream) {
if (err != null) {
) {
linkedFileData = _sanitizeData(linkedFileData)
_getUrlStream(projectId, linkedFileData, userId, (err, readStream) => {
if (err) {
return callback(err)
}
readStream.on('error', callback)
return readStream.on('response', function (response) {
readStream.on('response', response => {
if (response.statusCode >= 200 && response.statusCode < 300) {
readStream.resume()
return LinkedFilesHandler.importFromStream(
project_id,
LinkedFilesHandler.importFromStream(
projectId,
readStream,
linkedFileData,
name,
parent_folder_id,
user_id,
function (err, file) {
if (err != null) {
parentFolderId,
userId,
(err, file) => {
if (err) {
return callback(err)
}
return callback(null, file._id)
callback(null, file._id)
}
) // Created
} else {
@ -61,42 +40,38 @@ module.exports = UrlAgent = {
`url fetch failed: ${linkedFileData.url}`
)
error.statusCode = response.statusCode
return callback(error)
callback(error)
}
})
}
)
},
})
}
refreshLinkedFile(
project_id,
function refreshLinkedFile(
projectId,
linkedFileData,
name,
parent_folder_id,
user_id,
parentFolderId,
userId,
callback
) {
return this.createLinkedFile(
project_id,
) {
createLinkedFile(
projectId,
linkedFileData,
name,
parent_folder_id,
user_id,
parentFolderId,
userId,
callback
)
},
}
_sanitizeData(data) {
function _sanitizeData(data) {
return {
provider: data.provider,
url: UrlHelper.prependHttpIfNeeded(data.url),
}
},
}
_getUrlStream(project_id, data, current_user_id, callback) {
if (callback == null) {
callback = function (error, fsPath) {}
}
function _getUrlStream(projectId, data, currentUserId, callback) {
callback = _.once(callback)
let { url } = data
if (!urlValidator.isWebUri(url)) {
@ -105,6 +80,7 @@ module.exports = UrlAgent = {
url = UrlHelper.wrapUrlWithProxy(url)
const readStream = request.get(url)
readStream.pause()
return callback(null, readStream)
},
callback(null, readStream)
}
module.exports = { createLinkedFile, refreshLinkedFile }