HttpController.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. getAllDeletedDocs(req, res, next) {
  94. const { project_id } = req.params
  95. logger.log({ project_id }, 'getting all deleted docs')
  96. DocManager.getAllDeletedDocs(project_id, { name: true }, function (
  97. error,
  98. docs
  99. ) {
  100. if (error) {
  101. return next(error)
  102. }
  103. res.json(
  104. docs.map((doc) => {
  105. return { _id: doc._id.toString(), name: doc.name }
  106. })
  107. )
  108. })
  109. },
  110. getAllRanges(req, res, next) {
  111. if (next == null) {
  112. next = function (error) {}
  113. }
  114. const { project_id } = req.params
  115. logger.log({ project_id }, 'getting all ranges')
  116. return DocManager.getAllNonDeletedDocs(
  117. project_id,
  118. { ranges: true },
  119. function (error, docs) {
  120. if (docs == null) {
  121. docs = []
  122. }
  123. if (error != null) {
  124. return next(error)
  125. }
  126. return res.json(HttpController._buildDocsArrayView(project_id, docs))
  127. }
  128. )
  129. },
  130. updateDoc(req, res, next) {
  131. if (next == null) {
  132. next = function (error) {}
  133. }
  134. const { project_id } = req.params
  135. const { doc_id } = req.params
  136. const lines = req.body != null ? req.body.lines : undefined
  137. const version = req.body != null ? req.body.version : undefined
  138. const ranges = req.body != null ? req.body.ranges : undefined
  139. if (lines == null || !(lines instanceof Array)) {
  140. logger.error({ project_id, doc_id }, 'no doc lines provided')
  141. res.sendStatus(400) // Bad Request
  142. return
  143. }
  144. if (version == null || typeof version === !'number') {
  145. logger.error({ project_id, doc_id }, 'no doc version provided')
  146. res.sendStatus(400) // Bad Request
  147. return
  148. }
  149. if (ranges == null) {
  150. logger.error({ project_id, doc_id }, 'no doc ranges provided')
  151. res.sendStatus(400) // Bad Request
  152. return
  153. }
  154. const bodyLength = lines.reduce((len, line) => line.length + len, 0)
  155. if (bodyLength > Settings.max_doc_length) {
  156. logger.error(
  157. { project_id, doc_id, bodyLength },
  158. 'document body too large'
  159. )
  160. res.status(413).send('document body too large')
  161. return
  162. }
  163. logger.log({ project_id, doc_id }, 'got http request to update doc')
  164. return DocManager.updateDoc(
  165. project_id,
  166. doc_id,
  167. lines,
  168. version,
  169. ranges,
  170. function (error, modified, rev) {
  171. if (error != null) {
  172. return next(error)
  173. }
  174. return res.json({
  175. modified,
  176. rev
  177. })
  178. }
  179. )
  180. },
  181. deleteDoc(req, res, next) {
  182. if (next == null) {
  183. next = function (error) {}
  184. }
  185. const { project_id } = req.params
  186. const { doc_id } = req.params
  187. logger.log({ project_id, doc_id }, 'deleting doc')
  188. return DocManager.deleteDoc(project_id, doc_id, function (error) {
  189. if (error != null) {
  190. return next(error)
  191. }
  192. return res.sendStatus(204)
  193. })
  194. },
  195. patchDoc(req, res, next) {
  196. const { project_id, doc_id } = req.params
  197. logger.log({ project_id, doc_id }, 'patching doc')
  198. const allowedFields = ['deleted', 'deletedAt', 'name']
  199. const meta = {}
  200. Object.entries(req.body).forEach(([field, value]) => {
  201. if (allowedFields.includes(field)) {
  202. meta[field] = value
  203. } else {
  204. logger.fatal({ field }, 'joi validation for pathDoc is broken')
  205. }
  206. })
  207. DocManager.patchDoc(project_id, doc_id, meta, function (error) {
  208. if (error) {
  209. return next(error)
  210. }
  211. res.sendStatus(204)
  212. })
  213. },
  214. _buildDocView(doc) {
  215. const doc_view = { _id: doc._id != null ? doc._id.toString() : undefined }
  216. for (const attribute of ['lines', 'rev', 'version', 'ranges', 'deleted']) {
  217. if (doc[attribute] != null) {
  218. doc_view[attribute] = doc[attribute]
  219. }
  220. }
  221. return doc_view
  222. },
  223. _buildRawDocView(doc) {
  224. return ((doc != null ? doc.lines : undefined) || []).join('\n')
  225. },
  226. _buildDocsArrayView(project_id, docs) {
  227. const docViews = []
  228. for (const doc of Array.from(docs)) {
  229. if (doc != null) {
  230. // There can end up being null docs for some reason :( (probably a race condition)
  231. docViews.push(HttpController._buildDocView(doc))
  232. } else {
  233. logger.error(
  234. { err: new Error('null doc'), project_id },
  235. 'encountered null doc'
  236. )
  237. }
  238. }
  239. return docViews
  240. },
  241. archiveAllDocs(req, res, next) {
  242. if (next == null) {
  243. next = function (error) {}
  244. }
  245. const { project_id } = req.params
  246. logger.log({ project_id }, 'archiving all docs')
  247. return DocArchive.archiveAllDocs(project_id, function (error) {
  248. if (error != null) {
  249. return next(error)
  250. }
  251. return res.sendStatus(204)
  252. })
  253. },
  254. archiveDoc(req, res, next) {
  255. const { project_id, doc_id } = req.params
  256. logger.log({ project_id, doc_id }, 'archiving a doc')
  257. DocArchive.archiveDocById(project_id, doc_id, function (error) {
  258. if (error) {
  259. return next(error)
  260. }
  261. res.sendStatus(204)
  262. })
  263. },
  264. unArchiveAllDocs(req, res, next) {
  265. if (next == null) {
  266. next = function (error) {}
  267. }
  268. const { project_id } = req.params
  269. logger.log({ project_id }, 'unarchiving all docs')
  270. return DocArchive.unArchiveAllDocs(project_id, function (error) {
  271. if (error != null) {
  272. return next(error)
  273. }
  274. return res.sendStatus(200)
  275. })
  276. },
  277. destroyAllDocs(req, res, next) {
  278. if (next == null) {
  279. next = function (error) {}
  280. }
  281. const { project_id } = req.params
  282. logger.log({ project_id }, 'destroying all docs')
  283. return DocArchive.destroyAllDocs(project_id, function (error) {
  284. if (error != null) {
  285. return next(error)
  286. }
  287. return res.sendStatus(204)
  288. })
  289. },
  290. healthCheck(req, res) {
  291. return HealthChecker.check(function (err) {
  292. if (err != null) {
  293. logger.err({ err }, 'error performing health check')
  294. return res.sendStatus(500)
  295. } else {
  296. return res.sendStatus(200)
  297. }
  298. })
  299. }
  300. }