2018-02-14 10:12:46 -05:00
|
|
|
AuthenticationController = require '../Authentication/AuthenticationController'
|
|
|
|
EditorController = require '../Editor/EditorController'
|
2018-06-01 10:16:27 -04:00
|
|
|
ProjectLocator = require '../Project/ProjectLocator'
|
2018-03-01 05:17:12 -05:00
|
|
|
Settings = require 'settings-sharelatex'
|
|
|
|
logger = require 'logger-sharelatex'
|
2018-06-01 10:16:27 -04:00
|
|
|
_ = require 'underscore'
|
2018-06-20 05:01:03 -04:00
|
|
|
LinkedFilesErrors = require './LinkedFilesErrors'
|
|
|
|
|
2018-02-14 10:12:46 -05:00
|
|
|
|
|
|
|
module.exports = LinkedFilesController = {
|
2018-06-20 05:01:03 -04:00
|
|
|
|
2018-02-14 10:12:46 -05:00
|
|
|
Agents: {
|
2018-05-03 09:29:03 -04:00
|
|
|
url: require('./UrlAgent'),
|
2018-06-08 10:33:02 -04:00
|
|
|
project_file: require('./ProjectFileAgent'),
|
|
|
|
project_output_file: require('./ProjectOutputFileAgent')
|
2018-02-14 10:12:46 -05:00
|
|
|
}
|
|
|
|
|
2018-06-01 10:16:27 -04:00
|
|
|
_getAgent: (provider) ->
|
|
|
|
if !LinkedFilesController.Agents.hasOwnProperty(provider)
|
|
|
|
return null
|
|
|
|
unless provider in Settings.enabledLinkedFileTypes
|
|
|
|
return null
|
|
|
|
LinkedFilesController.Agents[provider]
|
|
|
|
|
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
|
|
|
|
2018-06-01 10:16:27 -04:00
|
|
|
Agent = LinkedFilesController._getAgent(provider)
|
|
|
|
if !Agent?
|
|
|
|
return res.sendStatus(400)
|
2018-02-14 10:12:46 -05:00
|
|
|
|
2018-06-20 05:01:03 -04:00
|
|
|
data.provider = provider
|
2018-06-01 10:16:27 -04:00
|
|
|
|
2018-06-20 05:01:03 -04:00
|
|
|
Agent.createLinkedFile project_id,
|
|
|
|
data,
|
|
|
|
name,
|
|
|
|
parent_folder_id,
|
|
|
|
user_id,
|
|
|
|
(err, newFileId) ->
|
|
|
|
return LinkedFilesErrors.handleError(err, req, res, next) if err?
|
|
|
|
res.json(new_file_id: newFileId)
|
2018-06-01 10:16:27 -04:00
|
|
|
|
|
|
|
refreshLinkedFile: (req, res, next) ->
|
|
|
|
{project_id, file_id} = req.params
|
|
|
|
user_id = AuthenticationController.getLoggedInUserId(req)
|
|
|
|
logger.log {project_id, file_id, user_id}, 'refresh linked file request'
|
|
|
|
|
2018-06-21 06:40:36 -04:00
|
|
|
LinkedFilesHandler.getFileById project_id, file_id, (err, file, path, parentFolder) ->
|
2018-06-01 10:16:27 -04:00
|
|
|
return next(err) if err?
|
|
|
|
return res.sendStatus(404) if !file?
|
|
|
|
name = file.name
|
|
|
|
linkedFileData = file.linkedFileData
|
|
|
|
if !linkedFileData? || !linkedFileData?.provider?
|
|
|
|
return res.send(409)
|
|
|
|
provider = linkedFileData.provider
|
|
|
|
parent_folder_id = parentFolder._id
|
|
|
|
Agent = LinkedFilesController._getAgent(provider)
|
|
|
|
if !Agent?
|
|
|
|
return res.sendStatus(400)
|
|
|
|
|
2018-06-20 05:01:03 -04:00
|
|
|
Agent.refreshLinkedFile project_id,
|
|
|
|
linkedFileData,
|
|
|
|
name,
|
|
|
|
parent_folder_id,
|
|
|
|
user_id,
|
|
|
|
(err, newFileId) ->
|
|
|
|
return LinkedFilesErrors.handleError(err, req, res, next) if err?
|
|
|
|
res.json(new_file_id: newFileId)
|
2018-06-01 10:16:27 -04:00
|
|
|
|
2018-05-29 05:07:31 -04:00
|
|
|
}
|