2018-05-03 09:29:03 -04:00
|
|
|
AuthorizationManager = require('../Authorization/AuthorizationManager')
|
|
|
|
ProjectLocator = require('../Project/ProjectLocator')
|
2018-05-29 05:07:31 -04:00
|
|
|
ProjectGetter = require('../Project/ProjectGetter')
|
2018-05-03 09:29:03 -04:00
|
|
|
DocstoreManager = require('../Docstore/DocstoreManager')
|
2018-08-20 07:19:59 -04:00
|
|
|
DocumentUpdaterHandler = require('../DocumentUpdater/DocumentUpdaterHandler')
|
2018-05-03 09:29:03 -04:00
|
|
|
FileStoreHandler = require('../FileStore/FileStoreHandler')
|
|
|
|
_ = require "underscore"
|
|
|
|
Settings = require 'settings-sharelatex'
|
2018-06-20 05:01:03 -04:00
|
|
|
LinkedFilesHandler = require './LinkedFilesHandler'
|
|
|
|
{
|
|
|
|
BadDataError,
|
|
|
|
AccessDeniedError,
|
|
|
|
BadEntityTypeError,
|
|
|
|
SourceFileNotFoundError,
|
|
|
|
ProjectNotFoundError,
|
|
|
|
V1ProjectNotFoundError
|
|
|
|
} = require './LinkedFilesErrors'
|
2018-05-03 09:29:03 -04:00
|
|
|
|
2018-06-20 05:01:03 -04:00
|
|
|
module.exports = ProjectFileAgent = {
|
2018-05-03 09:29:03 -04:00
|
|
|
|
2018-06-20 05:01:03 -04:00
|
|
|
createLinkedFile: (project_id, linkedFileData, name, parent_folder_id, user_id, callback) ->
|
|
|
|
if !@_canCreate(linkedFileData)
|
|
|
|
return callback(new AccessDeniedError())
|
|
|
|
@_go(project_id, linkedFileData, name, parent_folder_id, user_id, callback)
|
2018-05-21 05:12:41 -04:00
|
|
|
|
2018-06-20 05:01:03 -04:00
|
|
|
refreshLinkedFile: (project_id, linkedFileData, name, parent_folder_id, user_id, callback) ->
|
|
|
|
@_go project_id, linkedFileData, name, parent_folder_id, user_id, callback
|
2018-05-03 09:29:03 -04:00
|
|
|
|
2018-06-20 05:01:03 -04:00
|
|
|
_prepare: (project_id, linkedFileData, user_id, callback=(err, linkedFileData)->) ->
|
|
|
|
@_checkAuth project_id, linkedFileData, user_id, (err, allowed) =>
|
|
|
|
return callback(err) if err?
|
|
|
|
return callback(new AccessDeniedError()) if !allowed
|
2018-06-21 10:27:32 -04:00
|
|
|
if !@_validate(linkedFileData)
|
|
|
|
return callback(new BadDataError())
|
|
|
|
callback(null, linkedFileData)
|
2018-06-14 07:27:20 -04:00
|
|
|
|
2018-06-20 05:01:03 -04:00
|
|
|
_go: (project_id, linkedFileData, name, parent_folder_id, user_id, callback) ->
|
|
|
|
linkedFileData = @_sanitizeData(linkedFileData)
|
|
|
|
@_prepare project_id, linkedFileData, user_id, (err, linkedFileData) =>
|
|
|
|
return callback(err) if err?
|
|
|
|
if !@_validate(linkedFileData)
|
|
|
|
return callback(new BadDataError())
|
|
|
|
@_getEntity linkedFileData, user_id, (err, source_project, entity, type) =>
|
|
|
|
return callback(err) if err?
|
|
|
|
if type == 'doc'
|
|
|
|
DocstoreManager.getDoc source_project._id, entity._id, (err, lines) ->
|
|
|
|
return callback(err) if err?
|
|
|
|
LinkedFilesHandler.importContent project_id,
|
|
|
|
lines.join('\n'),
|
|
|
|
linkedFileData,
|
|
|
|
name,
|
|
|
|
parent_folder_id,
|
|
|
|
user_id,
|
|
|
|
(err, file) ->
|
|
|
|
return callback(err) if err?
|
|
|
|
callback(null, file._id) # Created
|
|
|
|
else if type == 'file'
|
|
|
|
FileStoreHandler.getFileStream source_project._id, entity._id, null, (err, fileStream) ->
|
|
|
|
return callback(err) if err?
|
|
|
|
LinkedFilesHandler.importFromStream project_id,
|
|
|
|
fileStream,
|
|
|
|
linkedFileData,
|
|
|
|
name,
|
|
|
|
parent_folder_id,
|
|
|
|
user_id,
|
|
|
|
(err, file) ->
|
|
|
|
return callback(err) if err?
|
|
|
|
callback(null, file._id) # Created
|
|
|
|
else
|
|
|
|
callback(new BadEntityTypeError())
|
|
|
|
|
|
|
|
_getEntity:
|
|
|
|
(linkedFileData, current_user_id, callback = (err, entity, type) ->) ->
|
|
|
|
callback = _.once(callback)
|
|
|
|
{ source_entity_path } = linkedFileData
|
|
|
|
@_getSourceProject linkedFileData, (err, project) ->
|
|
|
|
return callback(err) if err?
|
|
|
|
source_project_id = project._id
|
2018-08-20 07:19:59 -04:00
|
|
|
DocumentUpdaterHandler.flushProjectToMongo source_project_id, (err) ->
|
|
|
|
return callback(err) if err?
|
|
|
|
ProjectLocator.findElementByPath {
|
|
|
|
project_id: source_project_id,
|
|
|
|
path: source_entity_path
|
|
|
|
}, (err, entity, type) ->
|
|
|
|
if err?
|
2018-08-29 04:27:05 -04:00
|
|
|
if /^not found.*/.test(err.toString())
|
2018-08-20 07:19:59 -04:00
|
|
|
err = new SourceFileNotFoundError()
|
|
|
|
return callback(err)
|
|
|
|
callback(null, project, entity, type)
|
2018-06-14 07:27:20 -04:00
|
|
|
|
2018-06-20 05:01:03 -04:00
|
|
|
_sanitizeData: (data) ->
|
2018-05-24 06:30:03 -04:00
|
|
|
return _.pick(
|
|
|
|
data,
|
2018-06-20 05:01:03 -04:00
|
|
|
'provider',
|
2018-05-24 06:30:03 -04:00
|
|
|
'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-20 05:01:03 -04:00
|
|
|
_canCreate: (data) ->
|
2018-06-01 10:16:27 -04:00
|
|
|
# Don't allow creation of linked-files with v1 doc ids
|
|
|
|
!data.v1_source_doc_id?
|
|
|
|
|
2018-06-21 10:27:32 -04:00
|
|
|
_getSourceProject: LinkedFilesHandler.getSourceProject
|
2018-05-29 05:07:31 -04:00
|
|
|
|
2018-06-20 05:01:03 -04:00
|
|
|
_checkAuth: (project_id, data, current_user_id, callback = (error, allowed)->) ->
|
2018-05-24 06:29:37 -04:00
|
|
|
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-06-15 06:17:20 -04:00
|
|
|
}
|