LinkedFilesController.coffee 4.4 KB

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