listScenes.js 823 B

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