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