Skip to content

Commit

Permalink
Merge branch 'main' into selfie-instruction-screen
Browse files Browse the repository at this point in the history
  • Loading branch information
ayinloya authored Jan 23, 2024
2 parents 594b88e + ac2d3d9 commit 2cfd0c5
Show file tree
Hide file tree
Showing 9 changed files with 999 additions and 1 deletion.
6 changes: 5 additions & 1 deletion .github/workflows/stale.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@ on:
schedule:
- cron: "30 16 * * *"

permissions:
issues: write
pull-requests: write

jobs:
stale:
runs-on: ubuntu-latest
timeout-minutes: 5
steps:
- uses: actions/stale@v7
- uses: actions/stale@v9
with:
stale-issue-message: "This issue is stale because it has been open 14 days with no activity. Remove stale label or comment or this will be closed in 7 days."
stale-pr-message: "This PR is stale because it has been open 14 days with no activity. Remove stale label or comment or this will be closed in 7 days."
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import "./index";

const meta = {
component: "document-instruction",
};

export default meta;

export const DocumentInstruction = {
render: () => `
<document-instruction
show-navigation
document-capture-modes="camera,upload"
>
</document-instruction>
`,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export {
DocumentInstruction
} from './DocumentInstruction';
23 changes: 23 additions & 0 deletions packages/components/domain/Constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* The type of image submitted in the job request
* @readonly
* @enum {number}
*/
export const IMAGE_TYPE = {
/** SELFIE_IMAGE_FILE Selfie image in .png or .jpg file format */
SELFIE_IMAGE_FILE: 0,
/** ID_CARD_IMAGE_FILE ID card image in .png or .jpg file format */
ID_CARD_IMAGE_FILE: 1,
/** SELFIE_IMAGE_BASE64 Base64 encoded selfie image (.png or .jpg) */
SELFIE_IMAGE_BASE64: 2,
/** ID_CARD_IMAGE_BASE64 Base64 encoded ID card image (.png or .jpg) */
ID_CARD_IMAGE_BASE64: 3,
/** LIVENESS_IMAGE_FILE Liveness image in .png or .jpg file format */
LIVENESS_IMAGE_FILE: 4,
/** ID_CARD_BACK_IMAGE_FILE Back of ID card image in .png or .jpg file format */
ID_CARD_BACK_IMAGE_FILE: 5,
/** LIVENESS_IMAGE_BASE64 Base64 encoded liveness image (.jpg or .png) */
LIVENESS_IMAGE_BASE64: 6,
/** ID_CARD_BACK_IMAGE_BASE64 Base64 encoded back of ID card image (.jpg or .png) */
ID_CARD_BACK_IMAGE_BASE64: 7,
};
66 changes: 66 additions & 0 deletions packages/components/domain/SmartFileUpload.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
class SmartFileUpload {
static memoryLimit = 10240000;

static supportedTypes = ['image/jpeg', 'image/png'];

static getHumanSize(numberOfBytes) {
// Approximate to the closest prefixed unit
const units = [
'B',
'kB',
'MB',
'GB',
'TB',
'PB',
'EB',
'ZB',
'YB',
];
const exponent = Math.min(
Math.floor(Math.log(numberOfBytes) / Math.log(1024)),
units.length - 1,
);
const approx = numberOfBytes / 1024 ** exponent;
const output = exponent === 0
? `${numberOfBytes} bytes`
: `${approx.toFixed(0)} ${units[exponent]}`;

return output;
}

static getData(file) {
return new Promise((resolve, reject) => {
const reader = new FileReader();

reader.onload = (e) => {
resolve(e.target.result);
};
reader.onerror = () => {
reject(new Error('An error occurred reading the file. Please check the file, and try again'));
};
reader.readAsDataURL(file);
});
}

static async retrieve(files) {
if (files.length > 1) {
throw new Error('Only one file upload is permitted at a time');
}

const file = files[0];

if (!SmartFileUpload.supportedTypes.includes(file.type)) {
throw new Error('Unsupported file format. Please ensure that you are providing a JPG or PNG image');
}

if (file.size > SmartFileUpload.memoryLimit) {
throw new Error(`${file.name} is too large. Please ensure that the file is less than ${SmartFileUpload.getHumanSize(SmartFileUpload.memoryLimit)}.`);
}

const imageAsDataUrl = await SmartFileUpload.getData(file);

return imageAsDataUrl;
}
}

export default SmartFileUpload;
76 changes: 76 additions & 0 deletions packages/components/domain/camera/SmartCamera.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
class SmartCamera {
static stream = null;

static async getMedia(constraints) {
try {
SmartCamera.stream = await navigator.mediaDevices.getUserMedia({
...constraints,
video: {
...constraints.video,
// NOTE: Special case for multi-camera Samsung devices (learnt from Acuant)
// "We found out that some triple camera Samsung devices (S10, S20, Note 20, etc) capture images blurry at edges.
// Zooming to 2X, matching the telephoto lens, doesn't solve it completely but mitigates it."
zoom: SmartCamera.isSamsungMultiCameraDevice() ? 2.0 : 1.0,
}
});
return SmartCamera.stream;
} catch (error) {
throw error;
}
}

static stopMedia() {
if (SmartCamera.stream) {
SmartCamera.stream.getTracks().forEach(track => track.stop());
SmartCamera.stream = null;
}
}

static isSamsungMultiCameraDevice() {
const matchedModelNumber = navigator.userAgent.match(/SM-[N|G]\d{3}/);
if (!matchedModelNumber) {
return false;
}

const modelNumber = parseInt(matchedModelNumber[0].match(/\d{3}/)[0], 10);
const smallerModelNumber = 970; // S10e
return !isNaN(modelNumber) && modelNumber >= smallerModelNumber;
}

static handleCameraError(e) {
switch (e.name) {
case 'NotAllowedError':
case 'SecurityError':
return `
Looks like camera access was not granted, or was blocked by a browser
level setting / extension. Please follow the prompt from the URL bar,
or extensions, and enable access.
You may need to refresh to start all over again
`;
case 'AbortError':
return `
Oops! Something happened, and we lost access to your stream.
Please refresh to start all over again
`;
case 'NotReadableError':
return `
There seems to be a problem with your device's camera, or its connection.
Please check this, and when resolved, try again. Or try another device.
`;
case 'NotFoundError':
return `
We are unable to find a video stream.
You may need to refresh to start all over again
`;
case 'TypeError':
return `
This site is insecure, and as such cannot have access to your camera.
Try to navigate to a secure version of this page, or contact the owner.
`;
default:
return e.message;
}
}
}

export { SmartCamera };
Loading

0 comments on commit 2cfd0c5

Please sign in to comment.