listScenes.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. 'use strict'
  2. import express from 'express'
  3. import { promises as fs } from 'fs'
  4. import path from 'path'
  5. import boom from '@hapi/boom'
  6. import { asyncMiddleware } from '../functions'
  7. import { imagesPath } from '../../config'
  8. const router = express.Router()
  9. /**
  10. * @api {get} /listScenes /listScenes
  11. * @apiVersion 0.1.0
  12. * @apiName listScenes
  13. * @apiGroup API
  14. *
  15. * @apiDescription List all scenes availables in your `IMAGES_PATH` directory
  16. * @apiSampleRequest /listScenes
  17. *
  18. * @apiExample Usage example
  19. * curl -i -L -X GET "https://diran.univ-littoral.fr/api/listScenes"
  20. *
  21. * @apiSuccess {String[]} data List of available scenes
  22. * @apiSuccessExample {json} Success response example
  23. * HTTP/1.1 200 OK /api/listScenes
  24. * {
  25. * "data": [
  26. * "bathroom",
  27. * "contemporary",
  28. * ]
  29. * }
  30. *
  31. * @apiError (Error 5xx) 500_[1] Can't access the `IMAGES_PATH` directory
  32. * @apiErrorExample {json} Images directory not accessible
  33. * HTTP/1.1 500 Internal Server Error
  34. * {
  35. * "message": "Can't access the \"images\" directory. Check it exists and you have read permission on it"
  36. * }
  37. */
  38. /**
  39. * Get the list of all files in the images directory
  40. *
  41. * @returns {string[]} the list of files
  42. * @throws the directory does not exist or is not accessible
  43. */
  44. export const getSceneList = () => fs.readdir(imagesPath).catch(() => {
  45. throw boom.internal(`Can't access the "${path.basename(imagesPath)}" directory. Check it exists and you have read permission on it.`)
  46. })
  47. // Route which returns a list of all available scenes in the `imagesPath` directory
  48. router.get('/', asyncMiddleware(async (req, res) => res.json({ data: await getSceneList() })))
  49. export default router