-
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Daniel Salvagni
committed
Oct 7, 2021
1 parent
1b7b093
commit c95b5a1
Showing
1 changed file
with
11 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,37 +4,39 @@ const UNKNOWN = "UNKNOWN"; | |
/** | ||
* Wrapper for the JS FileReader API. | ||
* @author Daniel Salvagni <[email protected]> | ||
* | ||
* | ||
* @param {File} file Image file | ||
* @param {object} settings Function's settings | ||
* @param {function} settings.onError Error Callback | ||
* @param {function} settings.onError Error Callback | ||
* @param {function} settings.onLoadStart Start Callback | ||
* @param {function} settings.onLoadEnd Finished loading callback | ||
* @returns {object} FileReader instance | ||
*/ | ||
const fileReader = (file, settings) => { | ||
if (!file.type.match("image.*")) | ||
if (typeof config.onError === "function") | ||
config.onError.call(this, { error: INVALID_FILE_TYPE }); | ||
|
||
const config = { | ||
onError: () => {}, | ||
onLoadStart: () => {}, | ||
onLoadEnd: () => {}, | ||
...settings | ||
...settings, | ||
}; | ||
|
||
if (!file.type.match("image.*")) { | ||
if (typeof config.onError === "function") | ||
config.onError.call(this, { error: INVALID_FILE_TYPE }); | ||
return; | ||
} | ||
|
||
const reader = new FileReader(); | ||
|
||
reader.onloadstart = () => { | ||
if (typeof config.onLoadStart === "function") | ||
config.onLoadStart.call(this, { file }); | ||
}; | ||
reader.onloadend = data => { | ||
reader.onloadend = (data) => { | ||
if (typeof config.onLoadEnd === "function") | ||
config.onLoadEnd.call(this, { | ||
base64Image: data.target.result, | ||
type: file.type | ||
type: file.type, | ||
}); | ||
}; | ||
reader.onerror = () => { | ||
|