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

34 lines
1.4 KiB
CoffeeScript
Raw Normal View History

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
Agent.writeIncomingFileToDisk project_id, linkedFileData, user_id, (error, fsPath) ->
if error?
2018-03-01 06:32:05 -05:00
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) ->
2018-02-14 10:12:46 -05:00
return next(error) if error?
res.json(new_file_id: file._id) # created
2018-05-03 09:29:03 -04:00
}