2020-01-10 00:13:32 +00:00
|
|
|
const fs = require('fs')
|
2019-12-16 10:42:31 +00:00
|
|
|
const path = require('path')
|
2021-07-12 16:47:19 +00:00
|
|
|
const Settings = require('@overleaf/settings')
|
2023-06-01 11:38:45 +00:00
|
|
|
const { WritableBuffer } = require('@overleaf/stream-utils')
|
2019-12-19 15:36:48 +00:00
|
|
|
const { promisify } = require('util')
|
|
|
|
const Stream = require('stream')
|
2017-03-08 14:59:34 +00:00
|
|
|
|
2019-12-19 15:36:48 +00:00
|
|
|
const pipeline = promisify(Stream.pipeline)
|
2020-01-10 00:13:32 +00:00
|
|
|
const fsCopy = promisify(fs.copyFile)
|
2019-12-19 15:36:48 +00: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
|
|
|
|
|
2023-06-01 11:38:45 +00:00
|
|
|
const buffer = new WritableBuffer({ initialSize: 100 })
|
2019-12-19 15:36:48 +00:00
|
|
|
|
|
|
|
const sourceStream = await FileHandler.getFile(bucket, key, {})
|
|
|
|
try {
|
|
|
|
await pipeline(sourceStream, buffer)
|
|
|
|
} catch (err) {
|
2020-07-07 12:49:54 +00:00
|
|
|
throw new HealthCheckError('failed to get health-check file', {}, err)
|
2019-12-19 15:36:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!buffer.size()) {
|
|
|
|
throw new HealthCheckError('no bytes written to download stream')
|
2019-12-16 10:42:31 +00:00
|
|
|
}
|
|
|
|
}
|
2016-12-14 17:14:15 +00:00
|
|
|
|
2019-12-19 15:36:48 +00: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 10:42:31 +00:00
|
|
|
}
|
|
|
|
}
|
2016-12-14 17:14:15 +00:00
|
|
|
|
2019-12-16 10:24:35 +00:00
|
|
|
module.exports = {
|
2020-01-07 21:19:26 +00:00
|
|
|
check(req, res, next) {
|
2019-12-19 15:36:48 +00:00
|
|
|
Promise.all([checkCanGetFiles(), checkFileConvert()])
|
2020-01-02 15:51:09 +00:00
|
|
|
.then(() => res.sendStatus(200))
|
2021-07-13 11:04:46 +00:00
|
|
|
.catch(err => {
|
2020-01-07 21:19:26 +00:00
|
|
|
next(err)
|
2019-12-19 15:36:48 +00:00
|
|
|
})
|
2021-07-13 11:04:46 +00:00
|
|
|
},
|
2019-12-16 10:42:31 +00:00
|
|
|
}
|