ProjectFileAgent.coffee 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 data
  36. _validate: (data) ->
  37. return (
  38. !!data.source_project_id &&
  39. !!data.source_entity_path &&
  40. !!data.source_project_display_name
  41. )
  42. checkAuth: (project_id, data, current_user_id, callback = (error, allowed)->) ->
  43. callback = _.once(callback)
  44. if !ProjectFileAgent._validate(data)
  45. return callback(new BadDataError())
  46. {source_project_id, source_entity_path} = data
  47. AuthorizationManager.canUserReadProject current_user_id, source_project_id, null, (err, canRead) ->
  48. return callback(err) if err?
  49. callback(null, canRead)
  50. writeIncomingFileToDisk:
  51. (project_id, data, current_user_id, callback = (error, fsPath) ->) ->
  52. callback = _.once(callback)
  53. if !ProjectFileAgent._validate(data)
  54. return callback(new BadDataError())
  55. {source_project_id, source_entity_path} = data
  56. ProjectLocator.findElementByPath {
  57. project_id: source_project_id,
  58. path: source_entity_path
  59. }, (err, entity, type) ->
  60. if err?
  61. if err.toString().match(/^not found.*/)
  62. err = new SourceFileNotFoundError()
  63. return callback(err)
  64. ProjectFileAgent._writeEntityToDisk source_project_id, entity._id, type, callback
  65. _writeEntityToDisk: (project_id, entity_id, type, callback=(err, location)->) ->
  66. callback = _.once(callback)
  67. if type == 'doc'
  68. DocstoreManager.getDoc project_id, entity_id, (err, lines) ->
  69. return callback(err) if err?
  70. FileWriter.writeLinesToDisk entity_id, lines, callback
  71. else if type == 'file'
  72. FileStoreHandler.getFileStream project_id, entity_id, null, (err, fileStream) ->
  73. return callback(err) if err?
  74. FileWriter.writeStreamToDisk entity_id, fileStream, callback
  75. else
  76. callback(new BadEntityTypeError())
  77. handleError: (error, req, res, next) ->
  78. if error instanceof AccessDeniedError
  79. res.status(403).send("You do not have access to this project")
  80. else if error instanceof BadDataError
  81. res.status(400).send("The submitted data is not valid")
  82. else if error instanceof BadEntityTypeError
  83. res.status(400).send("The file is the wrong type")
  84. else if error instanceof SourceFileNotFoundError
  85. res.status(404).send("Source file not found")
  86. else
  87. next(error)
  88. next()