2020-02-16 09:02:21 -05:00
|
|
|
const DocManager = require('./DocManager')
|
2021-12-14 08:00:35 -05:00
|
|
|
const logger = require('@overleaf/logger')
|
2020-02-16 09:02:21 -05:00
|
|
|
const DocArchive = require('./DocArchiveManager')
|
|
|
|
const HealthChecker = require('./HealthChecker')
|
2022-04-26 07:16:36 -04:00
|
|
|
const Errors = require('./Errors')
|
2021-07-12 12:47:20 -04:00
|
|
|
const Settings = require('@overleaf/settings')
|
2015-10-16 05:13:54 -04:00
|
|
|
|
2022-04-19 08:28:47 -04:00
|
|
|
function getDoc(req, res, next) {
|
|
|
|
const { doc_id: docId, project_id: projectId } = req.params
|
|
|
|
const includeDeleted = req.query.include_deleted === 'true'
|
2022-05-16 08:38:18 -04:00
|
|
|
logger.debug({ projectId, docId }, 'getting doc')
|
2022-04-19 08:28:47 -04:00
|
|
|
DocManager.getFullDoc(projectId, docId, function (error, doc) {
|
|
|
|
if (error) {
|
|
|
|
return next(error)
|
2020-02-16 09:02:21 -05:00
|
|
|
}
|
2022-05-16 08:38:18 -04:00
|
|
|
logger.debug({ docId, projectId }, 'got doc')
|
2022-04-19 08:28:47 -04:00
|
|
|
if (doc == null) {
|
|
|
|
res.sendStatus(404)
|
|
|
|
} else if (doc.deleted && !includeDeleted) {
|
|
|
|
res.sendStatus(404)
|
|
|
|
} else {
|
|
|
|
res.json(_buildDocView(doc))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2021-07-30 11:06:16 -04:00
|
|
|
|
2022-04-19 08:28:47 -04:00
|
|
|
function peekDoc(req, res, next) {
|
|
|
|
const { doc_id: docId, project_id: projectId } = req.params
|
2022-05-16 08:38:18 -04:00
|
|
|
logger.debug({ projectId, docId }, 'peeking doc')
|
2022-04-19 08:28:47 -04:00
|
|
|
DocManager.peekDoc(projectId, docId, function (error, doc) {
|
|
|
|
if (error) {
|
|
|
|
return next(error)
|
|
|
|
}
|
|
|
|
if (doc == null) {
|
|
|
|
res.sendStatus(404)
|
|
|
|
} else {
|
|
|
|
res.setHeader('x-doc-status', doc.inS3 ? 'archived' : 'active')
|
|
|
|
res.json(_buildDocView(doc))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function 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 })
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function getRawDoc(req, res, next) {
|
|
|
|
const { doc_id: docId, project_id: projectId } = req.params
|
2022-05-16 08:38:18 -04:00
|
|
|
logger.debug({ projectId, docId }, 'getting raw doc')
|
2022-04-19 08:28:47 -04:00
|
|
|
DocManager.getDocLines(projectId, docId, function (error, doc) {
|
|
|
|
if (error) {
|
|
|
|
return next(error)
|
|
|
|
}
|
|
|
|
if (doc == null) {
|
|
|
|
res.sendStatus(404)
|
|
|
|
} else {
|
|
|
|
res.setHeader('content-type', 'text/plain')
|
|
|
|
res.send(_buildRawDocView(doc))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function getAllDocs(req, res, next) {
|
|
|
|
const { project_id: projectId } = req.params
|
2022-05-16 08:38:18 -04:00
|
|
|
logger.debug({ projectId }, 'getting all docs')
|
2022-04-19 08:28:47 -04:00
|
|
|
DocManager.getAllNonDeletedDocs(
|
|
|
|
projectId,
|
|
|
|
{ lines: true, rev: true },
|
|
|
|
function (error, docs) {
|
|
|
|
if (docs == null) {
|
|
|
|
docs = []
|
|
|
|
}
|
2021-07-30 11:06:16 -04:00
|
|
|
if (error) {
|
|
|
|
return next(error)
|
|
|
|
}
|
2022-10-11 07:33:01 -04:00
|
|
|
const docViews = _buildDocsArrayView(projectId, docs)
|
|
|
|
for (const docView of docViews) {
|
|
|
|
if (!docView.lines) {
|
|
|
|
logger.warn({ projectId, docId: docView._id }, 'missing doc lines')
|
|
|
|
docView.lines = []
|
|
|
|
}
|
|
|
|
}
|
|
|
|
res.json(docViews)
|
2022-04-19 08:28:47 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
2020-02-16 09:02:21 -05:00
|
|
|
|
2022-04-19 08:28:47 -04:00
|
|
|
function getAllDeletedDocs(req, res, next) {
|
|
|
|
const { project_id: projectId } = req.params
|
2022-05-16 08:38:18 -04:00
|
|
|
logger.debug({ projectId }, 'getting all deleted docs')
|
2022-04-19 08:28:47 -04:00
|
|
|
DocManager.getAllDeletedDocs(
|
|
|
|
projectId,
|
|
|
|
{ name: true, deletedAt: true },
|
|
|
|
function (error, docs) {
|
2021-01-12 12:29:06 -05:00
|
|
|
if (error) {
|
|
|
|
return next(error)
|
|
|
|
}
|
2022-04-19 08:28:47 -04:00
|
|
|
res.json(
|
|
|
|
docs.map(doc => ({
|
|
|
|
_id: doc._id.toString(),
|
|
|
|
name: doc.name,
|
|
|
|
deletedAt: doc.deletedAt,
|
|
|
|
}))
|
|
|
|
)
|
2020-02-16 09:02:21 -05:00
|
|
|
}
|
2022-04-19 08:28:47 -04:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
function getAllRanges(req, res, next) {
|
|
|
|
const { project_id: projectId } = req.params
|
2022-05-16 08:38:18 -04:00
|
|
|
logger.debug({ projectId }, 'getting all ranges')
|
2022-04-19 08:28:47 -04:00
|
|
|
DocManager.getAllNonDeletedDocs(
|
|
|
|
projectId,
|
|
|
|
{ ranges: true },
|
|
|
|
function (error, docs) {
|
|
|
|
if (docs == null) {
|
|
|
|
docs = []
|
2020-02-16 09:02:21 -05:00
|
|
|
}
|
2022-04-19 08:28:47 -04:00
|
|
|
if (error) {
|
|
|
|
return next(error)
|
2020-02-16 09:02:21 -05:00
|
|
|
}
|
2022-04-19 08:28:47 -04:00
|
|
|
res.json(_buildDocsArrayView(projectId, docs))
|
2020-02-16 09:02:21 -05:00
|
|
|
}
|
2022-04-19 08:28:47 -04:00
|
|
|
)
|
|
|
|
}
|
2020-02-16 09:02:21 -05:00
|
|
|
|
2022-04-19 08:28:47 -04:00
|
|
|
function updateDoc(req, res, next) {
|
|
|
|
const { doc_id: docId, project_id: projectId } = req.params
|
|
|
|
const lines = req.body?.lines
|
|
|
|
const version = req.body?.version
|
|
|
|
const ranges = req.body?.ranges
|
2021-02-18 05:10:14 -05:00
|
|
|
|
2022-04-19 08:28:47 -04:00
|
|
|
if (lines == null || !(lines instanceof Array)) {
|
|
|
|
logger.error({ projectId, docId }, 'no doc lines provided')
|
|
|
|
res.sendStatus(400) // Bad Request
|
|
|
|
return
|
|
|
|
}
|
2020-02-16 09:02:21 -05:00
|
|
|
|
2022-04-19 08:28:47 -04:00
|
|
|
if (version == null || typeof version !== 'number') {
|
|
|
|
logger.error({ projectId, docId }, 'no doc version provided')
|
|
|
|
res.sendStatus(400) // Bad Request
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ranges == null) {
|
|
|
|
logger.error({ projectId, docId }, 'no doc ranges provided')
|
|
|
|
res.sendStatus(400) // Bad Request
|
|
|
|
return
|
|
|
|
}
|
2020-02-16 09:02:21 -05:00
|
|
|
|
2022-04-19 08:28:47 -04:00
|
|
|
const bodyLength = lines.reduce((len, line) => line.length + len, 0)
|
|
|
|
if (bodyLength > Settings.max_doc_length) {
|
|
|
|
logger.error({ projectId, docId, bodyLength }, 'document body too large')
|
|
|
|
res.status(413).send('document body too large')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-05-16 08:38:18 -04:00
|
|
|
logger.debug({ projectId, docId }, 'got http request to update doc')
|
2022-04-19 08:28:47 -04:00
|
|
|
DocManager.updateDoc(
|
|
|
|
projectId,
|
|
|
|
docId,
|
|
|
|
lines,
|
|
|
|
version,
|
|
|
|
ranges,
|
|
|
|
function (error, modified, rev) {
|
|
|
|
if (error) {
|
|
|
|
return next(error)
|
|
|
|
}
|
|
|
|
res.json({
|
|
|
|
modified,
|
|
|
|
rev,
|
|
|
|
})
|
2020-02-16 09:02:21 -05:00
|
|
|
}
|
2022-04-19 08:28:47 -04:00
|
|
|
)
|
|
|
|
}
|
2020-02-16 09:02:21 -05:00
|
|
|
|
2022-04-19 08:28:47 -04:00
|
|
|
function patchDoc(req, res, next) {
|
|
|
|
const { doc_id: docId, project_id: projectId } = req.params
|
2022-05-16 08:38:18 -04:00
|
|
|
logger.debug({ projectId, docId }, 'patching doc')
|
2022-04-19 08:28:47 -04:00
|
|
|
|
|
|
|
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(projectId, docId, meta, function (error) {
|
|
|
|
if (error) {
|
|
|
|
return next(error)
|
2020-02-16 09:02:21 -05:00
|
|
|
}
|
2022-04-19 08:28:47 -04:00
|
|
|
res.sendStatus(204)
|
|
|
|
})
|
|
|
|
}
|
2020-02-16 09:02:21 -05:00
|
|
|
|
2022-04-19 08:28:47 -04:00
|
|
|
function _buildDocView(doc) {
|
|
|
|
const docView = { _id: doc._id?.toString() }
|
|
|
|
for (const attribute of ['lines', 'rev', 'version', 'ranges', 'deleted']) {
|
|
|
|
if (doc[attribute] != null) {
|
|
|
|
docView[attribute] = doc[attribute]
|
2020-02-16 09:02:21 -05:00
|
|
|
}
|
2022-04-19 08:28:47 -04:00
|
|
|
}
|
|
|
|
return docView
|
|
|
|
}
|
2020-02-16 09:02:21 -05:00
|
|
|
|
2022-04-19 08:28:47 -04:00
|
|
|
function _buildRawDocView(doc) {
|
|
|
|
return (doc?.lines ?? []).join('\n')
|
|
|
|
}
|
|
|
|
|
|
|
|
function _buildDocsArrayView(projectId, docs) {
|
|
|
|
const docViews = []
|
|
|
|
for (const doc of docs) {
|
|
|
|
if (doc != null) {
|
|
|
|
// There can end up being null docs for some reason :( (probably a race condition)
|
|
|
|
docViews.push(_buildDocView(doc))
|
|
|
|
} else {
|
2020-02-16 09:02:21 -05:00
|
|
|
logger.error(
|
2022-04-19 08:28:47 -04:00
|
|
|
{ err: new Error('null doc'), projectId },
|
|
|
|
'encountered null doc'
|
2020-02-16 09:02:21 -05:00
|
|
|
)
|
|
|
|
}
|
2022-04-19 08:28:47 -04:00
|
|
|
}
|
|
|
|
return docViews
|
|
|
|
}
|
2020-02-16 09:02:21 -05:00
|
|
|
|
2022-04-19 08:28:47 -04:00
|
|
|
function archiveAllDocs(req, res, next) {
|
|
|
|
const { project_id: projectId } = req.params
|
2022-05-16 08:38:18 -04:00
|
|
|
logger.debug({ projectId }, 'archiving all docs')
|
2022-04-19 08:28:47 -04:00
|
|
|
DocArchive.archiveAllDocs(projectId, function (error) {
|
|
|
|
if (error) {
|
|
|
|
return next(error)
|
2020-02-16 09:02:21 -05:00
|
|
|
}
|
2022-04-19 08:28:47 -04:00
|
|
|
res.sendStatus(204)
|
|
|
|
})
|
|
|
|
}
|
2020-02-16 09:02:21 -05:00
|
|
|
|
2022-04-19 08:28:47 -04:00
|
|
|
function archiveDoc(req, res, next) {
|
|
|
|
const { doc_id: docId, project_id: projectId } = req.params
|
2022-05-16 08:38:18 -04:00
|
|
|
logger.debug({ projectId, docId }, 'archiving a doc')
|
2022-05-17 07:49:13 -04:00
|
|
|
DocArchive.archiveDoc(projectId, docId, function (error) {
|
2022-04-19 08:28:47 -04:00
|
|
|
if (error) {
|
|
|
|
return next(error)
|
2020-02-16 09:02:21 -05:00
|
|
|
}
|
2022-04-19 08:28:47 -04:00
|
|
|
res.sendStatus(204)
|
|
|
|
})
|
|
|
|
}
|
2020-02-16 09:02:21 -05:00
|
|
|
|
2022-04-19 08:28:47 -04:00
|
|
|
function unArchiveAllDocs(req, res, next) {
|
|
|
|
const { project_id: projectId } = req.params
|
2022-05-16 08:38:18 -04:00
|
|
|
logger.debug({ projectId }, 'unarchiving all docs')
|
2022-04-26 11:10:15 -04:00
|
|
|
DocArchive.unArchiveAllDocs(projectId, function (err) {
|
|
|
|
if (err) {
|
|
|
|
if (err instanceof Errors.DocRevValueError) {
|
|
|
|
logger.warn({ err }, 'Failed to unarchive doc')
|
2022-04-26 07:16:36 -04:00
|
|
|
return res.sendStatus(409)
|
|
|
|
}
|
2022-04-26 11:10:15 -04:00
|
|
|
return next(err)
|
2020-02-16 09:02:21 -05:00
|
|
|
}
|
2022-04-19 08:28:47 -04:00
|
|
|
res.sendStatus(200)
|
|
|
|
})
|
|
|
|
}
|
2020-02-16 09:02:21 -05:00
|
|
|
|
2022-04-26 07:16:36 -04:00
|
|
|
function destroyProject(req, res, next) {
|
2022-04-19 08:28:47 -04:00
|
|
|
const { project_id: projectId } = req.params
|
2022-05-16 08:38:18 -04:00
|
|
|
logger.debug({ projectId }, 'destroying all docs')
|
2022-04-26 07:16:36 -04:00
|
|
|
DocArchive.destroyProject(projectId, function (error) {
|
2022-04-19 08:28:47 -04:00
|
|
|
if (error) {
|
|
|
|
return next(error)
|
2020-02-16 09:02:21 -05:00
|
|
|
}
|
2022-04-19 08:28:47 -04:00
|
|
|
res.sendStatus(204)
|
|
|
|
})
|
|
|
|
}
|
2020-02-16 09:02:21 -05:00
|
|
|
|
2022-04-19 08:28:47 -04:00
|
|
|
function healthCheck(req, res) {
|
|
|
|
HealthChecker.check(function (err) {
|
|
|
|
if (err) {
|
|
|
|
logger.err({ err }, 'error performing health check')
|
|
|
|
res.sendStatus(500)
|
|
|
|
} else {
|
|
|
|
res.sendStatus(200)
|
2020-02-16 09:02:21 -05:00
|
|
|
}
|
2022-04-19 08:28:47 -04:00
|
|
|
})
|
|
|
|
}
|
2020-02-16 09:02:21 -05:00
|
|
|
|
2022-04-19 08:28:47 -04:00
|
|
|
module.exports = {
|
|
|
|
getDoc,
|
|
|
|
peekDoc,
|
|
|
|
isDocDeleted,
|
|
|
|
getRawDoc,
|
|
|
|
getAllDocs,
|
|
|
|
getAllDeletedDocs,
|
|
|
|
getAllRanges,
|
|
|
|
updateDoc,
|
|
|
|
patchDoc,
|
|
|
|
archiveAllDocs,
|
|
|
|
archiveDoc,
|
|
|
|
unArchiveAllDocs,
|
2022-04-26 07:16:36 -04:00
|
|
|
destroyProject,
|
2022-04-19 08:28:47 -04:00
|
|
|
healthCheck,
|
2020-02-16 09:02:21 -05:00
|
|
|
}
|