2018-02-14 15:12:46 +00:00
|
|
|
AuthenticationController = require '../Authentication/AuthenticationController'
|
|
|
|
EditorController = require '../Editor/EditorController'
|
2018-06-01 14:16:27 +00:00
|
|
|
ProjectLocator = require '../Project/ProjectLocator'
|
2018-03-01 10:17:12 +00:00
|
|
|
Settings = require 'settings-sharelatex'
|
|
|
|
logger = require 'logger-sharelatex'
|
2018-06-01 14:16:27 +00:00
|
|
|
_ = require 'underscore'
|
2018-06-21 14:27:32 +00:00
|
|
|
LinkedFilesHandler = require './LinkedFilesHandler'
|
2018-06-22 10:14:04 +00:00
|
|
|
{
|
|
|
|
UrlFetchFailedError,
|
|
|
|
InvalidUrlError,
|
|
|
|
OutputFileFetchFailedError,
|
|
|
|
AccessDeniedError,
|
|
|
|
BadEntityTypeError,
|
|
|
|
BadDataError,
|
|
|
|
ProjectNotFoundError,
|
|
|
|
V1ProjectNotFoundError,
|
|
|
|
SourceFileNotFoundError,
|
2018-06-26 09:15:45 +00:00
|
|
|
NotOriginalImporterError,
|
|
|
|
FeatureNotAvailableError,
|
2018-07-05 11:13:07 +00:00
|
|
|
RemoteServiceError,
|
|
|
|
FileCannotRefreshError
|
2018-06-22 10:14:04 +00:00
|
|
|
} = require './LinkedFilesErrors'
|
2018-06-26 09:15:45 +00:00
|
|
|
Modules = require '../../infrastructure/Modules'
|
2018-06-20 09:01:03 +00:00
|
|
|
|
2018-02-14 15:12:46 +00:00
|
|
|
|
|
|
|
module.exports = LinkedFilesController = {
|
2018-06-20 09:01:03 +00:00
|
|
|
|
2018-06-26 09:15:45 +00:00
|
|
|
Agents: _.extend(
|
|
|
|
{
|
|
|
|
url: require('./UrlAgent'),
|
|
|
|
project_file: require('./ProjectFileAgent'),
|
|
|
|
project_output_file: require('./ProjectOutputFileAgent'),
|
|
|
|
},
|
|
|
|
Modules.linkedFileAgentsIncludes()
|
|
|
|
)
|
2018-02-14 15:12:46 +00:00
|
|
|
|
2018-06-01 14:16:27 +00:00
|
|
|
_getAgent: (provider) ->
|
|
|
|
if !LinkedFilesController.Agents.hasOwnProperty(provider)
|
|
|
|
return null
|
|
|
|
unless provider in Settings.enabledLinkedFileTypes
|
|
|
|
return null
|
|
|
|
LinkedFilesController.Agents[provider]
|
|
|
|
|
2018-02-14 15:12:46 +00: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 10:17:12 +00:00
|
|
|
logger.log {project_id, name, provider, data, parent_folder_id, user_id}, 'create linked file request'
|
2018-02-14 15:12:46 +00:00
|
|
|
|
2018-06-01 14:16:27 +00:00
|
|
|
Agent = LinkedFilesController._getAgent(provider)
|
|
|
|
if !Agent?
|
|
|
|
return res.sendStatus(400)
|
2018-02-14 15:12:46 +00:00
|
|
|
|
2018-06-20 09:01:03 +00:00
|
|
|
data.provider = provider
|
2018-06-01 14:16:27 +00:00
|
|
|
|
2018-06-20 09:01:03 +00:00
|
|
|
Agent.createLinkedFile project_id,
|
|
|
|
data,
|
|
|
|
name,
|
|
|
|
parent_folder_id,
|
|
|
|
user_id,
|
|
|
|
(err, newFileId) ->
|
2018-06-22 10:14:04 +00:00
|
|
|
return LinkedFilesController.handleError(err, req, res, next) if err?
|
2018-06-20 09:01:03 +00:00
|
|
|
res.json(new_file_id: newFileId)
|
2018-06-01 14:16:27 +00: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 10:40:36 +00:00
|
|
|
LinkedFilesHandler.getFileById project_id, file_id, (err, file, path, parentFolder) ->
|
2018-06-01 14:16:27 +00: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 09:01:03 +00:00
|
|
|
Agent.refreshLinkedFile project_id,
|
|
|
|
linkedFileData,
|
|
|
|
name,
|
|
|
|
parent_folder_id,
|
|
|
|
user_id,
|
|
|
|
(err, newFileId) ->
|
2018-06-22 10:14:04 +00:00
|
|
|
return LinkedFilesController.handleError(err, req, res, next) if err?
|
2018-06-20 09:01:03 +00:00
|
|
|
res.json(new_file_id: newFileId)
|
2018-06-01 14:16:27 +00:00
|
|
|
|
2018-06-22 10:14:04 +00:00
|
|
|
handleError: (error, req, res, next) ->
|
|
|
|
if error instanceof BadDataError
|
|
|
|
res.status(400).send("The submitted data is not valid")
|
|
|
|
|
|
|
|
else if error instanceof AccessDeniedError
|
|
|
|
res.status(403).send("You do not have access to this project")
|
|
|
|
|
|
|
|
else if error instanceof BadDataError
|
|
|
|
res.status(400).send("The submitted data is not valid")
|
|
|
|
|
|
|
|
else if error instanceof BadEntityTypeError
|
|
|
|
res.status(400).send("The file is the wrong type")
|
|
|
|
|
|
|
|
else if error instanceof SourceFileNotFoundError
|
|
|
|
res.status(404).send("Source file not found")
|
|
|
|
|
|
|
|
else if error instanceof ProjectNotFoundError
|
|
|
|
res.status(404).send("Project not found")
|
|
|
|
|
|
|
|
else if error instanceof V1ProjectNotFoundError
|
|
|
|
res.status(409).send("Sorry, the source project is not yet imported to Overleaf v2. Please import it to Overleaf v2 to refresh this file")
|
|
|
|
|
|
|
|
else if error instanceof OutputFileFetchFailedError
|
|
|
|
res.status(404).send("Could not get output file")
|
|
|
|
|
|
|
|
else if error instanceof UrlFetchFailedError
|
|
|
|
res.status(422).send(
|
|
|
|
"Your URL could not be reached (#{error.statusCode} status code). Please check it and try again."
|
|
|
|
)
|
|
|
|
|
|
|
|
else if error instanceof InvalidUrlError
|
|
|
|
res.status(422).send(
|
|
|
|
"Your URL is not valid. Please check it and try again."
|
|
|
|
)
|
|
|
|
|
2018-06-26 09:15:45 +00:00
|
|
|
else if error instanceof NotOriginalImporterError
|
|
|
|
res.status(400).send(
|
|
|
|
"You are not the user who originally imported this file"
|
|
|
|
)
|
|
|
|
|
|
|
|
else if error instanceof FeatureNotAvailableError
|
|
|
|
res.status(400).send(
|
|
|
|
"This feature is not enabled on your account"
|
|
|
|
)
|
|
|
|
|
|
|
|
else if error instanceof RemoteServiceError
|
|
|
|
res.status(502).send(
|
|
|
|
"The remote service produced an error"
|
|
|
|
)
|
|
|
|
|
2018-07-05 11:13:07 +00:00
|
|
|
else if error instanceof FileCannotRefreshError
|
|
|
|
res.status(400).send(
|
|
|
|
"This file cannot be refreshed"
|
|
|
|
)
|
2018-06-26 09:15:45 +00:00
|
|
|
|
2018-06-22 10:14:04 +00:00
|
|
|
else
|
|
|
|
next(error)
|
|
|
|
}
|