listScenes.js 929 B

1234567891011121314151617181920212223242526272829303132
  1. 'use strict'
  2. import express from 'express'
  3. import _fs, { promises as fs } from 'fs'
  4. import boom from 'boom'
  5. import { imagesPath } from '../config'
  6. import { asyncMiddleware } from '../functions'
  7. const router = express.Router()
  8. // Route which returns a list of all available scenes in the `imagesPath` directory
  9. /**
  10. * Get a list of all available scenes
  11. *
  12. * @returns {string[]} the available scenes
  13. */
  14. const getAvailableScenes = async () => {
  15. try {
  16. // Check if the directory which contains images exists
  17. await fs.access(imagesPath, _fs.constants.R_OK)
  18. }
  19. catch (err) {
  20. // The images directory does not exist or is not accessible
  21. throw boom.badRequest(`Can't access the "${imagesPath}" directory. Check it exists and you have read permission on it.`)
  22. }
  23. return fs.readdir(imagesPath)
  24. }
  25. router.get('/', asyncMiddleware(async (req, res) => res.json(await getAvailableScenes())))
  26. export default router