getImageExtracts.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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, Image } from './getImage' // eslint-disable-line no-unused-vars
  10. const router = express.Router()
  11. /**
  12. * Cut an image, save its extracts and get the url of these extracts
  13. *
  14. * @param {Image} image the path to the image to cut
  15. * @param {Number} xExtracts the number of extract to do on the horizontal axis (integer)
  16. * @param {Number} yExtracts the number of extract to do on the vertical axis (integer)
  17. * @returns {Promise<Image[]>} the list of extracted images
  18. */
  19. const cutImage = async (image, xExtracts, yExtracts) => {
  20. const input = sharp(image.path)
  21. const { width, height } = await input.metadata()
  22. const xCropSize = width / xExtracts
  23. const yCropSize = height / yExtracts
  24. // Check the image is cuttable with the current parameters
  25. let errorsList = []
  26. if (!Number.isInteger(xCropSize)) errorsList.push('Incompatible number of horizontal extracts (width % numberOfExtracts != 0).')
  27. if (!Number.isInteger(yCropSize)) errorsList.push('Incompatible number of vertical extracts (height % numberOfExtracts != 0).')
  28. if (errorsList.length > 0) throw boom.badRequest('Invalid query parameter(s).', errorsList)
  29. let extracts = []
  30. // Cut images
  31. // Vertical
  32. for (let y = 0; y < yExtracts; y++) {
  33. // Horizontal
  34. for (let x = 0; x < xExtracts; x++) {
  35. // How to cut the image
  36. const config = {
  37. left: x * xCropSize,
  38. top: y * yCropSize,
  39. width: xCropSize,
  40. height: yCropSize
  41. }
  42. // Zone number of the extract `00020`
  43. const fileNameCount = (extracts.length + 1).toString().padStart(5, '0')
  44. // File name of the extract : `Scene2_zone00199_100.png`
  45. const extractName = `${image.sceneName}_zone${fileNameCount}_${image.quality}.${image.ext}`
  46. // Configured path to the image (Check defined convention)
  47. const pathToImage = [image.sceneName, extractsDirName, `x${xExtracts}_y${yExtracts}`, `zone${fileNameCount}`, extractName]
  48. // File system path to the extract
  49. const extractPath = path.resolve(imagesPath, ...pathToImage)
  50. // URL to the extract on the app
  51. const extractLink = `${imageServedUrl}/${pathToImage.join('/')}`
  52. const extractObj = {
  53. link: extractLink,
  54. path: extractPath,
  55. fileName: extractName,
  56. sceneName: image.sceneName
  57. }
  58. // Check the file already exist
  59. let fileAlreadyExists = false
  60. try {
  61. await fs.access(extractPath, fsConstants.R_OK)
  62. fileAlreadyExists = true
  63. }
  64. catch (err) {
  65. // File does not exist already
  66. }
  67. // File already exist, just send its data
  68. if (fileAlreadyExists) {
  69. extracts.push(extractObj)
  70. continue
  71. }
  72. // File does not already exist, create it
  73. // Create the arborescence
  74. try {
  75. await fs.mkdir(path.resolve(imagesPath, ...pathToImage.slice(0, pathToImage.length - 1)), { recursive: true })
  76. }
  77. catch (err) {
  78. // An error was caught, add it and go to next extract
  79. errorsList.push(err.message)
  80. continue
  81. }
  82. // Cut and save the extract
  83. try {
  84. await input.extract(config).toFile(extractPath)
  85. extracts.push(extractObj)
  86. }
  87. catch (err) {
  88. // Error while cutting image
  89. errorsList.push(err)
  90. }
  91. }
  92. }
  93. // Extraction finished, check for errors
  94. if (errorsList.length > 0) throw boom.internal('Error(s) while extracting from image.', errorsList)
  95. return extracts
  96. }
  97. router.get('/', asyncMiddleware(async (req, res) => {
  98. // Check the request contains all the required parameters
  99. checkRequiredParameters(['sceneName', 'imageQuality', 'horizontalExtractCount', 'verticalExtractCount'], req.query)
  100. const { sceneName, imageQuality, horizontalExtractCount, verticalExtractCount } = req.query
  101. let errorList = []
  102. // Check the scene name is valid
  103. try {
  104. checkSceneName(sceneName)
  105. }
  106. catch (err) {
  107. errorList.push(err.message)
  108. }
  109. // Check `imageQuality` is an integer
  110. const qualityInt = parseInt(imageQuality, 10)
  111. if (isNaN(qualityInt)) errorList.push('The specified quality is not an integer.')
  112. // Check `horizontalExtractCount` is an integer
  113. const horizontalExtractCountInt = parseInt(horizontalExtractCount, 10)
  114. if (isNaN(horizontalExtractCountInt)) errorList.push('The specified number of extract for the horizontal axis is not an integer.')
  115. // Check `imageQuality` is an integer
  116. const verticalExtractCountInt = parseInt(verticalExtractCount, 10)
  117. if (isNaN(verticalExtractCountInt)) errorList.push('The specified number of extract for the vertical axis is not an integer.')
  118. // Check there is no errors with parameters
  119. if (errorList.length > 0)
  120. throw boom.badRequest('Invalid query parameter(s).', errorList)
  121. // Get the image path and link
  122. const image = await getImage(sceneName, qualityInt)
  123. // Cut the image
  124. const extracts = await cutImage(image, horizontalExtractCountInt, verticalExtractCountInt)
  125. // Send an array of links
  126. res.json({ data: extracts.map(x => x.link) })
  127. }))
  128. export default router