Skip to content

Commit

Permalink
fixes #30 #24
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Salvagni committed Oct 7, 2021
1 parent 1b7b093 commit c95b5a1
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions src/lib/FileReader.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = () => {
Expand Down

0 comments on commit c95b5a1

Please sign in to comment.