ProjectFileAgent.coffee 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. # return callback(err) if err? # also applies when file not found
  57. if err?
  58. if err.toString().match(/^not found.*/)
  59. err = new FileNotFoundError()
  60. return callback(err)
  61. ProjectFileAgent._writeEntityToDisk source_project_id, entity._id, type, callback
  62. _writeEntityToDisk: (project_id, entity_id, type, callback=(err, location)->) ->
  63. callback = _.once(callback)
  64. if type == 'doc'
  65. DocstoreManager.getDoc project_id, entity_id, (err, lines) ->
  66. return callback(err) if err?
  67. FileWriter.writeLinesToDisk entity_id, lines, callback
  68. else if type == 'file'
  69. FileStoreHandler.getFileStream project_id, entity_id, null, (err, fileStream) ->
  70. return callback(err) if err?
  71. FileWriter.writeStreamToDisk entity_id, fileStream, callback
  72. else
  73. callback(new BadEntityTypeError())
  74. handleError: (error, req, res, next) ->
  75. 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 FileNotFoundError
  82. res.status(404).send("File not found")
  83. else
  84. next(error)
  85. next()