HttpController.coffee 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. DocManager = require "./DocManager"
  2. logger = require "logger-sharelatex"
  3. DocArchive = require "./DocArchiveManager"
  4. HealthChecker = require "./HealthChecker"
  5. module.exports = HttpController =
  6. getDoc: (req, res, next = (error) ->) ->
  7. project_id = req.params.project_id
  8. doc_id = req.params.doc_id
  9. include_deleted = req.query?.include_deleted == "true"
  10. logger.log project_id: project_id, doc_id: doc_id, "getting doc"
  11. DocManager.getDoc project_id, doc_id, {lines: true, rev: true, deleted: true, version: true, ranges: true}, (error, doc) ->
  12. return next(error) if error?
  13. logger.log doc: doc, "got doc"
  14. if !doc?
  15. res.send 404
  16. else if doc.deleted && !include_deleted
  17. res.send 404
  18. else
  19. res.json HttpController._buildDocView(doc)
  20. getRawDoc: (req, res, next = (error)->)->
  21. project_id = req.params.project_id
  22. doc_id = req.params.doc_id
  23. logger.log project_id: project_id, doc_id: doc_id, "getting raw doc"
  24. DocManager.getDoc project_id, doc_id, {lines: true}, (error, doc) ->
  25. return next(error) if error?
  26. if !doc?
  27. res.send 404
  28. else
  29. res.setHeader('content-type', 'text/plain')
  30. res.send HttpController._buildRawDocView(doc)
  31. getAllDocs: (req, res, next = (error) ->) ->
  32. project_id = req.params.project_id
  33. logger.log project_id: project_id, "getting all docs"
  34. DocManager.getAllNonDeletedDocs project_id, {lines: true, rev: true}, (error, docs = []) ->
  35. return next(error) if error?
  36. res.json HttpController._buildDocsArrayView(project_id, docs)
  37. getAllRanges: (req, res, next = (error) ->) ->
  38. project_id = req.params.project_id
  39. logger.log {project_id}, "getting all ranges"
  40. DocManager.getAllNonDeletedDocs project_id, {ranges: true}, (error, docs = []) ->
  41. return next(error) if error?
  42. res.json HttpController._buildDocsArrayView(project_id, docs)
  43. updateDoc: (req, res, next = (error) ->) ->
  44. project_id = req.params.project_id
  45. doc_id = req.params.doc_id
  46. lines = req.body?.lines
  47. version = req.body?.version
  48. ranges = req.body?.ranges
  49. if !lines? or lines not instanceof Array
  50. logger.error project_id: project_id, doc_id: doc_id, "no doc lines provided"
  51. res.send 400 # Bad Request
  52. return
  53. if !version? or typeof version is not "number"
  54. logger.error project_id: project_id, doc_id: doc_id, "no doc version provided"
  55. res.send 400 # Bad Request
  56. return
  57. if !ranges?
  58. logger.error project_id: project_id, doc_id: doc_id, "no doc ranges provided"
  59. res.send 400 # Bad Request
  60. return
  61. logger.log project_id: project_id, doc_id: doc_id, "got http request to update doc"
  62. DocManager.updateDoc project_id, doc_id, lines, version, ranges, (error, modified, rev) ->
  63. return next(error) if error?
  64. res.json {
  65. modified: modified
  66. rev: rev
  67. }
  68. deleteDoc: (req, res, next = (error) ->) ->
  69. project_id = req.params.project_id
  70. doc_id = req.params.doc_id
  71. logger.log project_id: project_id, doc_id: doc_id, "deleting doc"
  72. DocManager.deleteDoc project_id, doc_id, (error) ->
  73. return next(error) if error?
  74. res.send 204
  75. _buildDocView: (doc) ->
  76. doc_view = { _id: doc._id?.toString() }
  77. for attribute in ["lines", "rev", "version", "ranges", "deleted"]
  78. if doc[attribute]?
  79. doc_view[attribute] = doc[attribute]
  80. return doc_view
  81. _buildRawDocView: (doc)->
  82. return (doc?.lines or []).join("\n")
  83. _buildDocsArrayView: (project_id, docs) ->
  84. docViews = []
  85. for doc in docs
  86. if doc? # There can end up being null docs for some reason :( (probably a race condition)
  87. docViews.push HttpController._buildDocView(doc)
  88. else
  89. logger.error err: new Error("null doc"), project_id: project_id, "encountered null doc"
  90. return docViews
  91. archiveAllDocs: (req, res, next = (error) ->) ->
  92. project_id = req.params.project_id
  93. logger.log project_id: project_id, "archiving all docs"
  94. DocArchive.archiveAllDocs project_id, (error) ->
  95. return next(error) if error?
  96. res.send 204
  97. unArchiveAllDocs: (req, res, next = (error) ->) ->
  98. project_id = req.params.project_id
  99. logger.log project_id: project_id, "unarchiving all docs"
  100. DocArchive.unArchiveAllDocs project_id, (error) ->
  101. return next(error) if error?
  102. res.send 200
  103. healthCheck: (req, res)->
  104. HealthChecker.check (err)->
  105. if err?
  106. logger.err err:err, "error performing health check"
  107. res.send 500
  108. else
  109. res.send 200