ProjectFileAgent.coffee 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. FileWriter = require('../../infrastructure/FileWriter')
  2. AuthorizationManager = require('../Authorization/AuthorizationManager')
  3. ProjectLocator = require('../Project/ProjectLocator')
  4. DocstoreManager = require('../Docstore/DocstoreManager')
  5. FileStoreHandler = require('../FileStore/FileStoreHandler')
  6. FileWriter = require('../../infrastructure/FileWriter')
  7. _ = require "underscore"
  8. Settings = require 'settings-sharelatex'
  9. AccessDeniedError = (message) ->
  10. error = new Error(message)
  11. error.name = 'AccessDenied'
  12. error.__proto__ = AccessDeniedError.prototype
  13. return error
  14. AccessDeniedError.prototype.__proto__ = Error.prototype
  15. BadEntityTypeError = (message) ->
  16. error = new Error(message)
  17. error.name = 'BadEntityType'
  18. error.__proto__ = BadEntityTypeError.prototype
  19. return error
  20. BadEntityTypeError.prototype.__proto__ = Error.prototype
  21. BadDataError = (message) ->
  22. error = new Error(message)
  23. error.name = 'BadData'
  24. error.__proto__ = BadDataError.prototype
  25. return error
  26. BadDataError.prototype.__proto__ = Error.prototype
  27. SourceFileNotFoundError = (message) ->
  28. error = new Error(message)
  29. error.name = 'BadData'
  30. error.__proto__ = SourceFileNotFoundError.prototype
  31. return error
  32. SourceFileNotFoundError.prototype.__proto__ = Error.prototype
  33. module.exports = ProjectFileAgent =
  34. sanitizeData: (data) ->
  35. return _.pick(
  36. data,
  37. 'source_project_id',
  38. 'source_entity_path',
  39. 'source_project_display_name'
  40. )
  41. _validate: (data) ->
  42. return (
  43. data.source_project_id? &&
  44. data.source_entity_path? &&
  45. data.source_project_display_name?
  46. )
  47. checkAuth: (project_id, data, current_user_id, callback = (error, allowed)->) ->
  48. callback = _.once(callback)
  49. if !ProjectFileAgent._validate(data)
  50. return callback(new BadDataError())
  51. {source_project_id, source_entity_path} = data
  52. AuthorizationManager.canUserReadProject current_user_id, source_project_id, null, (err, canRead) ->
  53. return callback(err) if err?
  54. callback(null, canRead)
  55. writeIncomingFileToDisk:
  56. (project_id, data, current_user_id, callback = (error, fsPath) ->) ->
  57. callback = _.once(callback)
  58. if !ProjectFileAgent._validate(data)
  59. return callback(new BadDataError())
  60. {source_project_id, source_entity_path} = data
  61. ProjectLocator.findElementByPath {
  62. project_id: source_project_id,
  63. path: source_entity_path
  64. }, (err, entity, type) ->
  65. if err?
  66. if err.toString().match(/^not found.*/)
  67. err = new SourceFileNotFoundError()
  68. return callback(err)
  69. ProjectFileAgent._writeEntityToDisk source_project_id, entity._id, type, callback
  70. _writeEntityToDisk: (project_id, entity_id, type, callback=(err, location)->) ->
  71. callback = _.once(callback)
  72. if type == 'doc'
  73. DocstoreManager.getDoc project_id, entity_id, (err, lines) ->
  74. return callback(err) if err?
  75. FileWriter.writeLinesToDisk entity_id, lines, callback
  76. else if type == 'file'
  77. FileStoreHandler.getFileStream project_id, entity_id, null, (err, fileStream) ->
  78. return callback(err) if err?
  79. FileWriter.writeStreamToDisk entity_id, fileStream, callback
  80. else
  81. callback(new BadEntityTypeError())
  82. handleError: (error, req, res, next) ->
  83. 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
  92. next(error)
  93. next()