2020-02-17 12:34:21 -05:00
|
|
|
/* eslint-disable
|
|
|
|
camelcase,
|
|
|
|
no-unused-vars,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
2020-02-17 12:34:04 -05:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
*/
|
2020-02-17 12:34:28 -05:00
|
|
|
let HttpController
|
|
|
|
const UpdatesManager = require('./UpdatesManager')
|
|
|
|
const DiffManager = require('./DiffManager')
|
|
|
|
const PackManager = require('./PackManager')
|
|
|
|
const RestoreManager = require('./RestoreManager')
|
2021-08-16 08:09:52 -04:00
|
|
|
const ZipManager = require('./ZipManager')
|
2021-12-14 08:00:35 -05:00
|
|
|
const logger = require('@overleaf/logger')
|
2020-02-17 12:34:28 -05:00
|
|
|
const HealthChecker = require('./HealthChecker')
|
|
|
|
const _ = require('underscore')
|
2021-08-16 08:09:52 -04:00
|
|
|
const Path = require('path')
|
|
|
|
const { pipeline } = require('stream')
|
2020-02-17 12:34:28 -05:00
|
|
|
|
|
|
|
module.exports = HttpController = {
|
|
|
|
flushDoc(req, res, next) {
|
|
|
|
if (next == null) {
|
2021-10-27 05:49:18 -04:00
|
|
|
next = function () {}
|
2020-02-17 12:34:28 -05:00
|
|
|
}
|
|
|
|
const { doc_id } = req.params
|
|
|
|
const { project_id } = req.params
|
2022-05-16 08:38:18 -04:00
|
|
|
logger.debug({ project_id, doc_id }, 'compressing doc history')
|
2020-02-17 12:34:28 -05:00
|
|
|
return UpdatesManager.processUncompressedUpdatesWithLock(
|
|
|
|
project_id,
|
|
|
|
doc_id,
|
2020-06-04 04:24:21 -04:00
|
|
|
function (error) {
|
2020-02-17 12:34:28 -05:00
|
|
|
if (error != null) {
|
|
|
|
return next(error)
|
|
|
|
}
|
2020-03-23 06:10:24 -04:00
|
|
|
return res.sendStatus(204)
|
2020-02-17 12:34:28 -05:00
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
|
|
|
flushProject(req, res, next) {
|
|
|
|
if (next == null) {
|
2021-10-27 05:49:18 -04:00
|
|
|
next = function () {}
|
2020-02-17 12:34:28 -05:00
|
|
|
}
|
|
|
|
const { project_id } = req.params
|
2022-05-16 08:38:18 -04:00
|
|
|
logger.debug({ project_id }, 'compressing project history')
|
2020-02-17 12:34:28 -05:00
|
|
|
return UpdatesManager.processUncompressedUpdatesForProject(
|
|
|
|
project_id,
|
2020-06-04 04:24:21 -04:00
|
|
|
function (error) {
|
2020-02-17 12:34:28 -05:00
|
|
|
if (error != null) {
|
|
|
|
return next(error)
|
|
|
|
}
|
2020-03-23 06:10:24 -04:00
|
|
|
return res.sendStatus(204)
|
2020-02-17 12:34:28 -05:00
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
|
|
|
flushAll(req, res, next) {
|
|
|
|
// limit on projects to flush or -1 for all (default)
|
|
|
|
if (next == null) {
|
2021-10-27 05:49:18 -04:00
|
|
|
next = function () {}
|
2020-02-17 12:34:28 -05:00
|
|
|
}
|
|
|
|
const limit = req.query.limit != null ? parseInt(req.query.limit, 10) : -1
|
2022-05-16 08:38:18 -04:00
|
|
|
logger.debug({ limit }, 'flushing all projects')
|
2020-06-04 04:24:21 -04:00
|
|
|
return UpdatesManager.flushAll(limit, function (error, result) {
|
2020-02-17 12:34:28 -05:00
|
|
|
if (error != null) {
|
|
|
|
return next(error)
|
|
|
|
}
|
|
|
|
const { failed, succeeded, all } = result
|
|
|
|
const status = `${succeeded.length} succeeded, ${failed.length} failed`
|
|
|
|
if (limit === 0) {
|
|
|
|
return res
|
|
|
|
.status(200)
|
|
|
|
.send(`${status}\nwould flush:\n${all.join('\n')}\n`)
|
|
|
|
} else if (failed.length > 0) {
|
2022-05-16 08:38:18 -04:00
|
|
|
logger.debug({ failed, succeeded }, 'error flushing projects')
|
2020-02-17 12:34:28 -05:00
|
|
|
return res
|
|
|
|
.status(500)
|
|
|
|
.send(`${status}\nfailed to flush:\n${failed.join('\n')}\n`)
|
|
|
|
} else {
|
|
|
|
return res
|
|
|
|
.status(200)
|
|
|
|
.send(
|
|
|
|
`${status}\nflushed ${succeeded.length} projects of ${all.length}\n`
|
|
|
|
)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
checkDanglingUpdates(req, res, next) {
|
|
|
|
if (next == null) {
|
2021-10-27 05:49:18 -04:00
|
|
|
next = function () {}
|
2020-02-17 12:34:28 -05:00
|
|
|
}
|
2022-05-16 08:38:18 -04:00
|
|
|
logger.debug('checking dangling updates')
|
2020-06-04 04:24:21 -04:00
|
|
|
return UpdatesManager.getDanglingUpdates(function (error, result) {
|
2020-02-17 12:34:28 -05:00
|
|
|
if (error != null) {
|
|
|
|
return next(error)
|
|
|
|
}
|
|
|
|
if (result.length > 0) {
|
2022-05-16 08:38:18 -04:00
|
|
|
logger.debug({ dangling: result }, 'found dangling updates')
|
2020-02-17 12:34:28 -05:00
|
|
|
return res.status(500).send(`dangling updates:\n${result.join('\n')}\n`)
|
|
|
|
} else {
|
|
|
|
return res.status(200).send('no dangling updates found\n')
|
|
|
|
}
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
checkDoc(req, res, next) {
|
|
|
|
if (next == null) {
|
2021-10-27 05:49:18 -04:00
|
|
|
next = function () {}
|
2020-02-17 12:34:28 -05:00
|
|
|
}
|
|
|
|
const { doc_id } = req.params
|
|
|
|
const { project_id } = req.params
|
2022-05-16 08:38:18 -04:00
|
|
|
logger.debug({ project_id, doc_id }, 'checking doc history')
|
2020-06-04 04:24:21 -04:00
|
|
|
return DiffManager.getDocumentBeforeVersion(
|
|
|
|
project_id,
|
|
|
|
doc_id,
|
|
|
|
1,
|
|
|
|
function (error, document, rewoundUpdates) {
|
|
|
|
if (error != null) {
|
|
|
|
return next(error)
|
|
|
|
}
|
|
|
|
const broken = []
|
|
|
|
for (const update of Array.from(rewoundUpdates)) {
|
|
|
|
for (const op of Array.from(update.op)) {
|
|
|
|
if (op.broken === true) {
|
|
|
|
broken.push(op)
|
|
|
|
}
|
2020-02-17 12:34:28 -05:00
|
|
|
}
|
|
|
|
}
|
2020-06-04 04:24:21 -04:00
|
|
|
if (broken.length > 0) {
|
|
|
|
return res.send(broken)
|
|
|
|
} else {
|
|
|
|
return res.sendStatus(204)
|
|
|
|
}
|
2020-02-17 12:34:28 -05:00
|
|
|
}
|
2020-06-04 04:24:21 -04:00
|
|
|
)
|
2020-02-17 12:34:28 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
getDiff(req, res, next) {
|
|
|
|
let from, to
|
|
|
|
if (next == null) {
|
2021-10-27 05:49:18 -04:00
|
|
|
next = function () {}
|
2020-02-17 12:34:28 -05:00
|
|
|
}
|
|
|
|
const { doc_id } = req.params
|
|
|
|
const { project_id } = req.params
|
|
|
|
|
|
|
|
if (req.query.from != null) {
|
|
|
|
from = parseInt(req.query.from, 10)
|
|
|
|
} else {
|
|
|
|
from = null
|
|
|
|
}
|
|
|
|
if (req.query.to != null) {
|
|
|
|
to = parseInt(req.query.to, 10)
|
|
|
|
} else {
|
|
|
|
to = null
|
|
|
|
}
|
|
|
|
|
2022-05-16 08:38:18 -04:00
|
|
|
logger.debug({ project_id, doc_id, from, to }, 'getting diff')
|
2021-07-13 07:04:43 -04:00
|
|
|
return DiffManager.getDiff(
|
|
|
|
project_id,
|
|
|
|
doc_id,
|
|
|
|
from,
|
|
|
|
to,
|
|
|
|
function (error, diff) {
|
|
|
|
if (error != null) {
|
|
|
|
return next(error)
|
|
|
|
}
|
|
|
|
return res.json({ diff })
|
2020-02-17 12:34:28 -05:00
|
|
|
}
|
2021-07-13 07:04:43 -04:00
|
|
|
)
|
2020-02-17 12:34:28 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
getUpdates(req, res, next) {
|
|
|
|
let before, min_count
|
|
|
|
if (next == null) {
|
2021-10-27 05:49:18 -04:00
|
|
|
next = function () {}
|
2020-02-17 12:34:28 -05:00
|
|
|
}
|
|
|
|
const { project_id } = req.params
|
|
|
|
|
|
|
|
if (req.query.before != null) {
|
|
|
|
before = parseInt(req.query.before, 10)
|
|
|
|
}
|
|
|
|
if (req.query.min_count != null) {
|
|
|
|
min_count = parseInt(req.query.min_count, 10)
|
|
|
|
}
|
|
|
|
|
|
|
|
return UpdatesManager.getSummarizedProjectUpdates(
|
|
|
|
project_id,
|
|
|
|
{ before, min_count },
|
2020-06-04 04:24:21 -04:00
|
|
|
function (error, updates, nextBeforeTimestamp) {
|
2020-02-17 12:34:28 -05:00
|
|
|
if (error != null) {
|
|
|
|
return next(error)
|
|
|
|
}
|
|
|
|
return res.json({
|
|
|
|
updates,
|
2021-07-13 07:04:43 -04:00
|
|
|
nextBeforeTimestamp,
|
2020-02-17 12:34:28 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
2021-08-16 08:09:52 -04:00
|
|
|
zipProject(req, res, next) {
|
|
|
|
const { project_id: projectId } = req.params
|
2022-05-16 08:38:18 -04:00
|
|
|
logger.debug({ projectId }, 'exporting project history as zip file')
|
2021-08-16 08:09:52 -04:00
|
|
|
ZipManager.makeTempDirectory((err, tmpdir) => {
|
|
|
|
if (err) {
|
|
|
|
return next(err)
|
|
|
|
}
|
|
|
|
const zipFilePath = Path.join(tmpdir, 'export.zip')
|
|
|
|
ZipManager.exportProject(projectId, zipFilePath, err => {
|
|
|
|
if (err) {
|
|
|
|
ZipManager.cleanupTempDirectory(tmpdir)
|
|
|
|
return next(err)
|
|
|
|
}
|
|
|
|
res.download(zipFilePath, `${projectId}-track-changes.zip`, err => {
|
|
|
|
ZipManager.cleanupTempDirectory(tmpdir)
|
|
|
|
if (err && !res.headersSent) {
|
|
|
|
return next(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
2021-02-23 08:57:27 -05:00
|
|
|
exportProject(req, res, next) {
|
|
|
|
// The project history can be huge:
|
|
|
|
// - updates can weight MBs for insert/delete of full doc
|
|
|
|
// - multiple updates form a pack
|
|
|
|
// Flush updates per pack onto the wire.
|
|
|
|
const { project_id } = req.params
|
2022-05-16 08:38:18 -04:00
|
|
|
logger.debug({ project_id }, 'exporting project history')
|
2021-07-13 07:04:43 -04:00
|
|
|
UpdatesManager.exportProject(
|
|
|
|
project_id,
|
|
|
|
function (err, { updates, userIds }, confirmWrite) {
|
2022-04-13 08:31:32 -04:00
|
|
|
const abortStreaming = req.destroyed || res.finished || res.destroyed
|
2021-07-13 07:04:43 -04:00
|
|
|
if (abortStreaming) {
|
|
|
|
// Tell the producer to stop emitting data
|
|
|
|
if (confirmWrite) confirmWrite(new Error('stop'))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
const hasStartedStreamingResponse = res.headersSent
|
|
|
|
if (err) {
|
|
|
|
logger.error({ project_id, err }, 'export failed')
|
|
|
|
if (!hasStartedStreamingResponse) {
|
|
|
|
// Generate a nice 500
|
|
|
|
return next(err)
|
|
|
|
} else {
|
|
|
|
// Stop streaming
|
|
|
|
return res.destroy()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Compose the response incrementally
|
|
|
|
const isFirstWrite = !hasStartedStreamingResponse
|
|
|
|
const isLastWrite = updates.length === 0
|
|
|
|
if (isFirstWrite) {
|
|
|
|
// The first write will emit the 200 status, headers and start of the
|
|
|
|
// response payload (open array)
|
|
|
|
res.setHeader('Content-Type', 'application/json')
|
|
|
|
res.setHeader('Trailer', 'X-User-Ids')
|
|
|
|
res.writeHead(200)
|
|
|
|
res.write('[')
|
|
|
|
}
|
|
|
|
if (!isFirstWrite && !isLastWrite) {
|
|
|
|
// Starting from the 2nd non-empty write, emit a continuing comma.
|
|
|
|
// write 1: [updates1
|
|
|
|
// write 2: ,updates2
|
|
|
|
// write 3: ,updates3
|
|
|
|
// write N: ]
|
|
|
|
res.write(',')
|
2021-02-23 08:57:27 -05:00
|
|
|
}
|
|
|
|
|
2021-07-13 07:04:43 -04:00
|
|
|
// Every write will emit a blob onto the response stream:
|
|
|
|
// '[update1,update2,...]'
|
|
|
|
// ^^^^^^^^^^^^^^^^^^^
|
|
|
|
res.write(JSON.stringify(updates).slice(1, -1), confirmWrite)
|
2021-02-23 08:57:27 -05:00
|
|
|
|
2021-07-13 07:04:43 -04:00
|
|
|
if (isLastWrite) {
|
|
|
|
// The last write will have no updates and will finish the response
|
|
|
|
// payload (close array) and emit the userIds as trailer.
|
|
|
|
res.addTrailers({ 'X-User-Ids': JSON.stringify(userIds) })
|
|
|
|
res.end(']')
|
|
|
|
}
|
2021-02-23 08:57:27 -05:00
|
|
|
}
|
2021-07-13 07:04:43 -04:00
|
|
|
)
|
2021-02-23 08:57:27 -05:00
|
|
|
},
|
|
|
|
|
2020-02-17 12:34:28 -05:00
|
|
|
restore(req, res, next) {
|
|
|
|
if (next == null) {
|
2021-10-27 05:49:18 -04:00
|
|
|
next = function () {}
|
2020-02-17 12:34:28 -05:00
|
|
|
}
|
|
|
|
let { doc_id, project_id, version } = req.params
|
|
|
|
const user_id = req.headers['x-user-id']
|
|
|
|
version = parseInt(version, 10)
|
|
|
|
return RestoreManager.restoreToBeforeVersion(
|
|
|
|
project_id,
|
|
|
|
doc_id,
|
|
|
|
version,
|
|
|
|
user_id,
|
2020-06-04 04:24:21 -04:00
|
|
|
function (error) {
|
2020-02-17 12:34:28 -05:00
|
|
|
if (error != null) {
|
|
|
|
return next(error)
|
|
|
|
}
|
2020-03-23 06:10:24 -04:00
|
|
|
return res.sendStatus(204)
|
2020-02-17 12:34:28 -05:00
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
|
|
|
pushDocHistory(req, res, next) {
|
|
|
|
if (next == null) {
|
2021-10-27 05:49:18 -04:00
|
|
|
next = function () {}
|
2020-02-17 12:34:28 -05:00
|
|
|
}
|
|
|
|
const { project_id } = req.params
|
|
|
|
const { doc_id } = req.params
|
2022-05-16 08:38:18 -04:00
|
|
|
logger.debug({ project_id, doc_id }, 'pushing all finalised changes to s3')
|
2020-06-04 04:24:21 -04:00
|
|
|
return PackManager.pushOldPacks(project_id, doc_id, function (error) {
|
2020-02-17 12:34:28 -05:00
|
|
|
if (error != null) {
|
|
|
|
return next(error)
|
|
|
|
}
|
2020-03-23 06:10:24 -04:00
|
|
|
return res.sendStatus(204)
|
2020-02-17 12:34:28 -05:00
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
pullDocHistory(req, res, next) {
|
|
|
|
if (next == null) {
|
2021-10-27 05:49:18 -04:00
|
|
|
next = function () {}
|
2020-02-17 12:34:28 -05:00
|
|
|
}
|
|
|
|
const { project_id } = req.params
|
|
|
|
const { doc_id } = req.params
|
2022-05-16 08:38:18 -04:00
|
|
|
logger.debug({ project_id, doc_id }, 'pulling all packs from s3')
|
2020-06-04 04:24:21 -04:00
|
|
|
return PackManager.pullOldPacks(project_id, doc_id, function (error) {
|
2020-02-17 12:34:28 -05:00
|
|
|
if (error != null) {
|
|
|
|
return next(error)
|
|
|
|
}
|
2020-03-23 06:10:24 -04:00
|
|
|
return res.sendStatus(204)
|
2020-02-17 12:34:28 -05:00
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
healthCheck(req, res) {
|
2020-06-04 04:24:21 -04:00
|
|
|
return HealthChecker.check(function (err) {
|
2020-02-17 12:34:28 -05:00
|
|
|
if (err != null) {
|
|
|
|
logger.err({ err }, 'error performing health check')
|
2020-03-23 06:10:24 -04:00
|
|
|
return res.sendStatus(500)
|
2020-02-17 12:34:28 -05:00
|
|
|
} else {
|
2020-03-23 06:10:24 -04:00
|
|
|
return res.sendStatus(200)
|
2020-02-17 12:34:28 -05:00
|
|
|
}
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
checkLock(req, res) {
|
2020-06-04 04:24:21 -04:00
|
|
|
return HealthChecker.checkLock(function (err) {
|
2020-02-17 12:34:28 -05:00
|
|
|
if (err != null) {
|
|
|
|
logger.err({ err }, 'error performing lock check')
|
2020-03-23 06:10:24 -04:00
|
|
|
return res.sendStatus(500)
|
2020-02-17 12:34:28 -05:00
|
|
|
} else {
|
2020-03-23 06:10:24 -04:00
|
|
|
return res.sendStatus(200)
|
2020-02-17 12:34:28 -05:00
|
|
|
}
|
|
|
|
})
|
2021-07-13 07:04:43 -04:00
|
|
|
},
|
2020-02-17 12:34:28 -05:00
|
|
|
}
|