getImageExtracts.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. 'use strict'
  2. import express from 'express'
  3. import sharp from 'sharp'
  4. import { constants as fsConstants, promises as fs } from 'fs'
  5. import path from 'path'
  6. import boom from 'boom'
  7. import { asyncMiddleware, checkSceneName, checkRequiredParameters } from '../functions'
  8. import { imageServedUrl, imagesPath, extractsDirName } from '../../config'
  9. import { getImage } from './getImage'
  10. const router = express.Router()
  11. /**
  12. * @api {get} /getImageExtracts?sceneName=:sceneName&imageQuality=:imageQuality&horizontalExtractCount=:horizontalExtractCount&verticalExtractCount=:verticalExtractCount Get image extracts
  13. * @apiVersion 0.1.0
  14. * @apiName GetImageExtracts
  15. * @apiGroup API
  16. *
  17. * @apiDescription Get an image from a scene with the required quality and cut it with the requested configuration
  18. *
  19. * @apiParam {String} sceneName The selected scene
  20. * @apiParam {Number} imageQuality The required quality of the image
  21. * @apiParam {Number} horizontalExtractCount The amount of extracts for the horizontal axis
  22. * @apiParam {Number} verticalExtractCount The amount of extracts for the vertical axis
  23. *
  24. * @apiHeader (Response Headers) {String} Content-Type application/json; charset=utf-8
  25. *
  26. * @apiExample Usage example
  27. * curl -i -L -X GET "http://localhost:5000/api/getImageExtracts?sceneName=bathroom&imageQuality=200&horizontalExtractCount=1&verticalExtractCount=2"
  28. *
  29. * @apiSuccess {String[]} data Path to the extracted images
  30. * @apiSuccessExample {json} Success response example
  31. * HTTP/1.1 200 OK /api/getImageExtracts?sceneName=bathroom&imageQuality=200&horizontalExtractCount=1&verticalExtractCount=2
  32. * {
  33. * "data": [
  34. * "/api/images/bathroom/extracts/x1_y2/zone00001/bathroom_zone00001_200.png",
  35. * "/api/images/bathroom/extracts/x1_y2/zone00002/bathroom_zone00002_200.png"
  36. * ]
  37. * }
  38. *
  39. * @apiError (Error 4xx) 400 Missing parameter(s)
  40. * @apiErrorExample {json} Missing parameter
  41. * HTTP/1.1 400 Bad Request
  42. * {
  43. * "message": "Missing parameter(s). Required parameters : sceneName, imageQuality, horizontalExtractCount, verticalExtractCount."
  44. * }
  45. *
  46. * @apiError (Error 4xx) 400 Invalid query parameter
  47. * @apiErrorExample {json} Invalid query parameter(s)
  48. * HTTP/1.1 400 Bad Request
  49. * {
  50. * "message": "Invalid query parameter(s).",
  51. * "data": [
  52. * "The requested scene name \".//../\" is not valid.",
  53. * "The specified quality is not an integer.",
  54. * "The specified number of extract for the horizontal axis is not an integer.",
  55. * "The specified number of extract for the vertical axis is not an integer."
  56. * ]
  57. * }
  58. *
  59. * @apiError (Error 4xx) 400 Invalid configuration
  60. * @apiErrorExample {json} Invalid configuration
  61. * HTTP/1.1 400 Bad Request
  62. * {
  63. * "message": "Invalid query parameter(s).",
  64. * "data": [
  65. * "Incompatible number of horizontal extracts (width % numberOfExtracts != 0).",
  66. * "Incompatible number of vertical extracts (height % numberOfExtracts != 0)."
  67. * ]
  68. * }
  69. *
  70. * @apiError (Error 4xx) 404 Quality not found
  71. * @apiErrorExample {json} Quality not found
  72. * HTTP/1.1 404 Not Found
  73. * {
  74. * "message": "The requested quality (9999) was not found for the requested scene (bathroom)."
  75. * }
  76. *
  77. * @apiError (Error 5xx) 500 Can't access the `IMAGES_PATH` directory
  78. * @apiErrorExample {json} Images directory not accessible
  79. * HTTP/1.1 500 Internal Server Error
  80. * {
  81. * "message": "Can't access the \"images\" directory. Check it exists and you have read permission on it"
  82. * }
  83. *
  84. * @apiError (Error 5xx) 500 Failed to parse a file's name
  85. * @apiErrorExample {json} Failed to parse a file's name
  86. * HTTP/1.1 500 Internal Server Error
  87. * {
  88. * "message": "Failed to parse file names in the \"bathroom\"'s scene directory.",
  89. * "data": [
  90. * "The file name does not match convention (scene_000150.ext - /^(.*)?_([0-9]{2,})\\.(.*)$/) : \"bathroom_adz00020.png\".",
  91. * "The file name does not match convention (scene_000150.ext - /^(.*)?_([0-9]{2,})\\.(.*)$/) : \"bathroom_adz00020.png\"."
  92. * ]
  93. * }
  94. *
  95. */
  96. /**
  97. * Cut an image, save its extracts and get the url of these extracts
  98. *
  99. * @param {object} image the path to the image to cut
  100. * @param {Number} xExtracts the number of extract to do on the horizontal axis (integer)
  101. * @param {Number} yExtracts the number of extract to do on the vertical axis (integer)
  102. * @returns {Promise<Image[]>} the list of extracted images
  103. */
  104. const cutImage = async (image, xExtracts, yExtracts) => {
  105. const input = sharp(image.path)
  106. const { width, height } = await input.metadata()
  107. const xCropSize = width / xExtracts
  108. const yCropSize = height / yExtracts
  109. // Check the image is cuttable with the current parameters
  110. let errorsList = []
  111. if (!Number.isInteger(xCropSize)) errorsList.push('Incompatible number of horizontal extracts (width % numberOfExtracts != 0).')
  112. if (!Number.isInteger(yCropSize)) errorsList.push('Incompatible number of vertical extracts (height % numberOfExtracts != 0).')
  113. if (errorsList.length > 0) throw boom.badRequest('Invalid query parameter(s).', errorsList)
  114. let extracts = []
  115. // Cut images
  116. // Vertical
  117. for (let y = 0; y < yExtracts; y++) {
  118. // Horizontal
  119. for (let x = 0; x < xExtracts; x++) {
  120. // How to cut the image
  121. const config = {
  122. left: x * xCropSize,
  123. top: y * yCropSize,
  124. width: xCropSize,
  125. height: yCropSize
  126. }
  127. // Zone number of the extract `00020`
  128. const fileNameCount = (extracts.length + 1).toString().padStart(5, '0')
  129. // File name of the extract : `Scene2_zone00199_100.png`
  130. const extractName = `${image.sceneName}_zone${fileNameCount}_${image.quality}.${image.ext}`
  131. // Configured path to the image (Check defined convention)
  132. const pathToImage = [image.sceneName, extractsDirName, `x${xExtracts}_y${yExtracts}`, `zone${fileNameCount}`, extractName]
  133. // File system path to the extract
  134. const extractPath = path.resolve(imagesPath, ...pathToImage)
  135. // URL to the extract on the app
  136. const extractLink = `${imageServedUrl}/${pathToImage.join('/')}`
  137. const extractObj = {
  138. link: extractLink,
  139. path: extractPath,
  140. fileName: extractName,
  141. sceneName: image.sceneName
  142. }
  143. // Check the file already exist
  144. let fileAlreadyExists = false
  145. try {
  146. await fs.access(extractPath, fsConstants.R_OK)
  147. fileAlreadyExists = true
  148. }
  149. catch (err) {
  150. // File does not exist already
  151. }
  152. // File already exist, just send its data
  153. if (fileAlreadyExists) {
  154. extracts.push(extractObj)
  155. continue
  156. }
  157. // File does not already exist, create it
  158. // Create the arborescence
  159. try {
  160. await fs.mkdir(path.resolve(imagesPath, ...pathToImage.slice(0, pathToImage.length - 1)), { recursive: true })
  161. }
  162. catch (err) {
  163. // An error was caught, add it and go to next extract
  164. errorsList.push(err.message)
  165. continue
  166. }
  167. // Cut and save the extract
  168. try {
  169. await input.extract(config).toFile(extractPath)
  170. extracts.push(extractObj)
  171. }
  172. catch (err) {
  173. // Error while cutting image
  174. errorsList.push(err)
  175. }
  176. }
  177. }
  178. // Extraction finished, check for errors
  179. if (errorsList.length > 0) throw boom.internal('Error(s) while extracting from image.', errorsList)
  180. return extracts
  181. }
  182. router.get('/', asyncMiddleware(async (req, res) => {
  183. // Check the request contains all the required parameters
  184. checkRequiredParameters(['sceneName', 'imageQuality', 'horizontalExtractCount', 'verticalExtractCount'], req.query)
  185. const { sceneName, imageQuality, horizontalExtractCount, verticalExtractCount } = req.query
  186. let errorList = []
  187. // Check the scene name is valid
  188. try {
  189. checkSceneName(sceneName)
  190. }
  191. catch (err) {
  192. errorList.push(err.message)
  193. }
  194. // Check `imageQuality` is an integer
  195. const qualityInt = parseInt(imageQuality, 10)
  196. if (isNaN(qualityInt)) errorList.push('The specified quality is not an integer.')
  197. // Check `horizontalExtractCount` is an integer
  198. const horizontalExtractCountInt = parseInt(horizontalExtractCount, 10)
  199. if (isNaN(horizontalExtractCountInt)) errorList.push('The specified number of extract for the horizontal axis is not an integer.')
  200. // Check `imageQuality` is an integer
  201. const verticalExtractCountInt = parseInt(verticalExtractCount, 10)
  202. if (isNaN(verticalExtractCountInt)) errorList.push('The specified number of extract for the vertical axis is not an integer.')
  203. // Check there is no errors with parameters
  204. if (errorList.length > 0)
  205. throw boom.badRequest('Invalid query parameter(s).', errorList)
  206. // Get the image path and link
  207. const image = await getImage(sceneName, qualityInt)
  208. // Cut the image
  209. const extracts = await cutImage(image, horizontalExtractCountInt, verticalExtractCountInt)
  210. // Send an array of links
  211. res.json({ data: extracts.map(x => x.link) })
  212. }))
  213. export default router