2021-08-16 12:09:52 +00:00
|
|
|
const request = require('request')
|
2021-12-14 13:00:35 +00:00
|
|
|
const logger = require('@overleaf/logger')
|
2021-08-16 12:09:52 +00:00
|
|
|
const Settings = require('@overleaf/settings')
|
2021-12-03 13:15:37 +00:00
|
|
|
const Errors = require('./Errors')
|
2021-08-16 12:09:52 +00:00
|
|
|
|
|
|
|
function peekDocument(projectId, docId, callback) {
|
|
|
|
const url = `${Settings.apis.docstore.url}/project/${projectId}/doc/${docId}/peek`
|
2022-05-16 12:38:18 +00:00
|
|
|
logger.debug(
|
2021-08-16 12:09:52 +00:00
|
|
|
{ project_id: projectId, doc_id: docId },
|
|
|
|
'getting doc from docstore'
|
|
|
|
)
|
|
|
|
request.get(url, function (error, res, body) {
|
|
|
|
if (error != null) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
if (res.statusCode >= 200 && res.statusCode < 300) {
|
|
|
|
try {
|
|
|
|
body = JSON.parse(body)
|
2021-12-03 13:15:37 +00:00
|
|
|
} catch (error) {
|
2021-08-16 12:09:52 +00:00
|
|
|
return callback(error)
|
|
|
|
}
|
2022-05-16 12:38:18 +00:00
|
|
|
logger.debug(
|
2021-08-16 12:09:52 +00:00
|
|
|
{ project_id: projectId, doc_id: docId, version: body.version },
|
|
|
|
'got doc from docstore'
|
|
|
|
)
|
|
|
|
return callback(null, body.lines.join('\n'), body.version)
|
2021-12-03 13:15:37 +00:00
|
|
|
} else if (res.statusCode === 404) {
|
|
|
|
return callback(
|
|
|
|
new Errors.NotFoundError('doc not found', { projectId, docId })
|
|
|
|
)
|
2021-08-16 12:09:52 +00:00
|
|
|
} else {
|
|
|
|
return callback(
|
|
|
|
new Error(
|
|
|
|
`docstore returned a non-success status code: ${res.statusCode}`
|
|
|
|
)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
promises: {
|
|
|
|
peekDocument: (projectId, docId) => {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
peekDocument(projectId, docId, (err, content, version) => {
|
|
|
|
if (err) {
|
|
|
|
reject(err)
|
|
|
|
} else {
|
|
|
|
resolve([content, version])
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|