2020-01-09 19:13:32 -05:00
|
|
|
const fs = require('fs')
|
2019-12-16 05:42:31 -05:00
|
|
|
const path = require('path')
|
2021-07-12 12:47:19 -04:00
|
|
|
const Settings = require('@overleaf/settings')
|
2019-12-16 05:42:31 -05:00
|
|
|
const streamBuffers = require('stream-buffers')
|
2019-12-19 10:36:48 -05:00
|
|
|
const { promisify } = require('util')
|
|
|
|
const Stream = require('stream')
|
2017-03-08 09:59:34 -05:00
|
|
|
|
2019-12-19 10:36:48 -05:00
|
|
|
const pipeline = promisify(Stream.pipeline)
|
2020-01-09 19:13:32 -05:00
|
|
|
const fsCopy = promisify(fs.copyFile)
|
2019-12-19 10:36:48 -05:00
|
|
|
const fsUnlink = promisify(fs.unlink)
|
|
|
|
|
|
|
|
const { HealthCheckError } = require('./Errors')
|
|
|
|
const FileConverter = require('./FileConverter').promises
|
|
|
|
const FileHandler = require('./FileHandler').promises
|
|
|
|
|
|
|
|
async function checkCanGetFiles() {
|
|
|
|
if (!Settings.health_check) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
const projectId = Settings.health_check.project_id
|
|
|
|
const fileId = Settings.health_check.file_id
|
|
|
|
const key = `${projectId}/${fileId}`
|
|
|
|
const bucket = Settings.filestore.stores.user_files
|
|
|
|
|
|
|
|
const buffer = new streamBuffers.WritableStreamBuffer({
|
2021-07-13 07:04:46 -04:00
|
|
|
initialSize: 100,
|
2019-12-16 05:42:31 -05:00
|
|
|
})
|
2019-12-19 10:36:48 -05:00
|
|
|
|
|
|
|
const sourceStream = await FileHandler.getFile(bucket, key, {})
|
|
|
|
try {
|
|
|
|
await pipeline(sourceStream, buffer)
|
|
|
|
} catch (err) {
|
2020-07-07 08:49:54 -04:00
|
|
|
throw new HealthCheckError('failed to get health-check file', {}, err)
|
2019-12-19 10:36:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!buffer.size()) {
|
|
|
|
throw new HealthCheckError('no bytes written to download stream')
|
2019-12-16 05:42:31 -05:00
|
|
|
}
|
|
|
|
}
|
2016-12-14 12:14:15 -05:00
|
|
|
|
2019-12-19 10:36:48 -05:00
|
|
|
async function checkFileConvert() {
|
|
|
|
if (!Settings.enableConversions) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
const imgPath = path.join(Settings.path.uploadFolder, '/tiny.pdf')
|
|
|
|
|
|
|
|
let resultPath
|
|
|
|
try {
|
|
|
|
await fsCopy('./tiny.pdf', imgPath)
|
|
|
|
resultPath = await FileConverter.thumbnail(imgPath)
|
|
|
|
} finally {
|
|
|
|
if (resultPath) {
|
|
|
|
await fsUnlink(resultPath)
|
|
|
|
}
|
|
|
|
await fsUnlink(imgPath)
|
2019-12-16 05:42:31 -05:00
|
|
|
}
|
|
|
|
}
|
2016-12-14 12:14:15 -05:00
|
|
|
|
2019-12-16 05:24:35 -05:00
|
|
|
module.exports = {
|
2020-01-07 16:19:26 -05:00
|
|
|
check(req, res, next) {
|
2019-12-19 10:36:48 -05:00
|
|
|
Promise.all([checkCanGetFiles(), checkFileConvert()])
|
2020-01-02 10:51:09 -05:00
|
|
|
.then(() => res.sendStatus(200))
|
2021-07-13 07:04:46 -04:00
|
|
|
.catch(err => {
|
2020-01-07 16:19:26 -05:00
|
|
|
next(err)
|
2019-12-19 10:36:48 -05:00
|
|
|
})
|
2021-07-13 07:04:46 -04:00
|
|
|
},
|
2019-12-16 05:42:31 -05:00
|
|
|
}
|