Front facing camera (#289)
* Front facing camera * Adding in configuration and update script
This commit is contained in:
parent
90de4fe24d
commit
00c4187081
10 changed files with 142 additions and 8 deletions
|
|
@ -8,7 +8,6 @@ const fs = require('fs')
|
|||
const util = require('util')
|
||||
const moment = require('moment')
|
||||
|
||||
|
||||
const db = require('./db')
|
||||
const BN = require('./bn')
|
||||
const anonymous = require('../lib/constants').anonymousCustomer
|
||||
|
|
@ -19,6 +18,7 @@ const writeFile = util.promisify(fs.writeFile)
|
|||
|
||||
const NUM_RESULTS = 1000
|
||||
const idPhotoCardBasedir = _.get('idPhotoCardDir', options)
|
||||
const frontCameraBaseDir = _.get('frontCameraDir', options)
|
||||
|
||||
/**
|
||||
* Add new customer
|
||||
|
|
@ -432,4 +432,54 @@ function updatePhotoCard (id, patch) {
|
|||
})
|
||||
}
|
||||
|
||||
module.exports = {add, get, batch, getById, update, updatePhotoCard}
|
||||
function updateFrontCamera (id, patch) {
|
||||
return Promise.resolve(patch)
|
||||
.then(patch => {
|
||||
// Base64 encoded image /9j/4AAQSkZJRgABAQAAAQ..
|
||||
const imageData = _.get('frontCameraData', patch)
|
||||
|
||||
if (_.isEmpty(imageData)) {
|
||||
return patch
|
||||
}
|
||||
|
||||
// remove idCardPhotoData from the update record
|
||||
const newPatch = _.omit('frontCameraData', patch)
|
||||
|
||||
// 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>/idphotocard/24/0e/85
|
||||
const dirname = path.join(frontCameraBaseDir, rpath)
|
||||
|
||||
// create the directory tree if needed
|
||||
_.attempt(() => makeDir.sync(dirname))
|
||||
|
||||
// i.e. ../<lamassu-server-home>/idphotocard/24/0e/85/240e85ff2e4bb931f235985dd01....jpg
|
||||
const filename = path.join(dirname, hash + '.jpg')
|
||||
|
||||
// update db record patch
|
||||
// i.e. {
|
||||
// "idCardPhotoPath": "24/0e/85/240e85ff2e4bb931f235985dd01....jpg",
|
||||
// "idCardPhotoAt": "now()"
|
||||
// }
|
||||
newPatch.frontCameraPath = path.join(rpath, hash + '.jpg')
|
||||
newPatch.frontCameraAt = 'now()'
|
||||
|
||||
// write image file
|
||||
return writeFile(filename, decodedImageData)
|
||||
.then(() => newPatch)
|
||||
})
|
||||
}
|
||||
|
||||
module.exports = { add, get, batch, getById, update, updatePhotoCard, updateFrontCamera }
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue