2020-02-16 09:03:03 -05:00
|
|
|
/* eslint-disable
|
|
|
|
camelcase,
|
|
|
|
*/
|
2020-02-16 09:03:09 -05:00
|
|
|
let DocstoreClient
|
|
|
|
const request = require('request').defaults({ jar: false })
|
2021-07-12 12:47:20 -04:00
|
|
|
const settings = require('@overleaf/settings')
|
2020-07-23 14:43:33 -04:00
|
|
|
const Persistor = require('../../../../app/js/PersistorManager')
|
|
|
|
|
|
|
|
async function streamToString(stream) {
|
|
|
|
const chunks = []
|
|
|
|
return new Promise((resolve, reject) => {
|
2021-07-13 07:04:48 -04:00
|
|
|
stream.on('data', chunk => chunks.push(chunk))
|
2020-07-23 14:43:33 -04:00
|
|
|
stream.on('error', reject)
|
|
|
|
stream.on('end', () => resolve(Buffer.concat(chunks).toString('utf8')))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getStringFromPersistor(persistor, bucket, key) {
|
|
|
|
const stream = await persistor.getObjectStream(bucket, key, {})
|
|
|
|
stream.resume()
|
|
|
|
return streamToString(stream)
|
|
|
|
}
|
2014-04-29 06:49:09 -04:00
|
|
|
|
2020-02-16 09:03:09 -05:00
|
|
|
module.exports = DocstoreClient = {
|
|
|
|
createDoc(project_id, doc_id, lines, version, ranges, callback) {
|
|
|
|
return DocstoreClient.updateDoc(
|
|
|
|
project_id,
|
|
|
|
doc_id,
|
|
|
|
lines,
|
|
|
|
version,
|
|
|
|
ranges,
|
|
|
|
callback
|
|
|
|
)
|
|
|
|
},
|
2014-04-30 08:06:12 -04:00
|
|
|
|
2020-02-16 09:03:09 -05:00
|
|
|
getDoc(project_id, doc_id, qs, callback) {
|
2021-08-03 05:08:17 -04:00
|
|
|
request.get(
|
2020-02-16 09:03:09 -05:00
|
|
|
{
|
|
|
|
url: `http://localhost:${settings.internal.docstore.port}/project/${project_id}/doc/${doc_id}`,
|
|
|
|
json: true,
|
2021-07-13 07:04:48 -04:00
|
|
|
qs,
|
2020-02-16 09:03:09 -05:00
|
|
|
},
|
|
|
|
callback
|
|
|
|
)
|
|
|
|
},
|
2014-06-06 07:37:42 -04:00
|
|
|
|
2021-07-30 11:06:16 -04:00
|
|
|
peekDoc(project_id, doc_id, qs, callback) {
|
2021-08-03 05:07:00 -04:00
|
|
|
request.get(
|
2021-07-30 11:06:16 -04:00
|
|
|
{
|
|
|
|
url: `http://localhost:${settings.internal.docstore.port}/project/${project_id}/doc/${doc_id}/peek`,
|
|
|
|
json: true,
|
|
|
|
qs,
|
|
|
|
},
|
|
|
|
callback
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
2021-01-12 12:29:06 -05:00
|
|
|
isDocDeleted(project_id, doc_id, callback) {
|
|
|
|
request.get(
|
|
|
|
{
|
|
|
|
url: `http://localhost:${settings.internal.docstore.port}/project/${project_id}/doc/${doc_id}/deleted`,
|
2021-07-13 07:04:48 -04:00
|
|
|
json: true,
|
2021-01-12 12:29:06 -05:00
|
|
|
},
|
|
|
|
callback
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
2020-02-16 09:03:09 -05:00
|
|
|
getAllDocs(project_id, callback) {
|
2021-08-03 05:08:17 -04:00
|
|
|
request.get(
|
2020-02-16 09:03:09 -05:00
|
|
|
{
|
|
|
|
url: `http://localhost:${settings.internal.docstore.port}/project/${project_id}/doc`,
|
2021-07-13 07:04:48 -04:00
|
|
|
json: true,
|
2020-02-16 09:03:09 -05:00
|
|
|
},
|
2020-07-23 14:43:33 -04:00
|
|
|
(req, res, body) => {
|
|
|
|
callback(req, res, body)
|
|
|
|
}
|
2020-02-16 09:03:09 -05:00
|
|
|
)
|
|
|
|
},
|
2014-04-29 06:49:09 -04:00
|
|
|
|
2021-02-18 05:10:14 -05:00
|
|
|
getAllDeletedDocs(project_id, callback) {
|
|
|
|
request.get(
|
|
|
|
{
|
|
|
|
url: `http://localhost:${settings.internal.docstore.port}/project/${project_id}/doc-deleted`,
|
2021-07-13 07:04:48 -04:00
|
|
|
json: true,
|
2021-02-18 05:10:14 -05:00
|
|
|
},
|
|
|
|
(error, res, body) => {
|
|
|
|
if (error) return callback(error)
|
|
|
|
if (res.statusCode !== 200) {
|
|
|
|
return callback(new Error('unexpected statusCode'))
|
|
|
|
}
|
|
|
|
callback(null, body)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
2020-02-16 09:03:09 -05:00
|
|
|
getAllRanges(project_id, callback) {
|
2021-08-03 05:08:17 -04:00
|
|
|
request.get(
|
2020-02-16 09:03:09 -05:00
|
|
|
{
|
|
|
|
url: `http://localhost:${settings.internal.docstore.port}/project/${project_id}/ranges`,
|
2021-07-13 07:04:48 -04:00
|
|
|
json: true,
|
2020-02-16 09:03:09 -05:00
|
|
|
},
|
|
|
|
callback
|
|
|
|
)
|
|
|
|
},
|
2014-04-30 08:06:12 -04:00
|
|
|
|
2020-02-16 09:03:09 -05:00
|
|
|
updateDoc(project_id, doc_id, lines, version, ranges, callback) {
|
|
|
|
return request.post(
|
|
|
|
{
|
|
|
|
url: `http://localhost:${settings.internal.docstore.port}/project/${project_id}/doc/${doc_id}`,
|
|
|
|
json: {
|
|
|
|
lines,
|
|
|
|
version,
|
2021-07-13 07:04:48 -04:00
|
|
|
ranges,
|
|
|
|
},
|
2020-02-16 09:03:09 -05:00
|
|
|
},
|
|
|
|
callback
|
|
|
|
)
|
|
|
|
},
|
2020-02-16 09:03:02 -05:00
|
|
|
|
2021-02-15 08:13:48 -05:00
|
|
|
deleteDoc(project_id, doc_id, callback) {
|
|
|
|
DocstoreClient.deleteDocWithDateAndName(
|
|
|
|
project_id,
|
|
|
|
doc_id,
|
|
|
|
new Date(),
|
|
|
|
'main.tex',
|
|
|
|
callback
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
|
|
|
deleteDocWithDate(project_id, doc_id, date, callback) {
|
|
|
|
DocstoreClient.deleteDocWithDateAndName(
|
|
|
|
project_id,
|
|
|
|
doc_id,
|
|
|
|
date,
|
|
|
|
'main.tex',
|
|
|
|
callback
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
|
|
|
deleteDocWithName(project_id, doc_id, name, callback) {
|
|
|
|
DocstoreClient.deleteDocWithDateAndName(
|
|
|
|
project_id,
|
|
|
|
doc_id,
|
|
|
|
new Date(),
|
|
|
|
name,
|
|
|
|
callback
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
|
|
|
deleteDocWithDateAndName(project_id, doc_id, deletedAt, name, callback) {
|
|
|
|
request.patch(
|
|
|
|
{
|
|
|
|
url: `http://localhost:${settings.internal.docstore.port}/project/${project_id}/doc/${doc_id}`,
|
2021-07-13 07:04:48 -04:00
|
|
|
json: { name, deleted: true, deletedAt },
|
2021-02-15 08:13:48 -05:00
|
|
|
},
|
|
|
|
callback
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
2020-02-16 09:03:09 -05:00
|
|
|
archiveAllDoc(project_id, callback) {
|
2021-08-03 05:08:17 -04:00
|
|
|
request.post(
|
2020-02-16 09:03:09 -05:00
|
|
|
{
|
2021-07-13 07:04:48 -04:00
|
|
|
url: `http://localhost:${settings.internal.docstore.port}/project/${project_id}/archive`,
|
2020-02-16 09:03:09 -05:00
|
|
|
},
|
|
|
|
callback
|
|
|
|
)
|
|
|
|
},
|
2020-02-16 09:03:02 -05:00
|
|
|
|
2021-01-04 06:55:47 -05:00
|
|
|
archiveDocById(project_id, doc_id, callback) {
|
2021-08-03 05:08:17 -04:00
|
|
|
request.post(
|
2021-01-04 06:55:47 -05:00
|
|
|
{
|
2021-07-13 07:04:48 -04:00
|
|
|
url: `http://localhost:${settings.internal.docstore.port}/project/${project_id}/doc/${doc_id}/archive`,
|
2021-01-04 06:55:47 -05:00
|
|
|
},
|
|
|
|
callback
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
2020-02-16 09:03:09 -05:00
|
|
|
destroyAllDoc(project_id, callback) {
|
2021-08-03 05:08:17 -04:00
|
|
|
request.post(
|
2020-02-16 09:03:09 -05:00
|
|
|
{
|
2021-07-13 07:04:48 -04:00
|
|
|
url: `http://localhost:${settings.internal.docstore.port}/project/${project_id}/destroy`,
|
2020-02-16 09:03:09 -05:00
|
|
|
},
|
|
|
|
callback
|
|
|
|
)
|
|
|
|
},
|
2020-02-16 09:03:02 -05:00
|
|
|
|
2020-02-16 09:03:09 -05:00
|
|
|
getS3Doc(project_id, doc_id, callback) {
|
2020-07-23 14:43:33 -04:00
|
|
|
getStringFromPersistor(
|
|
|
|
Persistor,
|
|
|
|
settings.docstore.bucket,
|
|
|
|
`${project_id}/${doc_id}`
|
|
|
|
)
|
2021-07-13 07:04:48 -04:00
|
|
|
.then(data => {
|
2020-07-23 14:43:33 -04:00
|
|
|
callback(null, JSON.parse(data))
|
|
|
|
})
|
|
|
|
.catch(callback)
|
2021-07-13 07:04:48 -04:00
|
|
|
},
|
2020-02-16 09:03:09 -05:00
|
|
|
}
|