HttpController.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /* eslint-disable
  2. camelcase,
  3. handle-callback-err,
  4. valid-typeof,
  5. */
  6. // TODO: This file was created by bulk-decaffeinate.
  7. // Fix any style issues and re-enable lint.
  8. /*
  9. * decaffeinate suggestions:
  10. * DS101: Remove unnecessary use of Array.from
  11. * DS102: Remove unnecessary code created because of implicit returns
  12. * DS207: Consider shorter variations of null checks
  13. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  14. */
  15. let HttpController
  16. const DocManager = require('./DocManager')
  17. const logger = require('logger-sharelatex')
  18. const DocArchive = require('./DocArchiveManager')
  19. const HealthChecker = require('./HealthChecker')
  20. const Settings = require('settings-sharelatex')
  21. module.exports = HttpController = {
  22. getDoc(req, res, next) {
  23. if (next == null) {
  24. next = function (error) {}
  25. }
  26. const { project_id } = req.params
  27. const { doc_id } = req.params
  28. const include_deleted =
  29. (req.query != null ? req.query.include_deleted : undefined) === 'true'
  30. logger.log({ project_id, doc_id }, 'getting doc')
  31. return DocManager.getFullDoc(project_id, doc_id, function (error, doc) {
  32. if (error != null) {
  33. return next(error)
  34. }
  35. logger.log({ doc_id, project_id }, 'got doc')
  36. if (doc == null) {
  37. return res.send(404)
  38. } else if (doc.deleted && !include_deleted) {
  39. return res.send(404)
  40. } else {
  41. return res.json(HttpController._buildDocView(doc))
  42. }
  43. })
  44. },
  45. getRawDoc(req, res, next) {
  46. if (next == null) {
  47. next = function (error) {}
  48. }
  49. const { project_id } = req.params
  50. const { doc_id } = req.params
  51. logger.log({ project_id, doc_id }, 'getting raw doc')
  52. return DocManager.getDocLines(project_id, doc_id, function (error, doc) {
  53. if (error != null) {
  54. return next(error)
  55. }
  56. if (doc == null) {
  57. return res.send(404)
  58. } else {
  59. res.setHeader('content-type', 'text/plain')
  60. return res.send(HttpController._buildRawDocView(doc))
  61. }
  62. })
  63. },
  64. getAllDocs(req, res, next) {
  65. if (next == null) {
  66. next = function (error) {}
  67. }
  68. const { project_id } = req.params
  69. logger.log({ project_id }, 'getting all docs')
  70. return DocManager.getAllNonDeletedDocs(
  71. project_id,
  72. { lines: true, rev: true },
  73. function (error, docs) {
  74. if (docs == null) {
  75. docs = []
  76. }
  77. if (error != null) {
  78. return next(error)
  79. }
  80. return res.json(HttpController._buildDocsArrayView(project_id, docs))
  81. }
  82. )
  83. },
  84. getAllRanges(req, res, next) {
  85. if (next == null) {
  86. next = function (error) {}
  87. }
  88. const { project_id } = req.params
  89. logger.log({ project_id }, 'getting all ranges')
  90. return DocManager.getAllNonDeletedDocs(
  91. project_id,
  92. { ranges: true },
  93. function (error, docs) {
  94. if (docs == null) {
  95. docs = []
  96. }
  97. if (error != null) {
  98. return next(error)
  99. }
  100. return res.json(HttpController._buildDocsArrayView(project_id, docs))
  101. }
  102. )
  103. },
  104. updateDoc(req, res, next) {
  105. if (next == null) {
  106. next = function (error) {}
  107. }
  108. const { project_id } = req.params
  109. const { doc_id } = req.params
  110. const lines = req.body != null ? req.body.lines : undefined
  111. const version = req.body != null ? req.body.version : undefined
  112. const ranges = req.body != null ? req.body.ranges : undefined
  113. if (lines == null || !(lines instanceof Array)) {
  114. logger.error({ project_id, doc_id }, 'no doc lines provided')
  115. res.send(400) // Bad Request
  116. return
  117. }
  118. if (version == null || typeof version === !'number') {
  119. logger.error({ project_id, doc_id }, 'no doc version provided')
  120. res.send(400) // Bad Request
  121. return
  122. }
  123. if (ranges == null) {
  124. logger.error({ project_id, doc_id }, 'no doc ranges provided')
  125. res.send(400) // Bad Request
  126. return
  127. }
  128. const bodyLength = lines.reduce((len, line) => line.length + len, 0)
  129. if (bodyLength > Settings.max_doc_length) {
  130. logger.error(
  131. { project_id, doc_id, bodyLength },
  132. 'document body too large'
  133. )
  134. res.status(413).send('document body too large')
  135. return
  136. }
  137. logger.log({ project_id, doc_id }, 'got http request to update doc')
  138. return DocManager.updateDoc(
  139. project_id,
  140. doc_id,
  141. lines,
  142. version,
  143. ranges,
  144. function (error, modified, rev) {
  145. if (error != null) {
  146. return next(error)
  147. }
  148. return res.json({
  149. modified,
  150. rev
  151. })
  152. }
  153. )
  154. },
  155. deleteDoc(req, res, next) {
  156. if (next == null) {
  157. next = function (error) {}
  158. }
  159. const { project_id } = req.params
  160. const { doc_id } = req.params
  161. logger.log({ project_id, doc_id }, 'deleting doc')
  162. return DocManager.deleteDoc(project_id, doc_id, function (error) {
  163. if (error != null) {
  164. return next(error)
  165. }
  166. return res.send(204)
  167. })
  168. },
  169. _buildDocView(doc) {
  170. const doc_view = { _id: doc._id != null ? doc._id.toString() : undefined }
  171. for (const attribute of ['lines', 'rev', 'version', 'ranges', 'deleted']) {
  172. if (doc[attribute] != null) {
  173. doc_view[attribute] = doc[attribute]
  174. }
  175. }
  176. return doc_view
  177. },
  178. _buildRawDocView(doc) {
  179. return ((doc != null ? doc.lines : undefined) || []).join('\n')
  180. },
  181. _buildDocsArrayView(project_id, docs) {
  182. const docViews = []
  183. for (const doc of Array.from(docs)) {
  184. if (doc != null) {
  185. // There can end up being null docs for some reason :( (probably a race condition)
  186. docViews.push(HttpController._buildDocView(doc))
  187. } else {
  188. logger.error(
  189. { err: new Error('null doc'), project_id },
  190. 'encountered null doc'
  191. )
  192. }
  193. }
  194. return docViews
  195. },
  196. archiveAllDocs(req, res, next) {
  197. if (next == null) {
  198. next = function (error) {}
  199. }
  200. const { project_id } = req.params
  201. logger.log({ project_id }, 'archiving all docs')
  202. return DocArchive.archiveAllDocs(project_id, function (error) {
  203. if (error != null) {
  204. return next(error)
  205. }
  206. return res.send(204)
  207. })
  208. },
  209. unArchiveAllDocs(req, res, next) {
  210. if (next == null) {
  211. next = function (error) {}
  212. }
  213. const { project_id } = req.params
  214. logger.log({ project_id }, 'unarchiving all docs')
  215. return DocArchive.unArchiveAllDocs(project_id, function (error) {
  216. if (error != null) {
  217. return next(error)
  218. }
  219. return res.send(200)
  220. })
  221. },
  222. destroyAllDocs(req, res, next) {
  223. if (next == null) {
  224. next = function (error) {}
  225. }
  226. const { project_id } = req.params
  227. logger.log({ project_id }, 'destroying all docs')
  228. return DocArchive.destroyAllDocs(project_id, function (error) {
  229. if (error != null) {
  230. return next(error)
  231. }
  232. return res.send(204)
  233. })
  234. },
  235. healthCheck(req, res) {
  236. return HealthChecker.check(function (err) {
  237. if (err != null) {
  238. logger.err({ err }, 'error performing health check')
  239. return res.send(500)
  240. } else {
  241. return res.send(200)
  242. }
  243. })
  244. }
  245. }