HttpController.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /* eslint-disable
  2. camelcase,
  3. valid-typeof,
  4. */
  5. // TODO: This file was created by bulk-decaffeinate.
  6. // Fix any style issues and re-enable lint.
  7. /*
  8. * decaffeinate suggestions:
  9. * DS101: Remove unnecessary use of Array.from
  10. * DS102: Remove unnecessary code created because of implicit returns
  11. * DS207: Consider shorter variations of null checks
  12. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  13. */
  14. let HttpController
  15. const DocManager = require('./DocManager')
  16. const logger = require('logger-sharelatex')
  17. const DocArchive = require('./DocArchiveManager')
  18. const HealthChecker = require('./HealthChecker')
  19. const Settings = require('@overleaf/settings')
  20. module.exports = HttpController = {
  21. getDoc(req, res, next) {
  22. if (next == null) {
  23. next = function () {}
  24. }
  25. const { project_id } = req.params
  26. const { doc_id } = req.params
  27. const include_deleted =
  28. (req.query != null ? req.query.include_deleted : undefined) === 'true'
  29. logger.log({ project_id, doc_id }, 'getting doc')
  30. return DocManager.getFullDoc(project_id, doc_id, function (error, doc) {
  31. if (error != null) {
  32. return next(error)
  33. }
  34. logger.log({ doc_id, project_id }, 'got doc')
  35. if (doc == null) {
  36. return res.sendStatus(404)
  37. } else if (doc.deleted && !include_deleted) {
  38. return res.sendStatus(404)
  39. } else {
  40. return res.json(HttpController._buildDocView(doc))
  41. }
  42. })
  43. },
  44. peekDoc(req, res, next) {
  45. const { project_id } = req.params
  46. const { doc_id } = req.params
  47. logger.log({ project_id, doc_id }, 'peeking doc')
  48. DocManager.peekDoc(project_id, doc_id, function (error, doc) {
  49. if (error) {
  50. return next(error)
  51. }
  52. if (doc == null) {
  53. return res.sendStatus(404)
  54. } else {
  55. res.setHeader('x-doc-status', doc.inS3 ? 'archived' : 'active')
  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 () {}
  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 () {}
  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, deletedAt: true },
  114. function (error, docs) {
  115. if (error) {
  116. return next(error)
  117. }
  118. res.json(
  119. docs.map(doc => {
  120. return {
  121. _id: doc._id.toString(),
  122. name: doc.name,
  123. deletedAt: doc.deletedAt,
  124. }
  125. })
  126. )
  127. }
  128. )
  129. },
  130. getAllRanges(req, res, next) {
  131. if (next == null) {
  132. next = function () {}
  133. }
  134. const { project_id } = req.params
  135. logger.log({ project_id }, 'getting all ranges')
  136. return DocManager.getAllNonDeletedDocs(
  137. project_id,
  138. { ranges: true },
  139. function (error, docs) {
  140. if (docs == null) {
  141. docs = []
  142. }
  143. if (error != null) {
  144. return next(error)
  145. }
  146. return res.json(HttpController._buildDocsArrayView(project_id, docs))
  147. }
  148. )
  149. },
  150. updateDoc(req, res, next) {
  151. if (next == null) {
  152. next = function () {}
  153. }
  154. const { project_id } = req.params
  155. const { doc_id } = req.params
  156. const lines = req.body != null ? req.body.lines : undefined
  157. const version = req.body != null ? req.body.version : undefined
  158. const ranges = req.body != null ? req.body.ranges : undefined
  159. if (lines == null || !(lines instanceof Array)) {
  160. logger.error({ project_id, doc_id }, 'no doc lines provided')
  161. res.sendStatus(400) // Bad Request
  162. return
  163. }
  164. if (version == null || typeof version === !'number') {
  165. logger.error({ project_id, doc_id }, 'no doc version provided')
  166. res.sendStatus(400) // Bad Request
  167. return
  168. }
  169. if (ranges == null) {
  170. logger.error({ project_id, doc_id }, 'no doc ranges provided')
  171. res.sendStatus(400) // Bad Request
  172. return
  173. }
  174. const bodyLength = lines.reduce((len, line) => line.length + len, 0)
  175. if (bodyLength > Settings.max_doc_length) {
  176. logger.error(
  177. { project_id, doc_id, bodyLength },
  178. 'document body too large'
  179. )
  180. res.status(413).send('document body too large')
  181. return
  182. }
  183. logger.log({ project_id, doc_id }, 'got http request to update doc')
  184. return DocManager.updateDoc(
  185. project_id,
  186. doc_id,
  187. lines,
  188. version,
  189. ranges,
  190. function (error, modified, rev) {
  191. if (error != null) {
  192. return next(error)
  193. }
  194. return res.json({
  195. modified,
  196. rev,
  197. })
  198. }
  199. )
  200. },
  201. patchDoc(req, res, next) {
  202. const { project_id, doc_id } = req.params
  203. logger.log({ project_id, doc_id }, 'patching doc')
  204. const allowedFields = ['deleted', 'deletedAt', 'name']
  205. const meta = {}
  206. Object.entries(req.body).forEach(([field, value]) => {
  207. if (allowedFields.includes(field)) {
  208. meta[field] = value
  209. } else {
  210. logger.fatal({ field }, 'joi validation for pathDoc is broken')
  211. }
  212. })
  213. DocManager.patchDoc(project_id, doc_id, meta, function (error) {
  214. if (error) {
  215. return next(error)
  216. }
  217. res.sendStatus(204)
  218. })
  219. },
  220. _buildDocView(doc) {
  221. const doc_view = { _id: doc._id != null ? doc._id.toString() : undefined }
  222. for (const attribute of ['lines', 'rev', 'version', 'ranges', 'deleted']) {
  223. if (doc[attribute] != null) {
  224. doc_view[attribute] = doc[attribute]
  225. }
  226. }
  227. return doc_view
  228. },
  229. _buildRawDocView(doc) {
  230. return ((doc != null ? doc.lines : undefined) || []).join('\n')
  231. },
  232. _buildDocsArrayView(project_id, docs) {
  233. const docViews = []
  234. for (const doc of Array.from(docs)) {
  235. if (doc != null) {
  236. // There can end up being null docs for some reason :( (probably a race condition)
  237. docViews.push(HttpController._buildDocView(doc))
  238. } else {
  239. logger.error(
  240. { err: new Error('null doc'), project_id },
  241. 'encountered null doc'
  242. )
  243. }
  244. }
  245. return docViews
  246. },
  247. archiveAllDocs(req, res, next) {
  248. if (next == null) {
  249. next = function () {}
  250. }
  251. const { project_id } = req.params
  252. logger.log({ project_id }, 'archiving all docs')
  253. return DocArchive.archiveAllDocs(project_id, function (error) {
  254. if (error != null) {
  255. return next(error)
  256. }
  257. return res.sendStatus(204)
  258. })
  259. },
  260. archiveDoc(req, res, next) {
  261. const { project_id, doc_id } = req.params
  262. logger.log({ project_id, doc_id }, 'archiving a doc')
  263. DocArchive.archiveDocById(project_id, doc_id, function (error) {
  264. if (error) {
  265. return next(error)
  266. }
  267. res.sendStatus(204)
  268. })
  269. },
  270. unArchiveAllDocs(req, res, next) {
  271. if (next == null) {
  272. next = function () {}
  273. }
  274. const { project_id } = req.params
  275. logger.log({ project_id }, 'unarchiving all docs')
  276. return DocArchive.unArchiveAllDocs(project_id, function (error) {
  277. if (error != null) {
  278. return next(error)
  279. }
  280. return res.sendStatus(200)
  281. })
  282. },
  283. destroyAllDocs(req, res, next) {
  284. if (next == null) {
  285. next = function () {}
  286. }
  287. const { project_id } = req.params
  288. logger.log({ project_id }, 'destroying all docs')
  289. return DocArchive.destroyAllDocs(project_id, function (error) {
  290. if (error != null) {
  291. return next(error)
  292. }
  293. return res.sendStatus(204)
  294. })
  295. },
  296. healthCheck(req, res) {
  297. return HealthChecker.check(function (err) {
  298. if (err != null) {
  299. logger.err({ err }, 'error performing health check')
  300. return res.sendStatus(500)
  301. } else {
  302. return res.sendStatus(200)
  303. }
  304. })
  305. },
  306. }