HttpController.js 7.7 KB

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