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 AuthorizationManager = require('../Authorization/AuthorizationManager')
const ProjectGetter = require('../Project/ProjectGetter')
const Settings = require('@overleaf/settings')
const CompileManager = require('../Compile/CompileManager') const CompileManager = require('../Compile/CompileManager')
const ClsiManager = require('../Compile/ClsiManager') const ClsiManager = require('../Compile/ClsiManager')
const ProjectFileAgent = require('./ProjectFileAgent') const ProjectFileAgent = require('./ProjectFileAgent')
@ -24,276 +7,235 @@ const {
CompileFailedError, CompileFailedError,
BadDataError, BadDataError,
AccessDeniedError, AccessDeniedError,
BadEntityTypeError,
OutputFileFetchFailedError, OutputFileFetchFailedError,
} = require('./LinkedFilesErrors') } = require('./LinkedFilesErrors')
const LinkedFilesHandler = require('./LinkedFilesHandler') const LinkedFilesHandler = require('./LinkedFilesHandler')
const logger = require('logger-sharelatex')
module.exports = ProjectOutputFileAgent = { function _prepare(projectId, linkedFileData, userId, callback) {
_prepare(project_id, linkedFileData, user_id, callback) { _checkAuth(projectId, linkedFileData, userId, (err, allowed) => {
if (callback == null) { if (err) {
callback = function (err, linkedFileData) {} return callback(err)
} }
return this._checkAuth( if (!allowed) {
project_id,
linkedFileData,
user_id,
(err, allowed) => {
if (err != null) {
return callback(err)
}
if (!allowed) {
return callback(new AccessDeniedError())
}
if (!this._validate(linkedFileData)) {
return callback(new BadDataError())
}
return callback(null, linkedFileData)
}
)
},
createLinkedFile(
project_id,
linkedFileData,
name,
parent_folder_id,
user_id,
callback
) {
if (!this._canCreate(linkedFileData)) {
return callback(new AccessDeniedError()) return callback(new AccessDeniedError())
} }
linkedFileData = this._sanitizeData(linkedFileData) if (!_validate(linkedFileData)) {
return this._prepare(
project_id,
linkedFileData,
user_id,
(err, linkedFileData) => {
if (err != null) {
return callback(err)
}
return this._getFileStream(
linkedFileData,
user_id,
(err, readStream) => {
if (err != null) {
return callback(err)
}
readStream.on('error', callback)
return readStream.on('response', response => {
if (response.statusCode >= 200 && response.statusCode < 300) {
readStream.resume()
return LinkedFilesHandler.importFromStream(
project_id,
readStream,
linkedFileData,
name,
parent_folder_id,
user_id,
function (err, file) {
if (err != null) {
return callback(err)
}
return callback(null, file._id)
}
) // Created
} else {
err = new OutputFileFetchFailedError(
`Output file fetch failed: ${linkedFileData.build_id}, ${linkedFileData.source_output_file_path}`
)
err.statusCode = response.statusCode
return callback(err)
}
})
}
)
}
)
},
refreshLinkedFile(
project_id,
linkedFileData,
name,
parent_folder_id,
user_id,
callback
) {
return this._prepare(
project_id,
linkedFileData,
user_id,
(err, linkedFileData) => {
if (err != null) {
return callback(err)
}
return this._compileAndGetFileStream(
linkedFileData,
user_id,
(err, readStream, new_build_id) => {
if (err != null) {
return callback(err)
}
readStream.on('error', callback)
return readStream.on('response', response => {
if (response.statusCode >= 200 && response.statusCode < 300) {
readStream.resume()
linkedFileData.build_id = new_build_id
return LinkedFilesHandler.importFromStream(
project_id,
readStream,
linkedFileData,
name,
parent_folder_id,
user_id,
function (err, file) {
if (err != null) {
return callback(err)
}
return callback(null, file._id)
}
) // Created
} else {
err = new OutputFileFetchFailedError(
`Output file fetch failed: ${linkedFileData.build_id}, ${linkedFileData.source_output_file_path}`
)
err.statusCode = response.statusCode
return callback(err)
}
})
}
)
}
)
},
_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) {
return (
data.v1_source_doc_id != null && data.source_output_file_path != null
)
} else {
return (
data.source_project_id != null &&
data.source_output_file_path != null &&
data.build_id != null
)
}
},
_checkAuth(project_id, data, current_user_id, callback) {
if (callback == null) {
callback = function (err, allowed) {}
}
callback = _.once(callback)
if (!this._validate(data)) {
return callback(new BadDataError()) return callback(new BadDataError())
} }
return this._getSourceProject(data, function (err, project) { callback(null, linkedFileData)
if (err != null) { })
return callback(err) }
}
return AuthorizationManager.canUserReadProject(
current_user_id,
project._id,
null,
function (err, canRead) {
if (err != null) {
return callback(err)
}
return callback(null, canRead)
}
)
})
},
_getFileStream(linkedFileData, user_id, callback) { function createLinkedFile(
if (callback == null) { projectId,
callback = function (err, fileStream) {} linkedFileData,
name,
parentFolderId,
userId,
callback
) {
if (!ProjectFileAgent._canCreate(linkedFileData)) {
return callback(new AccessDeniedError())
}
linkedFileData = _sanitizeData(linkedFileData)
_prepare(projectId, linkedFileData, userId, (err, linkedFileData) => {
if (err) {
return callback(err)
} }
callback = _.once(callback) _getFileStream(linkedFileData, userId, (err, readStream) => {
const { source_output_file_path, build_id } = linkedFileData if (err) {
return this._getSourceProject(linkedFileData, function (err, project) {
if (err != null) {
return callback(err) return callback(err)
} }
const source_project_id = project._id readStream.on('error', callback)
return ClsiManager.getOutputFileStream( readStream.on('response', response => {
source_project_id, if (response.statusCode >= 200 && response.statusCode < 300) {
user_id, LinkedFilesHandler.importFromStream(
build_id, projectId,
source_output_file_path, readStream,
function (err, readStream) { linkedFileData,
if (err != null) { name,
return callback(err) parentFolderId,
} userId,
readStream.pause() (err, file) => {
return callback(null, readStream) if (err) {
}
)
})
},
_compileAndGetFileStream(linkedFileData, user_id, callback) {
if (callback == null) {
callback = function (err, stream, build_id) {}
}
callback = _.once(callback)
const { source_output_file_path } = linkedFileData
return this._getSourceProject(linkedFileData, function (err, project) {
if (err != null) {
return callback(err)
}
const source_project_id = project._id
return CompileManager.compile(
source_project_id,
user_id,
{},
function (err, status, outputFiles) {
if (err != null) {
return callback(err)
}
if (status !== 'success') {
return callback(new CompileFailedError())
}
const outputFile = _.find(
outputFiles,
o => o.path === source_output_file_path
)
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) {
return callback(err) return callback(err)
} }
readStream.pause() callback(null, file._id)
return callback(null, readStream, build_id)
} }
) // Created
} else {
err = new OutputFileFetchFailedError(
`Output file fetch failed: ${linkedFileData.build_id}, ${linkedFileData.source_output_file_path}`
) )
err.statusCode = response.statusCode
callback(err)
} }
) })
}) })
}, })
} }
function refreshLinkedFile(
projectId,
linkedFileData,
name,
parentFolderId,
userId,
callback
) {
_prepare(projectId, linkedFileData, userId, (err, linkedFileData) => {
if (err) {
return callback(err)
}
_compileAndGetFileStream(
linkedFileData,
userId,
(err, readStream, newBuildId) => {
if (err) {
return callback(err)
}
readStream.on('error', callback)
readStream.on('response', response => {
if (response.statusCode >= 200 && response.statusCode < 300) {
linkedFileData.build_id = newBuildId
LinkedFilesHandler.importFromStream(
projectId,
readStream,
linkedFileData,
name,
parentFolderId,
userId,
(err, file) => {
if (err) {
return callback(err)
}
callback(null, file._id)
}
) // Created
} else {
err = new OutputFileFetchFailedError(
`Output file fetch failed: ${linkedFileData.build_id}, ${linkedFileData.source_output_file_path}`
)
err.statusCode = response.statusCode
callback(err)
}
})
}
)
})
}
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,
}
}
function _validate(data) {
return (
(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)
)
}
function _checkAuth(projectId, data, currentUserId, callback) {
callback = _.once(callback)
if (!_validate(data)) {
return callback(new BadDataError())
}
LinkedFilesHandler.getSourceProject(data, (err, project) => {
if (err) {
return callback(err)
}
AuthorizationManager.canUserReadProject(
currentUserId,
project._id,
null,
(err, canRead) => {
if (err) {
return callback(err)
}
callback(null, canRead)
}
)
})
}
function _getFileStream(linkedFileData, userId, callback) {
callback = _.once(callback)
const {
source_output_file_path: sourceOutputFilePath,
build_id: buildId,
} = linkedFileData
LinkedFilesHandler.getSourceProject(linkedFileData, (err, project) => {
if (err) {
return callback(err)
}
const sourceProjectId = project._id
ClsiManager.getOutputFileStream(
sourceProjectId,
userId,
buildId,
sourceOutputFilePath,
(err, readStream) => {
if (err) {
return callback(err)
}
readStream.pause()
callback(null, readStream)
}
)
})
}
function _compileAndGetFileStream(linkedFileData, userId, callback) {
callback = _.once(callback)
const { source_output_file_path: sourceOutputFilePath } = linkedFileData
LinkedFilesHandler.getSourceProject(linkedFileData, (err, project) => {
if (err) {
return callback(err)
}
const sourceProjectId = project._id
CompileManager.compile(
sourceProjectId,
userId,
{},
(err, status, outputFiles) => {
if (err) {
return callback(err)
}
if (status !== 'success') {
return callback(new CompileFailedError())
}
const outputFile = _.find(
outputFiles,
o => o.path === sourceOutputFilePath
)
if (outputFile == null) {
return callback(new OutputFileFetchFailedError())
}
const buildId = outputFile.build
ClsiManager.getOutputFileStream(
sourceProjectId,
userId,
buildId,
sourceOutputFilePath,
(err, readStream) => {
if (err) {
return callback(err)
}
readStream.pause()
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 request = require('request')
const _ = require('underscore') const _ = require('underscore')
const urlValidator = require('valid-url') const urlValidator = require('valid-url')
@ -20,91 +5,82 @@ const { InvalidUrlError, UrlFetchFailedError } = require('./LinkedFilesErrors')
const LinkedFilesHandler = require('./LinkedFilesHandler') const LinkedFilesHandler = require('./LinkedFilesHandler')
const UrlHelper = require('../Helpers/UrlHelper') const UrlHelper = require('../Helpers/UrlHelper')
module.exports = UrlAgent = { function createLinkedFile(
createLinkedFile( projectId,
project_id, linkedFileData,
linkedFileData, name,
name, parentFolderId,
parent_folder_id, userId,
user_id, callback
callback ) {
) { linkedFileData = _sanitizeData(linkedFileData)
linkedFileData = this._sanitizeData(linkedFileData) _getUrlStream(projectId, linkedFileData, userId, (err, readStream) => {
return this._getUrlStream( if (err) {
project_id, return callback(err)
linkedFileData, }
user_id, readStream.on('error', callback)
function (err, readStream) { readStream.on('response', response => {
if (err != null) { if (response.statusCode >= 200 && response.statusCode < 300) {
return callback(err) LinkedFilesHandler.importFromStream(
} projectId,
readStream.on('error', callback) readStream,
return readStream.on('response', function (response) { linkedFileData,
if (response.statusCode >= 200 && response.statusCode < 300) { name,
readStream.resume() parentFolderId,
return LinkedFilesHandler.importFromStream( userId,
project_id, (err, file) => {
readStream, if (err) {
linkedFileData, return callback(err)
name, }
parent_folder_id, callback(null, file._id)
user_id,
function (err, file) {
if (err != null) {
return callback(err)
}
return callback(null, file._id)
}
) // Created
} else {
const error = new UrlFetchFailedError(
`url fetch failed: ${linkedFileData.url}`
)
error.statusCode = response.statusCode
return callback(error)
} }
}) ) // Created
} else {
const error = new UrlFetchFailedError(
`url fetch failed: ${linkedFileData.url}`
)
error.statusCode = response.statusCode
callback(error)
} }
) })
}, })
}
refreshLinkedFile( function refreshLinkedFile(
project_id, projectId,
linkedFileData,
name,
parentFolderId,
userId,
callback
) {
createLinkedFile(
projectId,
linkedFileData, linkedFileData,
name, name,
parent_folder_id, parentFolderId,
user_id, userId,
callback callback
) { )
return this.createLinkedFile(
project_id,
linkedFileData,
name,
parent_folder_id,
user_id,
callback
)
},
_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) {}
}
callback = _.once(callback)
let { url } = data
if (!urlValidator.isWebUri(url)) {
return callback(new InvalidUrlError(`invalid url: ${url}`))
}
url = UrlHelper.wrapUrlWithProxy(url)
const readStream = request.get(url)
readStream.pause()
return callback(null, readStream)
},
} }
function _sanitizeData(data) {
return {
provider: data.provider,
url: UrlHelper.prependHttpIfNeeded(data.url),
}
}
function _getUrlStream(projectId, data, currentUserId, callback) {
callback = _.once(callback)
let { url } = data
if (!urlValidator.isWebUri(url)) {
return callback(new InvalidUrlError(`invalid url: ${url}`))
}
url = UrlHelper.wrapUrlWithProxy(url)
const readStream = request.get(url)
readStream.pause()
callback(null, readStream)
}
module.exports = { createLinkedFile, refreshLinkedFile }