-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
51 lines (46 loc) · 1.72 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
function isValidImage(image, maxSizeInMegabytes, validExtenions) {
var extArray = ['png', 'jpeg', 'jpg', 'gif'];
if (validExtenions && validExtenions.length) extArray = validExtenions;
return new Promise(function (resolve, reject) {
if (extArray.indexOf(image.type.split('/')[1]) !== -1) {
var maxSizeInBytes = maxSizeInMegabytes * 1024 * 1024;
if (image.size <= maxSizeInBytes) resolve(true);
else reject("Image ".concat(image.name, " is greater than ").concat(maxSizeInMegabytes, " megabytes."));
}
else reject("Image ".concat(image.name, " is not a valid image format. (valid image formats are: ").concat(extArray.join(', '), ")"));
});
}
function getBase64Promise(file) {
return new Promise(function (resolve, reject) {
var reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = function () {
return resolve(reader.result);
};
reader.onerror = function (error) {
return reject(error);
};
});
}
function imageToBase64(image) {
var maxSizeInMegabytes = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 3;
var validExtenions = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
return new Promise(function (resolve, reject) {
isValidImage(image, maxSizeInMegabytes, validExtenions).then(function () {
return getBase64Promise(image).then(function (result) {
return resolve(result);
}).catch(function (error) {
return reject(error);
});
}).catch(function (error) {
return reject(error);
});
});
}
var _default = imageToBase64;
exports.default = _default;