HttpController.js 8.8 KB

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