feat: relay id data photos to server

This commit is contained in:
José Oliveira 2021-03-17 13:53:20 +00:00 committed by Josh Harvey
parent 04e583b47d
commit c221437618
5 changed files with 94 additions and 3 deletions

View file

@ -20,6 +20,7 @@ const notifierUtils = require('./notifier/utils')
const NUM_RESULTS = 1000
const idPhotoCardBasedir = _.get('idPhotoCardDir', options)
const frontCameraBaseDir = _.get('frontCameraDir', options)
const idCardDataDir = _.get('idCardDataDir', options)
/**
* Add new customer
@ -570,6 +571,77 @@ function updatePhotoCard (id, patch) {
})
}
/**
* @param {String} imageData image encoded
* @param {String} directory directory path of id card data for a certain user
*/
function updatePhoto (imageData, directory) {
return Promise.resolve(imageData)
.then(patch => {
if (_.isEmpty(imageData)) {
return patch
}
const newPatch = {
idCardDataPath: ''
}
// decode the base64 string to binary data
const decodedImageData = Buffer.from(imageData, 'base64')
// workout the image hash
// i.e. 240e85ff2e4bb931f235985dd0134e459239496d2b5af6c5665168d38ef89b50
const hash = crypto
.createHash('sha256')
.update(imageData)
.digest('hex')
// workout the image folder
// i.e. 24/0e/85
const rpath = _.join(path.sep, _.map(_.wrap(_.join, ''), _.take(3, _.chunk(2, _.split('', hash)))))
// i.e. ../<lamassu-server-home>/idcardphoto/customer_id/24/0e/85
const dirname = path.join(directory, rpath)
// create the directory tree if needed
_.attempt(() => makeDir.sync(dirname))
// i.e. ../<lamassu-server-home>/idcardphoto/customer_id/24/0e/85/240e85ff2e4bb931f235985dd01....jpg
const filename = path.join(dirname, hash + '.jpg')
//
// i.e. {
// "idCardDataPath": "24/0e/85/240e85ff2e4bb931f235985dd01....jpg",
// "idCardDataAt": "now()"
// }
newPatch.idCardData = path.join(rpath)
newPatch.idCardDataAt = 'now()'
// write image file
return writeFile(filename, decodedImageData)
.then(() => newPatch)
})
}
/**
* @param {String} id customer id
* @param {Object} patch customer latest id card photos
* @returns {Promise<Object>} new patch to be applied
*/
function updateIdCardData (patch, id) {
const directory = idCardDataDir + '/' + id
return Promise.resolve(patch)
.then(patch => {
const imagesData = _.get('photos', patch)
_.map(imageData => {
return updatePhoto(imageData, directory)
.then(newPatch => newPatch)
.catch(err => console.log('An error ocurred while saving the image ', err))
}, imagesData)
})
}
function updateFrontCamera (id, patch) {
return Promise.resolve(patch)
.then(patch => {
@ -620,4 +692,4 @@ function updateFrontCamera (id, patch) {
})
}
module.exports = { add, get, batch, getCustomersList, getCustomerById, getById, update, updateCustomer, updatePhotoCard, updateFrontCamera }
module.exports = { add, get, batch, getCustomersList, getCustomerById, getById, update, updateCustomer, updatePhotoCard, updateFrontCamera, updateIdCardData }