listScenes.js 1.7 KB

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