HealthCheckController.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. const fs = require('node:fs')
  2. const path = require('node:path')
  3. const Settings = require('@overleaf/settings')
  4. const { WritableBuffer } = require('@overleaf/stream-utils')
  5. const { promisify } = require('node:util')
  6. const Stream = require('node:stream')
  7. const pipeline = promisify(Stream.pipeline)
  8. const fsCopy = promisify(fs.copyFile)
  9. const fsUnlink = promisify(fs.unlink)
  10. const { HealthCheckError } = require('./Errors')
  11. const FileConverter = require('./FileConverter').promises
  12. const FileHandler = require('./FileHandler').promises
  13. async function checkCanGetFiles() {
  14. if (!Settings.health_check) {
  15. return
  16. }
  17. const projectId = Settings.health_check.project_id
  18. const fileId = Settings.health_check.file_id
  19. const key = `${projectId}/${fileId}`
  20. const bucket = Settings.filestore.stores.user_files
  21. const buffer = new WritableBuffer({ initialSize: 100 })
  22. const sourceStream = await FileHandler.getFile(bucket, key, {})
  23. try {
  24. await pipeline(sourceStream, buffer)
  25. } catch (err) {
  26. throw new HealthCheckError('failed to get health-check file', {}, err)
  27. }
  28. if (!buffer.size()) {
  29. throw new HealthCheckError('no bytes written to download stream')
  30. }
  31. }
  32. async function checkFileConvert() {
  33. if (!Settings.enableConversions) {
  34. return
  35. }
  36. const imgPath = path.join(Settings.path.uploadFolder, '/tiny.pdf')
  37. let resultPath
  38. try {
  39. await fsCopy('./tiny.pdf', imgPath)
  40. resultPath = await FileConverter.thumbnail(imgPath)
  41. } finally {
  42. if (resultPath) {
  43. await fsUnlink(resultPath)
  44. }
  45. await fsUnlink(imgPath)
  46. }
  47. }
  48. module.exports = {
  49. check(req, res, next) {
  50. Promise.all([checkCanGetFiles(), checkFileConvert()])
  51. .then(() => res.sendStatus(200))
  52. .catch(err => {
  53. next(err)
  54. })
  55. },
  56. }