2021-08-16 08:09:52 -04:00
|
|
|
const request = require('request')
|
2021-12-14 08:00:35 -05:00
|
|
|
const logger = require('@overleaf/logger')
|
2021-08-16 08:09:52 -04:00
|
|
|
const Settings = require('@overleaf/settings')
|
2021-12-03 08:15:37 -05:00
|
|
|
const Errors = require('./Errors')
|
2021-08-16 08:09:52 -04:00
|
|
|
|
|
|
|
function peekDocument(projectId, docId, callback) {
|
|
|
|
const url = `${Settings.apis.docstore.url}/project/${projectId}/doc/${docId}/peek`
|
2022-05-16 08:38:18 -04:00
|
|
|
logger.debug(
|
2021-08-16 08:09:52 -04: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 08:15:37 -05:00
|
|
|
} catch (error) {
|
2021-08-16 08:09:52 -04:00
|
|
|
return callback(error)
|
|
|
|
}
|
2022-05-16 08:38:18 -04:00
|
|
|
logger.debug(
|
2021-08-16 08:09:52 -04: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 08:15:37 -05:00
|
|
|
} else if (res.statusCode === 404) {
|
|
|
|
return callback(
|
|
|
|
new Errors.NotFoundError('doc not found', { projectId, docId })
|
|
|
|
)
|
2021-08-16 08:09:52 -04: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])
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|