Handle linked-output-files from v1 imports

This commit is contained in:
Shane Kilkelly 2018-06-14 12:27:20 +01:00
parent 28257462ae
commit a313184c71
2 changed files with 43 additions and 31 deletions

View file

@ -60,6 +60,10 @@ SourceFileNotFoundError.prototype.__proto__ = Error.prototype
module.exports = ProjectFileAgent = module.exports = ProjectFileAgent =
V1ProjectNotFoundError: V1ProjectNotFoundError
_v1ProjectNotFoundMessage: "Sorry, the source project is not yet imported to Overleaf v2. Please import it to Overleaf v2 to refresh this file"
sanitizeData: (data) -> sanitizeData: (data) ->
return _.pick( return _.pick(
data, data,
@ -155,7 +159,7 @@ module.exports = ProjectFileAgent =
else if error instanceof ProjectNotFoundError else if error instanceof ProjectNotFoundError
res.status(404).send("Project not found") res.status(404).send("Project not found")
else if error instanceof V1ProjectNotFoundError else if error instanceof V1ProjectNotFoundError
res.status(409).send("Sorry, the source project is not yet imported to Overleaf v2. Please import it to Overleaf v2 to refresh this file") res.status(409).send(ProjectFileAgent._v1ProjectNotFoundMessage)
else else
next(error) next(error)
next() next()

View file

@ -6,6 +6,7 @@ Settings = require 'settings-sharelatex'
CompileManager = require '../Compile/CompileManager' CompileManager = require '../Compile/CompileManager'
CompileController = require '../Compile/CompileController' CompileController = require '../Compile/CompileController'
ClsiCookieManager = require '../Compile/ClsiCookieManager' ClsiCookieManager = require '../Compile/ClsiCookieManager'
ProjectFileAgent = require './ProjectFileAgent'
_ = require "underscore" _ = require "underscore"
request = require "request" request = require "request"
@ -42,48 +43,53 @@ module.exports = ProjectOutputFileAgent = {
source_output_file_path: data.source_output_file_path source_output_file_path: data.source_output_file_path
} }
canCreate: (data) -> true canCreate: ProjectFileAgent.canCreate
decorateLinkedFileData: (data, callback = (err, newData) ->) -> _getSourceProject: ProjectFileAgent._getSourceProject
callback = _.once(callback)
ProjectGetter.getProject data.source_project_id, {name: 1}, (err, project) -> decorateLinkedFileData: ProjectFileAgent.decorateLinkedFileData
return callback(err) if err?
if !project? _validate: (data) ->
return callback(new ProjectNotFoundError()) return (
callback(err, _.extend(data, {source_project_display_name: project.name})) (data.source_project_id? || data.v1_source_doc_id?) &&
data.source_output_file_path?
)
checkAuth: (project_id, data, current_user_id, callback = (error, allowed)->) -> checkAuth: (project_id, data, current_user_id, callback = (error, allowed)->) ->
callback = _.once(callback) callback = _.once(callback)
{ source_project_id } = data if !ProjectOutputFileAgent._validate(data)
AuthorizationManager.canUserReadProject current_user_id, source_project_id, null, (err, canRead) -> return callback(new BadDataError())
@_getSourceProject data, (err, project) ->
return callback(err) if err? return callback(err) if err?
callback(null, canRead) AuthorizationManager.canUserReadProject current_user_id, project._id, null, (err, canRead) ->
return callback(err) if err?
_validate: (data) -> callback(null, canRead)
data.source_project_id? && data.source_output_file_path?
writeIncomingFileToDisk: (project_id, data, current_user_id, callback = (error, fsPath) ->) -> writeIncomingFileToDisk: (project_id, data, current_user_id, callback = (error, fsPath) ->) ->
callback = _.once(callback) callback = _.once(callback)
if !ProjectOutputFileAgent._validate(data) if !ProjectOutputFileAgent._validate(data)
return callback(new BadDataError()) return callback(new BadDataError())
{ source_project_id, source_output_file_path } = data { source_output_file_path } = data
CompileManager.compile source_project_id, null, {}, (err) -> @_getSourceProject data, (err, project) ->
return callback(err) if err? return callback(err) if err?
url = "#{Settings.apis.clsi.url}/project/#{source_project_id}/output/#{source_output_file_path}" source_project_id = project._id
ClsiCookieManager.getCookieJar source_project_id, (err, jar)-> CompileManager.compile source_project_id, null, {}, (err) ->
return callback(err) if err? return callback(err) if err?
oneMinute = 60 * 1000 url = "#{Settings.apis.clsi.url}/project/#{source_project_id}/output/#{source_output_file_path}"
# the base request ClsiCookieManager.getCookieJar source_project_id, (err, jar)->
options = { url: url, method: "GET", timeout: oneMinute, jar : jar } return callback(err) if err?
readStream = request(options) oneMinute = 60 * 1000
readStream.on "error", callback # the base request
readStream.on "response", (response) -> options = { url: url, method: "GET", timeout: oneMinute, jar : jar }
if 200 <= response.statusCode < 300 readStream = request(options)
FileWriter.writeStreamToDisk project_id, readStream, callback readStream.on "error", callback
else readStream.on "response", (response) ->
error = new OutputFileFetchFailedError("Output file fetch failed: #{url}") if 200 <= response.statusCode < 300
error.statusCode = response.statusCode FileWriter.writeStreamToDisk project_id, readStream, callback
callback(error) else
error = new OutputFileFetchFailedError("Output file fetch failed: #{url}")
error.statusCode = response.statusCode
callback(error)
handleError: (error, req, res, next) -> handleError: (error, req, res, next) ->
if error instanceof BadDataError if error instanceof BadDataError
@ -92,6 +98,8 @@ module.exports = ProjectOutputFileAgent = {
res.status(404).send("Could not get output file") res.status(404).send("Could not get output file")
else if error instanceof ProjectNotFoundError else if error instanceof ProjectNotFoundError
res.status(404).send("Project not found") res.status(404).send("Project not found")
else if error instanceof ProjectFileAgent.V1ProjectNotFoundError
res.status(404).send(ProjectFileAgent._v1ProjectNotFoundMessage)
else else
next(error) next(error)
} }