LinkedFilesController.coffee 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. AuthenticationController = require '../Authentication/AuthenticationController'
  2. EditorController = require '../Editor/EditorController'
  3. ProjectLocator = require '../Project/ProjectLocator'
  4. Settings = require 'settings-sharelatex'
  5. logger = require 'logger-sharelatex'
  6. _ = require 'underscore'
  7. LinkedFilesHandler = require './LinkedFilesHandler'
  8. {
  9. UrlFetchFailedError,
  10. InvalidUrlError,
  11. OutputFileFetchFailedError,
  12. AccessDeniedError,
  13. BadEntityTypeError,
  14. BadDataError,
  15. ProjectNotFoundError,
  16. V1ProjectNotFoundError,
  17. SourceFileNotFoundError,
  18. NotOriginalImporterError,
  19. FeatureNotAvailableError,
  20. RemoteServiceError
  21. } = require './LinkedFilesErrors'
  22. Modules = require '../../infrastructure/Modules'
  23. module.exports = LinkedFilesController = {
  24. Agents: _.extend(
  25. {
  26. url: require('./UrlAgent'),
  27. project_file: require('./ProjectFileAgent'),
  28. project_output_file: require('./ProjectOutputFileAgent'),
  29. },
  30. Modules.linkedFileAgentsIncludes()
  31. )
  32. _getAgent: (provider) ->
  33. if !LinkedFilesController.Agents.hasOwnProperty(provider)
  34. return null
  35. unless provider in Settings.enabledLinkedFileTypes
  36. return null
  37. LinkedFilesController.Agents[provider]
  38. createLinkedFile: (req, res, next) ->
  39. {project_id} = req.params
  40. {name, provider, data, parent_folder_id} = req.body
  41. user_id = AuthenticationController.getLoggedInUserId(req)
  42. logger.log {project_id, name, provider, data, parent_folder_id, user_id}, 'create linked file request'
  43. Agent = LinkedFilesController._getAgent(provider)
  44. if !Agent?
  45. return res.sendStatus(400)
  46. data.provider = provider
  47. Agent.createLinkedFile project_id,
  48. data,
  49. name,
  50. parent_folder_id,
  51. user_id,
  52. (err, newFileId) ->
  53. return LinkedFilesController.handleError(err, req, res, next) if err?
  54. res.json(new_file_id: newFileId)
  55. refreshLinkedFile: (req, res, next) ->
  56. {project_id, file_id} = req.params
  57. user_id = AuthenticationController.getLoggedInUserId(req)
  58. logger.log {project_id, file_id, user_id}, 'refresh linked file request'
  59. LinkedFilesHandler.getFileById project_id, file_id, (err, file, path, parentFolder) ->
  60. return next(err) if err?
  61. return res.sendStatus(404) if !file?
  62. name = file.name
  63. linkedFileData = file.linkedFileData
  64. if !linkedFileData? || !linkedFileData?.provider?
  65. return res.send(409)
  66. provider = linkedFileData.provider
  67. parent_folder_id = parentFolder._id
  68. Agent = LinkedFilesController._getAgent(provider)
  69. if !Agent?
  70. return res.sendStatus(400)
  71. Agent.refreshLinkedFile project_id,
  72. linkedFileData,
  73. name,
  74. parent_folder_id,
  75. user_id,
  76. (err, newFileId) ->
  77. return LinkedFilesController.handleError(err, req, res, next) if err?
  78. res.json(new_file_id: newFileId)
  79. handleError: (error, req, res, next) ->
  80. if error instanceof BadDataError
  81. res.status(400).send("The submitted data is not valid")
  82. else if error instanceof AccessDeniedError
  83. res.status(403).send("You do not have access to this project")
  84. else if error instanceof BadDataError
  85. res.status(400).send("The submitted data is not valid")
  86. else if error instanceof BadEntityTypeError
  87. res.status(400).send("The file is the wrong type")
  88. else if error instanceof SourceFileNotFoundError
  89. res.status(404).send("Source file not found")
  90. else if error instanceof ProjectNotFoundError
  91. res.status(404).send("Project not found")
  92. else if error instanceof V1ProjectNotFoundError
  93. 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")
  94. else if error instanceof OutputFileFetchFailedError
  95. res.status(404).send("Could not get output file")
  96. else if error instanceof UrlFetchFailedError
  97. res.status(422).send(
  98. "Your URL could not be reached (#{error.statusCode} status code). Please check it and try again."
  99. )
  100. else if error instanceof InvalidUrlError
  101. res.status(422).send(
  102. "Your URL is not valid. Please check it and try again."
  103. )
  104. else if error instanceof NotOriginalImporterError
  105. res.status(400).send(
  106. "You are not the user who originally imported this file"
  107. )
  108. else if error instanceof FeatureNotAvailableError
  109. res.status(400).send(
  110. "This feature is not enabled on your account"
  111. )
  112. else if error instanceof RemoteServiceError
  113. res.status(502).send(
  114. "The remote service produced an error"
  115. )
  116. else
  117. next(error)
  118. }