2018-05-03 09:29:03 -04:00
|
|
|
FileWriter = require('../../infrastructure/FileWriter')
|
|
|
|
AuthorizationManager = require('../Authorization/AuthorizationManager')
|
|
|
|
ProjectLocator = require('../Project/ProjectLocator')
|
2018-05-29 05:07:31 -04:00
|
|
|
ProjectGetter = require('../Project/ProjectGetter')
|
2018-06-01 10:16:27 -04:00
|
|
|
Project = require("../../models/Project").Project
|
2018-05-03 09:29:03 -04:00
|
|
|
DocstoreManager = require('../Docstore/DocstoreManager')
|
|
|
|
FileStoreHandler = require('../FileStore/FileStoreHandler')
|
|
|
|
FileWriter = require('../../infrastructure/FileWriter')
|
|
|
|
_ = require "underscore"
|
|
|
|
Settings = require 'settings-sharelatex'
|
|
|
|
|
|
|
|
|
|
|
|
AccessDeniedError = (message) ->
|
|
|
|
error = new Error(message)
|
|
|
|
error.name = 'AccessDenied'
|
|
|
|
error.__proto__ = AccessDeniedError.prototype
|
|
|
|
return error
|
|
|
|
AccessDeniedError.prototype.__proto__ = Error.prototype
|
|
|
|
|
2018-05-17 05:51:58 -04:00
|
|
|
|
2018-05-03 09:29:03 -04:00
|
|
|
BadEntityTypeError = (message) ->
|
|
|
|
error = new Error(message)
|
|
|
|
error.name = 'BadEntityType'
|
|
|
|
error.__proto__ = BadEntityTypeError.prototype
|
|
|
|
return error
|
|
|
|
BadEntityTypeError.prototype.__proto__ = Error.prototype
|
|
|
|
|
|
|
|
|
2018-05-17 05:51:58 -04:00
|
|
|
BadDataError = (message) ->
|
|
|
|
error = new Error(message)
|
|
|
|
error.name = 'BadData'
|
|
|
|
error.__proto__ = BadDataError.prototype
|
|
|
|
return error
|
|
|
|
BadDataError.prototype.__proto__ = Error.prototype
|
|
|
|
|
|
|
|
|
2018-05-29 05:07:31 -04:00
|
|
|
ProjectNotFoundError = (message) ->
|
|
|
|
error = new Error(message)
|
|
|
|
error.name = 'ProjectNotFound'
|
|
|
|
error.__proto__ = ProjectNotFoundError.prototype
|
|
|
|
return error
|
|
|
|
ProjectNotFoundError.prototype.__proto__ = Error.prototype
|
|
|
|
|
|
|
|
|
2018-06-01 10:16:27 -04:00
|
|
|
V1ProjectNotFoundError = (message) ->
|
|
|
|
error = new Error(message)
|
|
|
|
error.name = 'V1ProjectNotFound'
|
|
|
|
error.__proto__ = V1ProjectNotFoundError.prototype
|
|
|
|
return error
|
|
|
|
V1ProjectNotFoundError.prototype.__proto__ = Error.prototype
|
|
|
|
|
|
|
|
|
2018-05-22 06:36:35 -04:00
|
|
|
SourceFileNotFoundError = (message) ->
|
2018-05-21 05:12:41 -04:00
|
|
|
error = new Error(message)
|
2018-06-01 10:16:27 -04:00
|
|
|
error.name = 'SourceFileNotFound'
|
2018-05-22 06:36:35 -04:00
|
|
|
error.__proto__ = SourceFileNotFoundError.prototype
|
2018-05-21 05:12:41 -04:00
|
|
|
return error
|
2018-05-22 06:36:35 -04:00
|
|
|
SourceFileNotFoundError.prototype.__proto__ = Error.prototype
|
2018-05-21 05:12:41 -04:00
|
|
|
|
|
|
|
|
2018-06-15 06:17:20 -04:00
|
|
|
module.exports = ProjectFileAgent = {
|
2018-05-03 09:29:03 -04:00
|
|
|
|
2018-06-15 06:17:20 -04:00
|
|
|
V1ProjectNotFoundError
|
|
|
|
BadDataError
|
|
|
|
ProjectNotFoundError
|
|
|
|
V1ProjectNotFoundError
|
2018-06-14 07:27:20 -04:00
|
|
|
|
|
|
|
_v1ProjectNotFoundMessage: "Sorry, the source project is not yet imported to Overleaf v2. Please import it to Overleaf v2 to refresh this file"
|
|
|
|
|
2018-05-03 09:29:03 -04:00
|
|
|
sanitizeData: (data) ->
|
2018-05-24 06:30:03 -04:00
|
|
|
return _.pick(
|
|
|
|
data,
|
|
|
|
'source_project_id',
|
2018-06-01 10:16:27 -04:00
|
|
|
'v1_source_doc_id',
|
2018-05-29 05:07:31 -04:00
|
|
|
'source_entity_path'
|
2018-05-24 06:30:03 -04:00
|
|
|
)
|
2018-05-03 09:29:03 -04:00
|
|
|
|
2018-05-17 05:51:58 -04:00
|
|
|
_validate: (data) ->
|
|
|
|
return (
|
2018-06-01 10:16:27 -04:00
|
|
|
(data.source_project_id? || data.v1_source_doc_id?) &&
|
2018-05-29 05:07:31 -04:00
|
|
|
data.source_entity_path?
|
2018-05-17 05:51:58 -04:00
|
|
|
)
|
|
|
|
|
2018-06-01 10:16:27 -04:00
|
|
|
canCreate: (data) ->
|
|
|
|
# Don't allow creation of linked-files with v1 doc ids
|
|
|
|
!data.v1_source_doc_id?
|
|
|
|
|
|
|
|
_getSourceProject: (data, callback=(err, project)->) ->
|
|
|
|
projection = {_id: 1, name: 1}
|
|
|
|
if data.v1_source_doc_id?
|
|
|
|
Project.findOne {'overleaf.id': data.v1_source_doc_id}, projection, (err, project) ->
|
|
|
|
return callback(err) if err?
|
|
|
|
if !project?
|
|
|
|
return callback(new V1ProjectNotFoundError())
|
|
|
|
callback(null, project)
|
|
|
|
else if data.source_project_id?
|
|
|
|
ProjectGetter.getProject data.source_project_id, projection, (err, project) ->
|
|
|
|
return callback(err) if err?
|
|
|
|
if !project?
|
|
|
|
return callback(new ProjectNotFoundError())
|
|
|
|
callback(null, project)
|
|
|
|
else
|
|
|
|
callback(new BadDataError('neither v1 nor v2 id present'))
|
|
|
|
|
2018-05-29 05:07:31 -04:00
|
|
|
decorateLinkedFileData: (data, callback = (err, newData) ->) ->
|
|
|
|
callback = _.once(callback)
|
2018-06-01 10:16:27 -04:00
|
|
|
@_getSourceProject data, (err, project) ->
|
2018-05-29 05:07:31 -04:00
|
|
|
return callback(err) if err?
|
|
|
|
callback(err, _.extend(data, {source_project_display_name: project.name}))
|
|
|
|
|
2018-05-24 06:29:37 -04:00
|
|
|
checkAuth: (project_id, data, current_user_id, callback = (error, allowed)->) ->
|
|
|
|
callback = _.once(callback)
|
|
|
|
if !ProjectFileAgent._validate(data)
|
|
|
|
return callback(new BadDataError())
|
2018-06-01 10:16:27 -04:00
|
|
|
@_getSourceProject data, (err, project) ->
|
2018-05-24 06:29:37 -04:00
|
|
|
return callback(err) if err?
|
2018-06-01 10:16:27 -04:00
|
|
|
AuthorizationManager.canUserReadProject current_user_id, project._id, null, (err, canRead) ->
|
|
|
|
return callback(err) if err?
|
|
|
|
callback(null, canRead)
|
2018-05-24 06:29:37 -04:00
|
|
|
|
2018-05-03 09:29:03 -04:00
|
|
|
writeIncomingFileToDisk:
|
|
|
|
(project_id, data, current_user_id, callback = (error, fsPath) ->) ->
|
|
|
|
callback = _.once(callback)
|
2018-05-17 05:51:58 -04:00
|
|
|
if !ProjectFileAgent._validate(data)
|
|
|
|
return callback(new BadDataError())
|
2018-06-01 10:16:27 -04:00
|
|
|
{ source_entity_path } = data
|
|
|
|
@_getSourceProject data, (err, project) ->
|
|
|
|
return callback(err) if err?
|
|
|
|
source_project_id = project._id
|
|
|
|
ProjectLocator.findElementByPath {
|
|
|
|
project_id: source_project_id,
|
|
|
|
path: source_entity_path
|
|
|
|
}, (err, entity, type) ->
|
|
|
|
if err?
|
|
|
|
if err.toString().match(/^not found.*/)
|
|
|
|
err = new SourceFileNotFoundError()
|
|
|
|
return callback(err)
|
|
|
|
ProjectFileAgent._writeEntityToDisk source_project_id, entity._id, type, callback
|
2018-05-03 09:29:03 -04:00
|
|
|
|
|
|
|
_writeEntityToDisk: (project_id, entity_id, type, callback=(err, location)->) ->
|
|
|
|
callback = _.once(callback)
|
|
|
|
if type == 'doc'
|
|
|
|
DocstoreManager.getDoc project_id, entity_id, (err, lines) ->
|
|
|
|
return callback(err) if err?
|
|
|
|
FileWriter.writeLinesToDisk entity_id, lines, callback
|
|
|
|
else if type == 'file'
|
2018-05-03 10:30:44 -04:00
|
|
|
FileStoreHandler.getFileStream project_id, entity_id, null, (err, fileStream) ->
|
2018-05-03 09:29:03 -04:00
|
|
|
return callback(err) if err?
|
|
|
|
FileWriter.writeStreamToDisk entity_id, fileStream, callback
|
|
|
|
else
|
|
|
|
callback(new BadEntityTypeError())
|
|
|
|
|
|
|
|
handleError: (error, req, res, next) ->
|
|
|
|
if error instanceof AccessDeniedError
|
|
|
|
res.status(403).send("You do not have access to this project")
|
2018-05-17 05:51:58 -04:00
|
|
|
else if error instanceof BadDataError
|
|
|
|
res.status(400).send("The submitted data is not valid")
|
2018-05-03 09:29:03 -04:00
|
|
|
else if error instanceof BadEntityTypeError
|
2018-05-21 05:12:41 -04:00
|
|
|
res.status(400).send("The file is the wrong type")
|
2018-05-22 06:36:35 -04:00
|
|
|
else if error instanceof SourceFileNotFoundError
|
|
|
|
res.status(404).send("Source file not found")
|
2018-05-29 05:07:31 -04:00
|
|
|
else if error instanceof ProjectNotFoundError
|
|
|
|
res.status(404).send("Project not found")
|
2018-06-01 10:16:27 -04:00
|
|
|
else if error instanceof V1ProjectNotFoundError
|
2018-06-14 07:27:20 -04:00
|
|
|
res.status(409).send(ProjectFileAgent._v1ProjectNotFoundMessage)
|
2018-05-03 09:29:03 -04:00
|
|
|
else
|
|
|
|
next(error)
|
|
|
|
next()
|
2018-06-15 06:17:20 -04:00
|
|
|
}
|