getImage.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. 'use strict'
  2. import express from 'express'
  3. import path from 'path'
  4. import boom from '@hapi/boom'
  5. import { imagesPath, imageServedUrl } from '../../config'
  6. import { asyncMiddleware, checkSceneName, checkRequiredParameters, getSceneFilesData } from '../functions'
  7. const router = express.Router()
  8. /**
  9. * @api {get} /getImage?sceneName=:sceneName&imageQuality=:imageQuality&nearestQuality=:nearestQuality /getImage
  10. * @apiVersion 0.1.0
  11. * @apiName getImage
  12. * @apiGroup API
  13. *
  14. * @apiDescription Get an image from a scene with the required quality
  15. *
  16. * @apiParam {String} sceneName The selected scene
  17. * @apiParam {String="min","max","median", "any integer"} imageQuality The required quality of the image (can be an integer, `min`, `max` or `median`)
  18. * @apiParam {Boolean} [nearestQuality=false] if selected quality not availabie, select the nearest one
  19. *
  20. * @apiExample Usage example
  21. * curl -i -L -X GET "http://diran.univ-littoral.fr/api/getImage?sceneName=bathroom&imageQuality=200"
  22. *
  23. * @apiSuccess {Object} data Informations on the image
  24. * @apiSuccess {String} data.link Path to the image
  25. * @apiSuccess {String} data.fileName File name of the image
  26. * @apiSuccess {String} data.sceneName Scene name of the image
  27. * @apiSuccess {Number} data.quality Quality of the image
  28. * @apiSuccess {String} data.ext Extension of the image
  29. * @apiSuccessExample {json} Success response example
  30. * HTTP/1.1 200 OK /api/getImage?sceneName=bathroom&imageQuality=200
  31. * {
  32. * "data": {
  33. * "link": "/api/images/bathroom/bathroom_00200.png",
  34. * "fileName": "bathroom_00200.png",
  35. * "sceneName": "bathroom",
  36. * "quality": 200,
  37. * "ext": "png"
  38. * }
  39. * }
  40. *
  41. * @apiError (Error 4xx) 400_[1] Missing parameter(s)
  42. * @apiErrorExample {json} Missing parameter
  43. * HTTP/1.1 400 Bad Request
  44. * {
  45. * "message": "Missing parameter(s). Required parameters : sceneName, imageQuality."
  46. * }
  47. *
  48. * @apiError (Error 4xx) 400_[2] Invalid query parameter
  49. * @apiErrorExample {json} Invalid query parameter(s)
  50. * HTTP/1.1 400 Bad Request
  51. * {
  52. * "message": "Invalid query parameter(s).",
  53. * "data": [
  54. * "The requested scene name \".//../\" is not valid.",
  55. * "The specified quality is not an integer.",
  56. * "Impossible to use \"min\", \"max\" or \"median\" with \"nearestQuality\" on."
  57. * ]
  58. * }
  59. *
  60. * @apiError (Error 4xx) 404_[1] Quality not found
  61. * @apiErrorExample {json} Quality not found
  62. * HTTP/1.1 404 Not Found
  63. * {
  64. * "message": "The requested quality (9999) was not found for the requested scene (bathroom)."
  65. * }
  66. *
  67. * @apiError (Error 5xx) 500_[1] Can't access the `IMAGES_PATH` directory
  68. * @apiErrorExample {json} Images directory not accessible
  69. * HTTP/1.1 500 Internal Server Error
  70. * {
  71. * "message": "Can't access the \"images\" directory. Check it exists and you have read permission on it"
  72. * }
  73. *
  74. * @apiError (Error 5xx) 500_[2] Failed to parse a file's name
  75. * @apiErrorExample {json} Failed to parse a file's name
  76. * HTTP/1.1 500 Internal Server Error
  77. * {
  78. * "message": "Failed to parse file names in the \"bathroom\"'s scene directory.",
  79. * "data": [
  80. * "The file name does not match convention (scene_000150.ext - /^(.*)?_([0-9]{2,})\\.(.*)$/) : \"bathroom_adz00020.png\".",
  81. * "The file name does not match convention (scene_000150.ext - /^(.*)?_([0-9]{2,})\\.(.*)$/) : \"bathroom_adz00020.png\"."
  82. * ]
  83. * }
  84. *
  85. */
  86. /**
  87. * @typedef {Object} Image
  88. * @property {string} link the link (URL) to an image on the app
  89. * @property {string} path the path to the image in the file system
  90. * @property {string} fileName the name of the image
  91. * @property {string} sceneName the scene of the image
  92. * @property {number} quality the quality of the image
  93. * @property {string} ext the extension of the image
  94. */
  95. /**
  96. * Get the link and path to an image
  97. * @param {string} sceneName the scene to get the image from
  98. * @param {number|"min"|"max"|"median"} quality the requested quality
  99. * @param {boolean} [nearestQuality=false] if selected quality not availabie, select the nearest one
  100. * @returns {Promise<Image>} the link and path to the image
  101. */
  102. export const getImage = async (sceneName, quality, nearestQuality = false) => {
  103. const throwErrIfTrue = x => {
  104. if (x) throw boom.badRequest('Impossible to use "min", "max" or "median" with "nearestQuality" on.')
  105. }
  106. const sceneData = await getSceneFilesData(sceneName)
  107. let imageData = null
  108. // Search an image with the requested quality in the scene
  109. if (quality === 'min') {
  110. throwErrIfTrue(nearestQuality)
  111. const toFind = Math.min(...sceneData.map(x => x.quality))
  112. imageData = sceneData.find(x => x.quality === toFind)
  113. }
  114. else if (quality === 'max') {
  115. throwErrIfTrue(nearestQuality)
  116. const toFind = Math.max(...sceneData.map(x => x.quality))
  117. imageData = sceneData.find(x => x.quality === toFind)
  118. }
  119. else if (quality === 'median') {
  120. throwErrIfTrue(nearestQuality)
  121. imageData = sceneData.length > 0 ? sceneData[Math.ceil(sceneData.length / 2) - 1] : null
  122. }
  123. else {
  124. if (nearestQuality && sceneData.length > 0 && !isNaN(parseInt(quality, 10))) {
  125. let minGap = Number.MAX_SAFE_INTEGER
  126. let minGapImageData = null
  127. for (const x of sceneData) {
  128. const tempGap = Math.abs(x.quality - quality)
  129. if (tempGap < minGap) {
  130. minGap = tempGap
  131. minGapImageData = x
  132. }
  133. }
  134. imageData = minGapImageData
  135. }
  136. else imageData = sceneData.find(x => quality === x.quality)
  137. }
  138. if (imageData)
  139. return {
  140. link: `${imageServedUrl}/${sceneName}/${imageData.fileName}`,
  141. path: path.resolve(imagesPath, sceneName, imageData.fileName),
  142. fileName: imageData.fileName,
  143. sceneName: imageData.sceneName,
  144. quality: imageData.quality,
  145. ext: imageData.ext
  146. }
  147. // Image not found
  148. throw boom.notFound(`The requested quality "${quality}" was not found for the requested scene "${sceneName}".`)
  149. }
  150. router.get('/', asyncMiddleware(async (req, res) => {
  151. // Check the request contains all the required parameters
  152. checkRequiredParameters(['sceneName', 'imageQuality'], req.query)
  153. const { sceneName, imageQuality } = req.query
  154. const nearestQuality = req.query.nearestQuality === 'true'
  155. let errorList = []
  156. // Check the scene name is valid
  157. try {
  158. checkSceneName(sceneName)
  159. }
  160. catch (err) {
  161. errorList.push(err.message)
  162. }
  163. // Check `imageQuality` is an integer or `min`, `max` or `median`
  164. const qualityInt = parseInt(imageQuality, 10)
  165. let quality = null
  166. if (['min', 'median', 'max'].some(x => x === imageQuality)) {
  167. if (nearestQuality)
  168. errorList.push('Impossible to use "min", "max" or "median" with "nearestQuality" on.')
  169. else quality = imageQuality
  170. }
  171. else if (!isNaN(qualityInt))
  172. quality = qualityInt
  173. else
  174. errorList.push('The specified quality is not an integer or "min", "max" or "median".')
  175. // Check there is no errors with parameters
  176. if (errorList.length > 0)
  177. throw boom.badRequest('Invalid query parameter(s).', errorList)
  178. const data = await getImage(sceneName, quality, nearestQuality)
  179. data.path = undefined
  180. res.json({ data })
  181. }))
  182. export default router