listSceneQualities.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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, checkRequiredParameters } from '../functions'
  7. const router = express.Router()
  8. // Route which returns a list of all available qualities for a scene
  9. /**
  10. * Get a list of all qualities available for a given scene
  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. // Return the list of all files in the images directory
  19. return fs.readdir(imagesPath)
  20. }
  21. catch (err) {
  22. // The images directory does not exist or is not accessible
  23. throw boom.badRequest(`Can't access the "${imagesPath}" directory. Check it exists and you have read permission on it.`)
  24. }
  25. }
  26. router.get('/', async (req, res) => {
  27. // Check the request contains all the required parameters
  28. checkRequiredParameters(['sceneName', 'imageQuality'], req.query)
  29. const dirContent = await fs.readdir(imagesPath)
  30. res.json({ msg: 'Not ready yet' })
  31. })
  32. router.get('/', asyncMiddleware(async (req, res) => res.json(await getAvailableScenes())))
  33. export default router