-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix file selector Copy fileupload class to components
- Loading branch information
Showing
8 changed files
with
201 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
class Utils { | ||
static handleError(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 default Utils; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,7 +42,6 @@ ${typography} | |
img { | ||
height: auto; | ||
max-width: 100%; | ||
transform: scaleX(-1); | ||
} | ||
video { | ||
|