overleaf/services/web/app/coffee/Features/LinkedFiles/ProjectOutputFileAgent.coffee

87 lines
3.2 KiB
CoffeeScript
Raw Normal View History

2018-06-08 11:06:47 -04:00
FileWriter = require('../../infrastructure/FileWriter')
AuthorizationManager = require('../Authorization/AuthorizationManager')
ProjectGetter = require('../Project/ProjectGetter')
FileWriter = require('../../infrastructure/FileWriter')
Settings = require 'settings-sharelatex'
CompileManager = require '../Compile/CompileManager'
ClsiCookieManager = require '../Compile/ClsiCookieManager'
2018-06-15 11:20:55 -04:00
ClsiManager = require '../Compile/ClsiManager'
ProjectFileAgent = require './ProjectFileAgent'
2018-06-08 11:06:47 -04:00
_ = require "underscore"
request = require "request"
OutputFileFetchFailedError = (message) ->
error = new Error(message)
error.name = 'OutputFileFetchFailedError'
error.__proto__ = OutputFileFetchFailedError.prototype
return error
OutputFileFetchFailedError.prototype.__proto__ = Error.prototype
module.exports = ProjectOutputFileAgent = {
sanitizeData: (data) ->
return {
source_project_id: data.source_project_id,
source_output_file_path: data.source_output_file_path
}
canCreate: ProjectFileAgent.canCreate
2018-06-08 11:06:47 -04:00
_getSourceProject: ProjectFileAgent._getSourceProject
decorateLinkedFileData: ProjectFileAgent.decorateLinkedFileData
_validate: (data) ->
return (
(data.source_project_id? || data.v1_source_doc_id?) &&
data.source_output_file_path?
)
2018-06-08 11:06:47 -04:00
checkAuth: (project_id, data, current_user_id, callback = (error, allowed)->) ->
callback = _.once(callback)
if !ProjectOutputFileAgent._validate(data)
return callback(new BadDataError())
@_getSourceProject data, (err, project) ->
2018-06-08 11:06:47 -04:00
return callback(err) if err?
AuthorizationManager.canUserReadProject current_user_id, project._id, null, (err, canRead) ->
return callback(err) if err?
callback(null, canRead)
2018-06-08 11:06:47 -04:00
writeIncomingFileToDisk: (project_id, data, current_user_id, callback = (error, fsPath) ->) ->
callback = _.once(callback)
if !ProjectOutputFileAgent._validate(data)
return callback(new BadDataError())
{ source_output_file_path } = data
@_getSourceProject data, (err, project) ->
2018-06-08 11:06:47 -04:00
return callback(err) if err?
source_project_id = project._id
CompileManager.compile source_project_id, null, {}, (err) ->
2018-06-08 11:06:47 -04:00
return callback(err) if err?
2018-06-15 11:20:55 -04:00
ClsiManager.getOutputFileStream source_project_id, source_output_file_path, (err, readStream) ->
return callback(err) if err?
2018-06-15 11:20:55 -04:00
readStream.pause()
readStream.on "error", callback
readStream.on "response", (response) ->
if 200 <= response.statusCode < 300
2018-06-15 11:20:55 -04:00
readStream.resume()
FileWriter.writeStreamToDisk project_id, readStream, callback
else
error = new OutputFileFetchFailedError("Output file fetch failed: #{url}")
error.statusCode = response.statusCode
callback(error)
2018-06-08 11:06:47 -04:00
handleError: (error, req, res, next) ->
2018-06-15 06:17:20 -04:00
if error instanceof ProjectFileAgent.BadDataError
2018-06-08 11:06:47 -04:00
res.status(400).send("The submitted data is not valid")
else if error instanceof OutputFileFetchFailedError
res.status(404).send("Could not get output file")
2018-06-15 06:17:20 -04:00
else if error instanceof ProjectFileAgent.ProjectNotFoundError
2018-06-08 11:06:47 -04:00
res.status(404).send("Project not found")
else if error instanceof ProjectFileAgent.V1ProjectNotFoundError
res.status(409).send(ProjectFileAgent._v1ProjectNotFoundMessage)
2018-06-08 11:06:47 -04:00
else
next(error)
}