ProjectFileAgent.coffee 3.2 KB

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