HttpController.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. 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. patchDoc(req, res, next) {
  182. const { project_id, doc_id } = req.params
  183. logger.log({ project_id, doc_id }, 'patching doc')
  184. const allowedFields = ['deleted', 'deletedAt', 'name']
  185. const meta = {}
  186. Object.entries(req.body).forEach(([field, value]) => {
  187. if (allowedFields.includes(field)) {
  188. meta[field] = value
  189. } else {
  190. logger.fatal({ field }, 'joi validation for pathDoc is broken')
  191. }
  192. })
  193. DocManager.patchDoc(project_id, doc_id, meta, function (error) {
  194. if (error) {
  195. return next(error)
  196. }
  197. res.sendStatus(204)
  198. })
  199. },
  200. _buildDocView(doc) {
  201. const doc_view = { _id: doc._id != null ? doc._id.toString() : undefined }
  202. for (const attribute of ['lines', 'rev', 'version', 'ranges', 'deleted']) {
  203. if (doc[attribute] != null) {
  204. doc_view[attribute] = doc[attribute]
  205. }
  206. }
  207. return doc_view
  208. },
  209. _buildRawDocView(doc) {
  210. return ((doc != null ? doc.lines : undefined) || []).join('\n')
  211. },
  212. _buildDocsArrayView(project_id, docs) {
  213. const docViews = []
  214. for (const doc of Array.from(docs)) {
  215. if (doc != null) {
  216. // There can end up being null docs for some reason :( (probably a race condition)
  217. docViews.push(HttpController._buildDocView(doc))
  218. } else {
  219. logger.error(
  220. { err: new Error('null doc'), project_id },
  221. 'encountered null doc'
  222. )
  223. }
  224. }
  225. return docViews
  226. },
  227. archiveAllDocs(req, res, next) {
  228. if (next == null) {
  229. next = function (error) {}
  230. }
  231. const { project_id } = req.params
  232. logger.log({ project_id }, 'archiving all docs')
  233. return DocArchive.archiveAllDocs(project_id, function (error) {
  234. if (error != null) {
  235. return next(error)
  236. }
  237. return res.sendStatus(204)
  238. })
  239. },
  240. archiveDoc(req, res, next) {
  241. const { project_id, doc_id } = req.params
  242. logger.log({ project_id, doc_id }, 'archiving a doc')
  243. DocArchive.archiveDocById(project_id, doc_id, function (error) {
  244. if (error) {
  245. return next(error)
  246. }
  247. res.sendStatus(204)
  248. })
  249. },
  250. unArchiveAllDocs(req, res, next) {
  251. if (next == null) {
  252. next = function (error) {}
  253. }
  254. const { project_id } = req.params
  255. logger.log({ project_id }, 'unarchiving all docs')
  256. return DocArchive.unArchiveAllDocs(project_id, function (error) {
  257. if (error != null) {
  258. return next(error)
  259. }
  260. return res.sendStatus(200)
  261. })
  262. },
  263. destroyAllDocs(req, res, next) {
  264. if (next == null) {
  265. next = function (error) {}
  266. }
  267. const { project_id } = req.params
  268. logger.log({ project_id }, 'destroying all docs')
  269. return DocArchive.destroyAllDocs(project_id, function (error) {
  270. if (error != null) {
  271. return next(error)
  272. }
  273. return res.sendStatus(204)
  274. })
  275. },
  276. healthCheck(req, res) {
  277. return HealthChecker.check(function (err) {
  278. if (err != null) {
  279. logger.err({ err }, 'error performing health check')
  280. return res.sendStatus(500)
  281. } else {
  282. return res.sendStatus(200)
  283. }
  284. })
  285. }
  286. }