Parcourir la source

listSceneQualities working

rigwild il y a 5 ans
Parent
commit
fb10d89407

+ 1 - 1
server/index.js

@@ -31,7 +31,7 @@ else {
 }
 
 // Serve images. "serve-static" is used because it caches images ("express.static" doesn't)
-app.use(serveStatic(imagesPath))
+app.use(apiPrefix + '/images', serveStatic(imagesPath))
 
 // Load all the API routes in the server
 app.use(apiPrefix, routes)

+ 3 - 3
server/routes/getImage.js

@@ -4,18 +4,18 @@ import express from 'express'
 import { promises } from 'fs'
 
 import { imagesPath } from '../../config'
-import { checkRequiredParameters } from '../functions'
+import { asyncMiddleware, checkRequiredParameters } from '../functions'
 
 const fs = promises
 
 const router = express.Router()
 
-router.get('/', async (req, res) => {
+router.get('/', asyncMiddleware(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' })
-})
+}))
 
 export default router

+ 2 - 0
server/routes/index.js

@@ -2,11 +2,13 @@
 
 import express from 'express'
 import listScenes from './listScenes'
+import listSceneQualities from './listSceneQualities'
 import getImage from './getImage'
 
 const router = express.Router()
 
 router.use('/listScenes', listScenes)
+router.use('/listSceneQualities', listSceneQualities)
 router.use('/getImage', getImage)
 
 export default router

+ 47 - 27
server/routes/listSceneQualities.js

@@ -1,7 +1,8 @@
 'use strict'
 
 import express from 'express'
-import _fs, { promises as fs } from 'fs'
+import { promises as fs } from 'fs'
+import path from 'path'
 import boom from 'boom'
 import { imagesPath } from '../../config'
 import { asyncMiddleware, checkRequiredParameters } from '../functions'
@@ -9,34 +10,53 @@ 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) => {
+router.get('/', asyncMiddleware(async (req, res) => {
   // Check the request contains all the required parameters
-  checkRequiredParameters(['sceneName', 'imageQuality'], req.query)
+  checkRequiredParameters(['sceneName'], req.query)
+
+  const sceneName = req.query.sceneName
+
+  // Check the scene name is valid (Not trying to go back in the file system tree by using `/../`)
+  if (!/^(?!.*\.\.).*$/.test(sceneName))
+    throw boom.conflict(`The requested scene name "${sceneName}" is not valid.`)
+
+  // Path to the scene directory
+  const scenePath = path.resolve(imagesPath, sceneName)
+
+  // Get the list of all images in the selected scene
+  const images = await fs.readdir(scenePath)
+    .catch(() => {
+      // The images directory does not exist or is not accessible
+      throw boom.badRequest(`Can't access the "${scenePath}" directory. Check it exists and you have read permission on it.`)
+    })
+
+  // List of blacklisted words from image names
+  const blackList = ['config', 'seuilExpe']
+
+  // A list of all fails parsing file names
+  let failList = []
+  // Parse file name to get qualities
+  const qualities = images.reduce((acc, image) => {
+    // Go to next file if its name contains a blacklisted word
+    if (!blackList.every(x => !image.includes(x))) return
+
+    const sp = image.split('_')
+    const end = sp[sp.length - 1] // 000650.png
+    const qualityString = end.replace(/\..*/g, '') // 000650
+    const qualityInteger = parseInt(qualityString, 10) // 650
+
+    // Check the quality string
+    if (isNaN(qualityInteger))
+      return failList.push(`Failed to parse file name : ${image}`)
+
+    acc.push(qualityInteger)
+  }, [])
 
-  const dirContent = await fs.readdir(imagesPath)
-  res.json({ msg: 'Not ready yet' })
-})
+  // Check if the parse fail list is empty
+  if (failList.length > 0)
+    throw boom.internal(`Fail while parsing file names in the "${sceneName}"'s scene directory.`, failList)
 
-router.get('/', asyncMiddleware(async (req, res) => res.json(await getAvailableScenes())))
+  res.json(qualities)
+}))
 
 export default router

+ 1 - 1
server/routes/listScenes.js

@@ -1,7 +1,7 @@
 'use strict'
 
 import express from 'express'
-import _fs, { promises as fs } from 'fs'
+import { promises as fs } from 'fs'
 import boom from 'boom'
 import { imagesPath } from '../../config'
 import { asyncMiddleware } from '../functions'