ProjectFileAgent.coffee 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. module.exports = ProjectFileAgent =
  28. sanitizeData: (data) ->
  29. return data
  30. _validate: (data) ->
  31. return (
  32. !!data.source_project_id &&
  33. !!data.source_entity_path &&
  34. !!data.source_project_display_name
  35. )
  36. writeIncomingFileToDisk:
  37. (project_id, data, current_user_id, callback = (error, fsPath) ->) ->
  38. callback = _.once(callback)
  39. if !ProjectFileAgent._validate(data)
  40. return callback(new BadDataError())
  41. {source_project_id, source_entity_path} = data
  42. AuthorizationManager.canUserReadProject current_user_id, source_project_id,
  43. null, (err, canRead) ->
  44. return callback(err) if err?
  45. return callback(new AccessDeniedError()) if !canRead
  46. ProjectLocator.findElementByPath {
  47. project_id: source_project_id,
  48. path: source_entity_path
  49. }, (err, entity, type) ->
  50. return callback(err) if err? # also applies when file not found
  51. ProjectFileAgent._writeEntityToDisk source_project_id, entity._id, type, callback
  52. _writeEntityToDisk: (project_id, entity_id, type, callback=(err, location)->) ->
  53. callback = _.once(callback)
  54. if type == 'doc'
  55. DocstoreManager.getDoc project_id, entity_id, (err, lines) ->
  56. return callback(err) if err?
  57. FileWriter.writeLinesToDisk entity_id, lines, callback
  58. else if type == 'file'
  59. FileStoreHandler.getFileStream project_id, entity_id, null, (err, fileStream) ->
  60. return callback(err) if err?
  61. FileWriter.writeStreamToDisk entity_id, fileStream, callback
  62. else
  63. callback(new BadEntityTypeError())
  64. handleError: (error, req, res, next) ->
  65. if error instanceof AccessDeniedError
  66. res.status(403).send("You do not have access to this project")
  67. else if error instanceof BadDataError
  68. res.status(400).send("The submitted data is not valid")
  69. else if error instanceof BadEntityTypeError
  70. res.status(404).send("The file is the wrong type")
  71. else
  72. next(error)
  73. next()