HttpController.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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.sendStatus(404)
  38. } else if (doc.deleted && !include_deleted) {
  39. return res.sendStatus(404)
  40. } else {
  41. return res.json(HttpController._buildDocView(doc))
  42. }
  43. })
  44. },
  45. isDocDeleted(req, res, next) {
  46. const { doc_id: docId, project_id: projectId } = req.params
  47. DocManager.isDocDeleted(projectId, docId, function (error, deleted) {
  48. if (error) {
  49. return next(error)
  50. }
  51. res.json({ deleted })
  52. })
  53. },
  54. getRawDoc(req, res, next) {
  55. if (next == null) {
  56. next = function (error) {}
  57. }
  58. const { project_id } = req.params
  59. const { doc_id } = req.params
  60. logger.log({ project_id, doc_id }, 'getting raw doc')
  61. return DocManager.getDocLines(project_id, doc_id, function (error, doc) {
  62. if (error != null) {
  63. return next(error)
  64. }
  65. if (doc == null) {
  66. return res.sendStatus(404)
  67. } else {
  68. res.setHeader('content-type', 'text/plain')
  69. return res.send(HttpController._buildRawDocView(doc))
  70. }
  71. })
  72. },
  73. getAllDocs(req, res, next) {
  74. if (next == null) {
  75. next = function (error) {}
  76. }
  77. const { project_id } = req.params
  78. logger.log({ project_id }, 'getting all docs')
  79. return DocManager.getAllNonDeletedDocs(
  80. project_id,
  81. { lines: true, rev: true },
  82. function (error, docs) {
  83. if (docs == null) {
  84. docs = []
  85. }
  86. if (error != null) {
  87. return next(error)
  88. }
  89. return res.json(HttpController._buildDocsArrayView(project_id, docs))
  90. }
  91. )
  92. },
  93. getAllRanges(req, res, next) {
  94. if (next == null) {
  95. next = function (error) {}
  96. }
  97. const { project_id } = req.params
  98. logger.log({ project_id }, 'getting all ranges')
  99. return DocManager.getAllNonDeletedDocs(
  100. project_id,
  101. { ranges: true },
  102. function (error, docs) {
  103. if (docs == null) {
  104. docs = []
  105. }
  106. if (error != null) {
  107. return next(error)
  108. }
  109. return res.json(HttpController._buildDocsArrayView(project_id, docs))
  110. }
  111. )
  112. },
  113. updateDoc(req, res, next) {
  114. if (next == null) {
  115. next = function (error) {}
  116. }
  117. const { project_id } = req.params
  118. const { doc_id } = req.params
  119. const lines = req.body != null ? req.body.lines : undefined
  120. const version = req.body != null ? req.body.version : undefined
  121. const ranges = req.body != null ? req.body.ranges : undefined
  122. if (lines == null || !(lines instanceof Array)) {
  123. logger.error({ project_id, doc_id }, 'no doc lines provided')
  124. res.sendStatus(400) // Bad Request
  125. return
  126. }
  127. if (version == null || typeof version === !'number') {
  128. logger.error({ project_id, doc_id }, 'no doc version provided')
  129. res.sendStatus(400) // Bad Request
  130. return
  131. }
  132. if (ranges == null) {
  133. logger.error({ project_id, doc_id }, 'no doc ranges provided')
  134. res.sendStatus(400) // Bad Request
  135. return
  136. }
  137. const bodyLength = lines.reduce((len, line) => line.length + len, 0)
  138. if (bodyLength > Settings.max_doc_length) {
  139. logger.error(
  140. { project_id, doc_id, bodyLength },
  141. 'document body too large'
  142. )
  143. res.status(413).send('document body too large')
  144. return
  145. }
  146. logger.log({ project_id, doc_id }, 'got http request to update doc')
  147. return DocManager.updateDoc(
  148. project_id,
  149. doc_id,
  150. lines,
  151. version,
  152. ranges,
  153. function (error, modified, rev) {
  154. if (error != null) {
  155. return next(error)
  156. }
  157. return res.json({
  158. modified,
  159. rev
  160. })
  161. }
  162. )
  163. },
  164. deleteDoc(req, res, next) {
  165. if (next == null) {
  166. next = function (error) {}
  167. }
  168. const { project_id } = req.params
  169. const { doc_id } = req.params
  170. logger.log({ project_id, doc_id }, 'deleting doc')
  171. return DocManager.deleteDoc(project_id, doc_id, function (error) {
  172. if (error != null) {
  173. return next(error)
  174. }
  175. return res.sendStatus(204)
  176. })
  177. },
  178. patchDoc(req, res, next) {
  179. const { project_id, doc_id } = req.params
  180. logger.log({ project_id, doc_id }, 'patching doc')
  181. DocManager.patchDoc(project_id, doc_id, req.body, function (error) {
  182. if (error) {
  183. return next(error)
  184. }
  185. res.sendStatus(204)
  186. })
  187. },
  188. _buildDocView(doc) {
  189. const doc_view = { _id: doc._id != null ? doc._id.toString() : undefined }
  190. for (const attribute of ['lines', 'rev', 'version', 'ranges', 'deleted']) {
  191. if (doc[attribute] != null) {
  192. doc_view[attribute] = doc[attribute]
  193. }
  194. }
  195. return doc_view
  196. },
  197. _buildRawDocView(doc) {
  198. return ((doc != null ? doc.lines : undefined) || []).join('\n')
  199. },
  200. _buildDocsArrayView(project_id, docs) {
  201. const docViews = []
  202. for (const doc of Array.from(docs)) {
  203. if (doc != null) {
  204. // There can end up being null docs for some reason :( (probably a race condition)
  205. docViews.push(HttpController._buildDocView(doc))
  206. } else {
  207. logger.error(
  208. { err: new Error('null doc'), project_id },
  209. 'encountered null doc'
  210. )
  211. }
  212. }
  213. return docViews
  214. },
  215. archiveAllDocs(req, res, next) {
  216. if (next == null) {
  217. next = function (error) {}
  218. }
  219. const { project_id } = req.params
  220. logger.log({ project_id }, 'archiving all docs')
  221. return DocArchive.archiveAllDocs(project_id, function (error) {
  222. if (error != null) {
  223. return next(error)
  224. }
  225. return res.sendStatus(204)
  226. })
  227. },
  228. archiveDoc(req, res, next) {
  229. const { project_id, doc_id } = req.params
  230. logger.log({ project_id, doc_id }, 'archiving a doc')
  231. DocArchive.archiveDocById(project_id, doc_id, function (error) {
  232. if (error) {
  233. return next(error)
  234. }
  235. res.sendStatus(204)
  236. })
  237. },
  238. unArchiveAllDocs(req, res, next) {
  239. if (next == null) {
  240. next = function (error) {}
  241. }
  242. const { project_id } = req.params
  243. logger.log({ project_id }, 'unarchiving all docs')
  244. return DocArchive.unArchiveAllDocs(project_id, function (error) {
  245. if (error != null) {
  246. return next(error)
  247. }
  248. return res.sendStatus(200)
  249. })
  250. },
  251. destroyAllDocs(req, res, next) {
  252. if (next == null) {
  253. next = function (error) {}
  254. }
  255. const { project_id } = req.params
  256. logger.log({ project_id }, 'destroying all docs')
  257. return DocArchive.destroyAllDocs(project_id, function (error) {
  258. if (error != null) {
  259. return next(error)
  260. }
  261. return res.sendStatus(204)
  262. })
  263. },
  264. healthCheck(req, res) {
  265. return HealthChecker.check(function (err) {
  266. if (err != null) {
  267. logger.err({ err }, 'error performing health check')
  268. return res.sendStatus(500)
  269. } else {
  270. return res.sendStatus(200)
  271. }
  272. })
  273. }
  274. }