LinkedFilesController.coffee 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. } = require './LinkedFilesErrors'
  19. module.exports = LinkedFilesController = {
  20. Agents: {
  21. url: require('./UrlAgent'),
  22. project_file: require('./ProjectFileAgent'),
  23. project_output_file: require('./ProjectOutputFileAgent')
  24. }
  25. _getAgent: (provider) ->
  26. if !LinkedFilesController.Agents.hasOwnProperty(provider)
  27. return null
  28. unless provider in Settings.enabledLinkedFileTypes
  29. return null
  30. LinkedFilesController.Agents[provider]
  31. createLinkedFile: (req, res, next) ->
  32. {project_id} = req.params
  33. {name, provider, data, parent_folder_id} = req.body
  34. user_id = AuthenticationController.getLoggedInUserId(req)
  35. logger.log {project_id, name, provider, data, parent_folder_id, user_id}, 'create linked file request'
  36. Agent = LinkedFilesController._getAgent(provider)
  37. if !Agent?
  38. return res.sendStatus(400)
  39. data.provider = provider
  40. Agent.createLinkedFile project_id,
  41. data,
  42. name,
  43. parent_folder_id,
  44. user_id,
  45. (err, newFileId) ->
  46. return LinkedFilesController.handleError(err, req, res, next) if err?
  47. res.json(new_file_id: newFileId)
  48. refreshLinkedFile: (req, res, next) ->
  49. {project_id, file_id} = req.params
  50. user_id = AuthenticationController.getLoggedInUserId(req)
  51. logger.log {project_id, file_id, user_id}, 'refresh linked file request'
  52. LinkedFilesHandler.getFileById project_id, file_id, (err, file, path, parentFolder) ->
  53. return next(err) if err?
  54. return res.sendStatus(404) if !file?
  55. name = file.name
  56. linkedFileData = file.linkedFileData
  57. if !linkedFileData? || !linkedFileData?.provider?
  58. return res.send(409)
  59. provider = linkedFileData.provider
  60. parent_folder_id = parentFolder._id
  61. Agent = LinkedFilesController._getAgent(provider)
  62. if !Agent?
  63. return res.sendStatus(400)
  64. Agent.refreshLinkedFile project_id,
  65. linkedFileData,
  66. name,
  67. parent_folder_id,
  68. user_id,
  69. (err, newFileId) ->
  70. return LinkedFilesController.handleError(err, req, res, next) if err?
  71. res.json(new_file_id: newFileId)
  72. handleError: (error, req, res, next) ->
  73. if error instanceof BadDataError
  74. res.status(400).send("The submitted data is not valid")
  75. else if error instanceof AccessDeniedError
  76. res.status(403).send("You do not have access to this project")
  77. else if error instanceof BadDataError
  78. res.status(400).send("The submitted data is not valid")
  79. else if error instanceof BadEntityTypeError
  80. res.status(400).send("The file is the wrong type")
  81. else if error instanceof SourceFileNotFoundError
  82. res.status(404).send("Source file not found")
  83. else if error instanceof ProjectNotFoundError
  84. res.status(404).send("Project not found")
  85. else if error instanceof V1ProjectNotFoundError
  86. 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")
  87. else if error instanceof OutputFileFetchFailedError
  88. res.status(404).send("Could not get output file")
  89. else if error instanceof UrlFetchFailedError
  90. res.status(422).send(
  91. "Your URL could not be reached (#{error.statusCode} status code). Please check it and try again."
  92. )
  93. else if error instanceof InvalidUrlError
  94. res.status(422).send(
  95. "Your URL is not valid. Please check it and try again."
  96. )
  97. else
  98. next(error)
  99. }