| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325 |
- /* eslint-disable
- camelcase,
- handle-callback-err,
- valid-typeof,
- */
- // TODO: This file was created by bulk-decaffeinate.
- // Fix any style issues and re-enable lint.
- /*
- * decaffeinate suggestions:
- * DS101: Remove unnecessary use of Array.from
- * DS102: Remove unnecessary code created because of implicit returns
- * DS207: Consider shorter variations of null checks
- * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
- */
- let HttpController
- const DocManager = require('./DocManager')
- const logger = require('logger-sharelatex')
- const DocArchive = require('./DocArchiveManager')
- const HealthChecker = require('./HealthChecker')
- const Settings = require('@overleaf/settings')
- module.exports = HttpController = {
- getDoc(req, res, next) {
- if (next == null) {
- next = function (error) {}
- }
- const { project_id } = req.params
- const { doc_id } = req.params
- const include_deleted =
- (req.query != null ? req.query.include_deleted : undefined) === 'true'
- logger.log({ project_id, doc_id }, 'getting doc')
- return DocManager.getFullDoc(project_id, doc_id, function (error, doc) {
- if (error != null) {
- return next(error)
- }
- logger.log({ doc_id, project_id }, 'got doc')
- if (doc == null) {
- return res.sendStatus(404)
- } else if (doc.deleted && !include_deleted) {
- return res.sendStatus(404)
- } else {
- return res.json(HttpController._buildDocView(doc))
- }
- })
- },
- peekDoc(req, res, next) {
- const { project_id } = req.params
- const { doc_id } = req.params
- logger.log({ project_id, doc_id }, 'peeking doc')
- DocManager.peekDoc(project_id, doc_id, function (error, doc) {
- if (error) {
- return next(error)
- }
- if (doc == null) {
- return res.sendStatus(404)
- } else {
- return res.json(HttpController._buildDocView(doc))
- }
- })
- },
- isDocDeleted(req, res, next) {
- const { doc_id: docId, project_id: projectId } = req.params
- DocManager.isDocDeleted(projectId, docId, function (error, deleted) {
- if (error) {
- return next(error)
- }
- res.json({ deleted })
- })
- },
- getRawDoc(req, res, next) {
- if (next == null) {
- next = function (error) {}
- }
- const { project_id } = req.params
- const { doc_id } = req.params
- logger.log({ project_id, doc_id }, 'getting raw doc')
- return DocManager.getDocLines(project_id, doc_id, function (error, doc) {
- if (error != null) {
- return next(error)
- }
- if (doc == null) {
- return res.sendStatus(404)
- } else {
- res.setHeader('content-type', 'text/plain')
- return res.send(HttpController._buildRawDocView(doc))
- }
- })
- },
- getAllDocs(req, res, next) {
- if (next == null) {
- next = function (error) {}
- }
- const { project_id } = req.params
- logger.log({ project_id }, 'getting all docs')
- return DocManager.getAllNonDeletedDocs(
- project_id,
- { lines: true, rev: true },
- function (error, docs) {
- if (docs == null) {
- docs = []
- }
- if (error != null) {
- return next(error)
- }
- return res.json(HttpController._buildDocsArrayView(project_id, docs))
- }
- )
- },
- getAllDeletedDocs(req, res, next) {
- const { project_id } = req.params
- logger.log({ project_id }, 'getting all deleted docs')
- DocManager.getAllDeletedDocs(
- project_id,
- { name: true },
- function (error, docs) {
- if (error) {
- return next(error)
- }
- res.json(
- docs.map(doc => {
- return { _id: doc._id.toString(), name: doc.name }
- })
- )
- }
- )
- },
- getAllRanges(req, res, next) {
- if (next == null) {
- next = function (error) {}
- }
- const { project_id } = req.params
- logger.log({ project_id }, 'getting all ranges')
- return DocManager.getAllNonDeletedDocs(
- project_id,
- { ranges: true },
- function (error, docs) {
- if (docs == null) {
- docs = []
- }
- if (error != null) {
- return next(error)
- }
- return res.json(HttpController._buildDocsArrayView(project_id, docs))
- }
- )
- },
- updateDoc(req, res, next) {
- if (next == null) {
- next = function (error) {}
- }
- const { project_id } = req.params
- const { doc_id } = req.params
- const lines = req.body != null ? req.body.lines : undefined
- const version = req.body != null ? req.body.version : undefined
- const ranges = req.body != null ? req.body.ranges : undefined
- if (lines == null || !(lines instanceof Array)) {
- logger.error({ project_id, doc_id }, 'no doc lines provided')
- res.sendStatus(400) // Bad Request
- return
- }
- if (version == null || typeof version === !'number') {
- logger.error({ project_id, doc_id }, 'no doc version provided')
- res.sendStatus(400) // Bad Request
- return
- }
- if (ranges == null) {
- logger.error({ project_id, doc_id }, 'no doc ranges provided')
- res.sendStatus(400) // Bad Request
- return
- }
- const bodyLength = lines.reduce((len, line) => line.length + len, 0)
- if (bodyLength > Settings.max_doc_length) {
- logger.error(
- { project_id, doc_id, bodyLength },
- 'document body too large'
- )
- res.status(413).send('document body too large')
- return
- }
- logger.log({ project_id, doc_id }, 'got http request to update doc')
- return DocManager.updateDoc(
- project_id,
- doc_id,
- lines,
- version,
- ranges,
- function (error, modified, rev) {
- if (error != null) {
- return next(error)
- }
- return res.json({
- modified,
- rev,
- })
- }
- )
- },
- patchDoc(req, res, next) {
- const { project_id, doc_id } = req.params
- logger.log({ project_id, doc_id }, 'patching doc')
- const allowedFields = ['deleted', 'deletedAt', 'name']
- const meta = {}
- Object.entries(req.body).forEach(([field, value]) => {
- if (allowedFields.includes(field)) {
- meta[field] = value
- } else {
- logger.fatal({ field }, 'joi validation for pathDoc is broken')
- }
- })
- DocManager.patchDoc(project_id, doc_id, meta, function (error) {
- if (error) {
- return next(error)
- }
- res.sendStatus(204)
- })
- },
- _buildDocView(doc) {
- const doc_view = { _id: doc._id != null ? doc._id.toString() : undefined }
- for (const attribute of ['lines', 'rev', 'version', 'ranges', 'deleted']) {
- if (doc[attribute] != null) {
- doc_view[attribute] = doc[attribute]
- }
- }
- return doc_view
- },
- _buildRawDocView(doc) {
- return ((doc != null ? doc.lines : undefined) || []).join('\n')
- },
- _buildDocsArrayView(project_id, docs) {
- const docViews = []
- for (const doc of Array.from(docs)) {
- if (doc != null) {
- // There can end up being null docs for some reason :( (probably a race condition)
- docViews.push(HttpController._buildDocView(doc))
- } else {
- logger.error(
- { err: new Error('null doc'), project_id },
- 'encountered null doc'
- )
- }
- }
- return docViews
- },
- archiveAllDocs(req, res, next) {
- if (next == null) {
- next = function (error) {}
- }
- const { project_id } = req.params
- logger.log({ project_id }, 'archiving all docs')
- return DocArchive.archiveAllDocs(project_id, function (error) {
- if (error != null) {
- return next(error)
- }
- return res.sendStatus(204)
- })
- },
- archiveDoc(req, res, next) {
- const { project_id, doc_id } = req.params
- logger.log({ project_id, doc_id }, 'archiving a doc')
- DocArchive.archiveDocById(project_id, doc_id, function (error) {
- if (error) {
- return next(error)
- }
- res.sendStatus(204)
- })
- },
- unArchiveAllDocs(req, res, next) {
- if (next == null) {
- next = function (error) {}
- }
- const { project_id } = req.params
- logger.log({ project_id }, 'unarchiving all docs')
- return DocArchive.unArchiveAllDocs(project_id, function (error) {
- if (error != null) {
- return next(error)
- }
- return res.sendStatus(200)
- })
- },
- destroyAllDocs(req, res, next) {
- if (next == null) {
- next = function (error) {}
- }
- const { project_id } = req.params
- logger.log({ project_id }, 'destroying all docs')
- return DocArchive.destroyAllDocs(project_id, function (error) {
- if (error != null) {
- return next(error)
- }
- return res.sendStatus(204)
- })
- },
- healthCheck(req, res) {
- return HealthChecker.check(function (err) {
- if (err != null) {
- logger.err({ err }, 'error performing health check')
- return res.sendStatus(500)
- } else {
- return res.sendStatus(200)
- }
- })
- },
- }
|