listScenes.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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 "http://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 = () => {
  45. return fs.readdir(imagesPath).catch(() => {
  46. throw boom.internal(`Can't access the "${path.basename(imagesPath)}" directory. Check it exists and you have read permission on it.`)
  47. })
  48. }
  49. // Route which returns a list of all available scenes in the `imagesPath` directory
  50. router.get('/', asyncMiddleware(async (req, res) => res.json({ data: await getSceneList() })))
  51. export default router