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

32 lines
1.3 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: {
url: require('./UrlAgent')
}
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)
2018-02-14 10:12:46 -05:00
EditorController.upsertFile project_id, parent_folder_id, name, fsPath, linkedFileData, "upload", user_id, (error) ->
return next(error) if error?
res.send(204) # created
}