getImage.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 Get an image from a scene
  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 {Number|"min"|"max"|"median"} imageQuality The required quality of the image
  18. *
  19. * @apiExample Usage example
  20. * curl -i -L -X GET "http://diran.univ-littoral.fr/api/getImage?sceneName=bathroom&imageQuality=200"
  21. *
  22. * @apiSuccess {String} data Path to the image
  23. * @apiSuccessExample {json} Success response example
  24. * HTTP/1.1 200 OK /api/getImage?sceneName=bathroom&imageQuality=200
  25. * {
  26. * "data": "/api/images/bathroom/bathroom_00200.png"
  27. * }
  28. *
  29. * @apiError (Error 4xx) 400_[1] Missing parameter(s)
  30. * @apiErrorExample {json} Missing parameter
  31. * HTTP/1.1 400 Bad Request
  32. * {
  33. * "message": "Missing parameter(s). Required parameters : sceneName, imageQuality."
  34. * }
  35. *
  36. * @apiError (Error 4xx) 400_[2] Invalid query parameter
  37. * @apiErrorExample {json} Invalid query parameter(s)
  38. * HTTP/1.1 400 Bad Request
  39. * {
  40. * "message": "Invalid query parameter(s).",
  41. * "data": [
  42. * "The requested scene name \".//../\" is not valid.",
  43. * "The specified quality is not an integer."
  44. * ]
  45. * }
  46. *
  47. * @apiError (Error 4xx) 404_[1] Quality not found
  48. * @apiErrorExample {json} Quality not found
  49. * HTTP/1.1 404 Not Found
  50. * {
  51. * "message": "The requested quality (9999) was not found for the requested scene (bathroom)."
  52. * }
  53. *
  54. * @apiError (Error 5xx) 500_[1] Can't access the `IMAGES_PATH` directory
  55. * @apiErrorExample {json} Images directory not accessible
  56. * HTTP/1.1 500 Internal Server Error
  57. * {
  58. * "message": "Can't access the \"images\" directory. Check it exists and you have read permission on it"
  59. * }
  60. *
  61. * @apiError (Error 5xx) 500_[2] Failed to parse a file's name
  62. * @apiErrorExample {json} Failed to parse a file's name
  63. * HTTP/1.1 500 Internal Server Error
  64. * {
  65. * "message": "Failed to parse file names in the \"bathroom\"'s scene directory.",
  66. * "data": [
  67. * "The file name does not match convention (scene_000150.ext - /^(.*)?_([0-9]{2,})\\.(.*)$/) : \"bathroom_adz00020.png\".",
  68. * "The file name does not match convention (scene_000150.ext - /^(.*)?_([0-9]{2,})\\.(.*)$/) : \"bathroom_adz00020.png\"."
  69. * ]
  70. * }
  71. *
  72. */
  73. /**
  74. * @typedef {Object} Image
  75. * @property {string} link the link (URL) to an image on the app
  76. * @property {string} path the path to the image in the file system
  77. * @property {string} fileName the name of the image
  78. * @property {string} sceneName the scene of the image
  79. * @property {number} quality the quality of the image
  80. * @property {string} ext the extension of the image
  81. */
  82. /**
  83. * Get the link and path to an image
  84. * @param {string} sceneName the scene to get the image from
  85. * @param {number|"min"|"max"|"median"} quality the requested quality
  86. * @returns {Promise<Image>} the link and path to the image
  87. */
  88. export const getImage = async (sceneName, quality) => {
  89. const sceneData = await getSceneFilesData(sceneName)
  90. let imageData = null
  91. // Search an image with the requested quality in the scene
  92. if (quality === 'min') {
  93. const toFind = Math.min(...sceneData.map(x => x.quality))
  94. imageData = sceneData.find(x => x.quality === toFind)
  95. }
  96. else if (quality === 'max') {
  97. const toFind = Math.max(...sceneData.map(x => x.quality))
  98. imageData = sceneData.find(x => x.quality === toFind)
  99. }
  100. else if (quality === 'median')
  101. imageData = sceneData.length > 0 ? sceneData[Math.ceil(sceneData.length / 2) - 1] : null
  102. else
  103. imageData = sceneData.find(x => quality === x.quality)
  104. if (imageData)
  105. return {
  106. link: `${imageServedUrl}/${sceneName}/${imageData.fileName}`,
  107. path: path.resolve(imagesPath, sceneName, imageData.fileName),
  108. fileName: imageData.fileName,
  109. sceneName: imageData.sceneName,
  110. quality: imageData.quality,
  111. ext: imageData.ext
  112. }
  113. // Image not found
  114. throw boom.notFound(`The requested quality "${quality}" was not found for the requested scene "${sceneName}".`)
  115. }
  116. router.get('/', asyncMiddleware(async (req, res) => {
  117. // Check the request contains all the required parameters
  118. checkRequiredParameters(['sceneName', 'imageQuality'], req.query)
  119. const { sceneName, imageQuality } = req.query
  120. let errorList = []
  121. // Check the scene name is valid
  122. try {
  123. checkSceneName(sceneName)
  124. }
  125. catch (err) {
  126. errorList.push(err.message)
  127. }
  128. // Check `imageQuality` is an integer or `min`, `max` or `median`
  129. const qualityInt = parseInt(imageQuality, 10)
  130. let quality = null
  131. if (['min', 'median', 'max'].some(x => x === imageQuality))
  132. quality = imageQuality
  133. else if (!isNaN(qualityInt))
  134. quality = qualityInt
  135. else
  136. errorList.push('The specified quality is not an integer or "min", "max" or "median".')
  137. // Check there is no errors with parameters
  138. if (errorList.length > 0)
  139. throw boom.badRequest('Invalid query parameter(s).', errorList)
  140. const { link } = await getImage(sceneName, quality)
  141. res.json({ data: link })
  142. }))
  143. export default router