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 = {
|
2021-08-03 05:12:16 -04:00
|
|
|
createDoc(projectId, docId, lines, version, ranges, callback) {
|
2020-02-16 09:03:09 -05:00
|
|
|
return DocstoreClient.updateDoc(
|
2021-08-03 05:12:16 -04:00
|
|
|
projectId,
|
|
|
|
docId,
|
2020-02-16 09:03:09 -05:00
|
|
|
lines,
|
|
|
|
version,
|
|
|
|
ranges,
|
|
|
|
callback
|
|
|
|
)
|
|
|
|
},
|
2014-04-30 08:06:12 -04:00
|
|
|
|
2021-08-03 05:12:16 -04:00
|
|
|
getDoc(projectId, docId, qs, callback) {
|
2021-08-03 05:08:17 -04:00
|
|
|
request.get(
|
2020-02-16 09:03:09 -05:00
|
|
|
{
|
2021-08-03 05:12:16 -04:00
|
|
|
url: `http://localhost:${settings.internal.docstore.port}/project/${projectId}/doc/${docId}`,
|
2020-02-16 09:03:09 -05:00
|
|
|
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-08-03 05:12:16 -04:00
|
|
|
peekDoc(projectId, docId, qs, callback) {
|
2021-08-03 05:07:00 -04:00
|
|
|
request.get(
|
2021-07-30 11:06:16 -04:00
|
|
|
{
|
2021-08-03 05:12:16 -04:00
|
|
|
url: `http://localhost:${settings.internal.docstore.port}/project/${projectId}/doc/${docId}/peek`,
|
2021-07-30 11:06:16 -04:00
|
|
|
json: true,
|
|
|
|
qs,
|
|
|
|
},
|
|
|
|
callback
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
2021-08-03 05:12:16 -04:00
|
|
|
isDocDeleted(projectId, docId, callback) {
|
2021-01-12 12:29:06 -05:00
|
|
|
request.get(
|
|
|
|
{
|
2021-08-03 05:12:16 -04:00
|
|
|
url: `http://localhost:${settings.internal.docstore.port}/project/${projectId}/doc/${docId}/deleted`,
|
2021-07-13 07:04:48 -04:00
|
|
|
json: true,
|
2021-01-12 12:29:06 -05:00
|
|
|
},
|
|
|
|
callback
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
2021-08-03 05:12:16 -04:00
|
|
|
getAllDocs(projectId, callback) {
|
2021-08-03 05:08:17 -04:00
|
|
|
request.get(
|
2020-02-16 09:03:09 -05:00
|
|
|
{
|
2021-08-03 05:12:16 -04:00
|
|
|
url: `http://localhost:${settings.internal.docstore.port}/project/${projectId}/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-08-03 05:12:16 -04:00
|
|
|
getAllDeletedDocs(projectId, callback) {
|
2021-02-18 05:10:14 -05:00
|
|
|
request.get(
|
|
|
|
{
|
2021-08-03 05:12:16 -04:00
|
|
|
url: `http://localhost:${settings.internal.docstore.port}/project/${projectId}/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)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
2021-08-03 05:12:16 -04:00
|
|
|
getAllRanges(projectId, callback) {
|
2021-08-03 05:08:17 -04:00
|
|
|
request.get(
|
2020-02-16 09:03:09 -05:00
|
|
|
{
|
2021-08-03 05:12:16 -04:00
|
|
|
url: `http://localhost:${settings.internal.docstore.port}/project/${projectId}/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
|
|
|
|
2021-08-03 05:12:16 -04:00
|
|
|
updateDoc(projectId, docId, lines, version, ranges, callback) {
|
2020-02-16 09:03:09 -05:00
|
|
|
return request.post(
|
|
|
|
{
|
2021-08-03 05:12:16 -04:00
|
|
|
url: `http://localhost:${settings.internal.docstore.port}/project/${projectId}/doc/${docId}`,
|
2020-02-16 09:03:09 -05:00
|
|
|
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-08-03 05:12:16 -04:00
|
|
|
deleteDoc(projectId, docId, callback) {
|
2021-02-15 08:13:48 -05:00
|
|
|
DocstoreClient.deleteDocWithDateAndName(
|
2021-08-03 05:12:16 -04:00
|
|
|
projectId,
|
|
|
|
docId,
|
2021-02-15 08:13:48 -05:00
|
|
|
new Date(),
|
|
|
|
'main.tex',
|
|
|
|
callback
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
2021-08-03 05:12:16 -04:00
|
|
|
deleteDocWithDate(projectId, docId, date, callback) {
|
2021-02-15 08:13:48 -05:00
|
|
|
DocstoreClient.deleteDocWithDateAndName(
|
2021-08-03 05:12:16 -04:00
|
|
|
projectId,
|
|
|
|
docId,
|
2021-02-15 08:13:48 -05:00
|
|
|
date,
|
|
|
|
'main.tex',
|
|
|
|
callback
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
2021-08-03 05:12:16 -04:00
|
|
|
deleteDocWithName(projectId, docId, name, callback) {
|
2021-02-15 08:13:48 -05:00
|
|
|
DocstoreClient.deleteDocWithDateAndName(
|
2021-08-03 05:12:16 -04:00
|
|
|
projectId,
|
|
|
|
docId,
|
2021-02-15 08:13:48 -05:00
|
|
|
new Date(),
|
|
|
|
name,
|
|
|
|
callback
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
2021-08-03 05:12:16 -04:00
|
|
|
deleteDocWithDateAndName(projectId, docId, deletedAt, name, callback) {
|
2021-02-15 08:13:48 -05:00
|
|
|
request.patch(
|
|
|
|
{
|
2021-08-03 05:12:16 -04:00
|
|
|
url: `http://localhost:${settings.internal.docstore.port}/project/${projectId}/doc/${docId}`,
|
2021-07-13 07:04:48 -04:00
|
|
|
json: { name, deleted: true, deletedAt },
|
2021-02-15 08:13:48 -05:00
|
|
|
},
|
|
|
|
callback
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
2021-08-03 05:12:16 -04:00
|
|
|
archiveAllDoc(projectId, callback) {
|
2021-08-03 05:08:17 -04:00
|
|
|
request.post(
|
2020-02-16 09:03:09 -05:00
|
|
|
{
|
2021-08-03 05:12:16 -04:00
|
|
|
url: `http://localhost:${settings.internal.docstore.port}/project/${projectId}/archive`,
|
2020-02-16 09:03:09 -05:00
|
|
|
},
|
|
|
|
callback
|
|
|
|
)
|
|
|
|
},
|
2020-02-16 09:03:02 -05:00
|
|
|
|
2021-08-03 05:12:16 -04:00
|
|
|
archiveDocById(projectId, docId, callback) {
|
2021-08-03 05:08:17 -04:00
|
|
|
request.post(
|
2021-01-04 06:55:47 -05:00
|
|
|
{
|
2021-08-03 05:12:16 -04:00
|
|
|
url: `http://localhost:${settings.internal.docstore.port}/project/${projectId}/doc/${docId}/archive`,
|
2021-01-04 06:55:47 -05:00
|
|
|
},
|
|
|
|
callback
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
2021-08-03 05:12:16 -04:00
|
|
|
destroyAllDoc(projectId, callback) {
|
2021-08-03 05:08:17 -04:00
|
|
|
request.post(
|
2020-02-16 09:03:09 -05:00
|
|
|
{
|
2021-08-03 05:12:16 -04:00
|
|
|
url: `http://localhost:${settings.internal.docstore.port}/project/${projectId}/destroy`,
|
2020-02-16 09:03:09 -05:00
|
|
|
},
|
|
|
|
callback
|
|
|
|
)
|
|
|
|
},
|
2020-02-16 09:03:02 -05:00
|
|
|
|
2021-08-03 05:12:16 -04:00
|
|
|
getS3Doc(projectId, docId, callback) {
|
2020-07-23 14:43:33 -04:00
|
|
|
getStringFromPersistor(
|
|
|
|
Persistor,
|
|
|
|
settings.docstore.bucket,
|
2021-08-03 05:12:16 -04:00
|
|
|
`${projectId}/${docId}`
|
2020-07-23 14:43:33 -04:00
|
|
|
)
|
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
|
|
|
}
|