Browse Source

Hotfix config

rigwild 1 year ago
parent
commit
29d39ddf57
4 changed files with 65 additions and 25 deletions
  1. 15 0
      api/functions.js
  2. 3 11
      api/routes/getImage.js
  3. 42 0
      api/routes/listSceneQualities.js
  4. 5 14
      api/routes/listScenes.js

+ 15 - 0
api/functions.js

@@ -1,5 +1,7 @@
 'use strict'
 
+import boom from 'boom'
+
 /**
  * Call the error handler if a middleware function throw an error
  *
@@ -24,3 +26,16 @@ export const errorHandler = (err, req, res, next) => {
   console.error(`Error ${payload.statusCode} - ${payload.error}\n${payload.message}\n`)
   return res.status(payload.statusCode).json(payload)
 }
+
+
+/**
+ * Check the request contains all the required parameters
+ *
+ * @param {string[]} requiredParameters list of all required parameters
+ * @param {object} parameters parameters provided in the request (req.query)
+ * @returns {void}
+ */
+export const checkRequiredParameters = (requiredParameters, parameters) => {
+  if (!requiredParameters.every(aRequiredParameter => Object.keys(parameters).includes(aRequiredParameter)))
+    throw boom.badRequest(`Missing parameter(s). Required parameters : ${requiredParameters.join(', ')}.`)
+}

+ 3 - 11
api/routes/getImage.js

@@ -4,25 +4,17 @@ import express from 'express'
 import { promises } from 'fs'
 
 import { imagesPath } from '../../config'
+import { checkRequiredParameters } from '../functions'
 
 const fs = promises
 
 const router = express.Router()
 
 router.get('/', async (req, res) => {
-  const requiredParameters = ['sceneName', 'imageQuality']
-  const params = req.query
-
-  // Check if all required parameters were passed
-  if (!requiredParameters.every(parameter => Object.keys(params).includes(parameter))) {
-    // Some parameters are missing
-    res.statusCode = 400
-    res.json({ error: `Missing parameter(s). Required parameters : ${requiredParameters.join(', ')}.` })
-    return
-  }
+  // Check the request contains all the required parameters
+  checkRequiredParameters(['sceneName', 'imageQuality'], req.query)
 
   const dirContent = await fs.readdir(imagesPath)
-  res.json(req.query)
   res.json({ msg: 'Not ready yet' })
 })
 

+ 42 - 0
api/routes/listSceneQualities.js

@@ -0,0 +1,42 @@
+'use strict'
+
+import express from 'express'
+import _fs, { promises as fs } from 'fs'
+import boom from 'boom'
+import { imagesPath } from '../../config'
+import { asyncMiddleware, checkRequiredParameters } from '../functions'
+
+const router = express.Router()
+
+// Route which returns a list of all available qualities for a scene
+
+/**
+ * Get a list of all qualities available for a given scene
+ *
+ * @returns {string[]} the available scenes
+ */
+const getAvailableScenes = async () => {
+  try {
+    // Check if the directory which contains images exists
+    await fs.access(imagesPath, _fs.constants.R_OK)
+
+    // Return the list of all files in the images directory
+    return fs.readdir(imagesPath)
+  }
+  catch (err) {
+    // The images directory does not exist or is not accessible
+    throw boom.badRequest(`Can't access the "${imagesPath}" directory. Check it exists and you have read permission on it.`)
+  }
+}
+
+router.get('/', async (req, res) => {
+  // Check the request contains all the required parameters
+  checkRequiredParameters(['sceneName', 'imageQuality'], req.query)
+
+  const dirContent = await fs.readdir(imagesPath)
+  res.json({ msg: 'Not ready yet' })
+})
+
+router.get('/', asyncMiddleware(async (req, res) => res.json(await getAvailableScenes())))
+
+export default router

+ 5 - 14
api/routes/listScenes.js

@@ -3,30 +3,21 @@
 import express from 'express'
 import _fs, { promises as fs } from 'fs'
 import boom from 'boom'
-import { imagesPath } from '../config'
+import { imagesPath } from '../../config'
 import { asyncMiddleware } from '../functions'
 
 const router = express.Router()
 
 // Route which returns a list of all available scenes in the `imagesPath` directory
-
-/**
- * Get a list of all available scenes
- *
- * @returns {string[]} the available scenes
- */
-const getAvailableScenes = async () => {
+router.get('/', asyncMiddleware(async (req, res) => {
   try {
-    // Check if the directory which contains images exists
-    await fs.access(imagesPath, _fs.constants.R_OK)
+    // Return the list of all files in the images directory
+    res.json(await fs.readdir(imagesPath))
   }
   catch (err) {
     // The images directory does not exist or is not accessible
     throw boom.badRequest(`Can't access the "${imagesPath}" directory. Check it exists and you have read permission on it.`)
   }
-  return fs.readdir(imagesPath)
-}
-
-router.get('/', asyncMiddleware(async (req, res) => res.json(await getAvailableScenes())))
+}))
 
 export default router