2018-02-14 10:12:46 -05:00
|
|
|
AuthenticationController = require '../Authentication/AuthenticationController'
|
|
|
|
EditorController = require '../Editor/EditorController'
|
2018-03-01 05:17:12 -05:00
|
|
|
Settings = require 'settings-sharelatex'
|
|
|
|
logger = require 'logger-sharelatex'
|
2018-02-14 10:12:46 -05:00
|
|
|
|
|
|
|
module.exports = LinkedFilesController = {
|
|
|
|
Agents: {
|
2018-05-03 09:29:03 -04:00
|
|
|
url: require('./UrlAgent'),
|
|
|
|
project_file: require('./ProjectFileAgent')
|
2018-02-14 10:12:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
createLinkedFile: (req, res, next) ->
|
|
|
|
{project_id} = req.params
|
|
|
|
{name, provider, data, parent_folder_id} = req.body
|
|
|
|
user_id = AuthenticationController.getLoggedInUserId(req)
|
2018-03-01 05:17:12 -05:00
|
|
|
logger.log {project_id, name, provider, data, parent_folder_id, user_id}, 'create linked file request'
|
2018-02-14 10:12:46 -05:00
|
|
|
|
|
|
|
if !LinkedFilesController.Agents.hasOwnProperty(provider)
|
|
|
|
return res.send(400)
|
2018-03-01 05:17:12 -05:00
|
|
|
unless provider in Settings.enabledLinkedFileTypes
|
|
|
|
return res.send(400)
|
2018-02-14 10:12:46 -05:00
|
|
|
Agent = LinkedFilesController.Agents[provider]
|
|
|
|
|
|
|
|
linkedFileData = Agent.sanitizeData(data)
|
|
|
|
linkedFileData.provider = provider
|
2018-05-24 06:29:37 -04:00
|
|
|
Agent.checkAuth project_id, data, user_id, (err, allowed) ->
|
2018-05-25 11:01:11 -04:00
|
|
|
return Agent.handleError(err, req, res, next) if err?
|
|
|
|
return res.sendStatus(403) if !allowed
|
2018-05-24 06:29:37 -04:00
|
|
|
Agent.writeIncomingFileToDisk project_id, linkedFileData, user_id, (error, fsPath) ->
|
|
|
|
if error?
|
|
|
|
logger.error {err: error, project_id, name, linkedFileData, parent_folder_id, user_id}, 'error writing linked file to disk'
|
|
|
|
return Agent.handleError(error, req, res, next)
|
|
|
|
EditorController.upsertFile project_id, parent_folder_id, name, fsPath, linkedFileData, "upload", user_id, (error, file) ->
|
|
|
|
return next(error) if error?
|
|
|
|
res.json(new_file_id: file._id) # created
|
2018-05-03 09:29:03 -04:00
|
|
|
}
|