Roll Back compile version
This commit is contained in:
Vendored
+12
-7
@@ -1,13 +1,18 @@
|
||||
import imageInputFileToBase64 from "./imageInputFileToBase64";
|
||||
import imageInputToBase64 from "./imageInputToBase64";
|
||||
"use strict";
|
||||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
const imageInputFileToBase64_1 = __importDefault(require("./imageInputFileToBase64"));
|
||||
const imageInputToBase64_1 = __importDefault(require("./imageInputToBase64"));
|
||||
/**
|
||||
* ==========================
|
||||
* Media Functions Object
|
||||
* ==========================
|
||||
*/
|
||||
const media = {
|
||||
imageInputToBase64: imageInputToBase64,
|
||||
imageInputFileToBase64: imageInputFileToBase64,
|
||||
imageInputToBase64: imageInputToBase64_1.default,
|
||||
imageInputFileToBase64: imageInputFileToBase64_1.default,
|
||||
};
|
||||
/**
|
||||
* ==========================
|
||||
@@ -15,8 +20,8 @@ const media = {
|
||||
* ==========================
|
||||
*/
|
||||
const auth = {
|
||||
imageInputToBase64: imageInputToBase64,
|
||||
imageInputFileToBase64: imageInputFileToBase64,
|
||||
imageInputToBase64: imageInputToBase64_1.default,
|
||||
imageInputFileToBase64: imageInputFileToBase64_1.default,
|
||||
};
|
||||
/**
|
||||
* ==========================
|
||||
@@ -26,4 +31,4 @@ const auth = {
|
||||
const datasquirelClient = {
|
||||
media: media,
|
||||
};
|
||||
export default datasquirelClient;
|
||||
exports.default = datasquirelClient;
|
||||
|
||||
+86
-72
@@ -1,78 +1,92 @@
|
||||
"use strict";
|
||||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
||||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
||||
return new (P || (P = Promise))(function (resolve, reject) {
|
||||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
||||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
||||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
||||
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
||||
});
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.default = imageInputFileToBase64;
|
||||
/**
|
||||
* # Image input File top Base64
|
||||
*/
|
||||
export default async function imageInputFileToBase64({ imageInputFile, maxWidth, imagePreviewNode, }) {
|
||||
/**
|
||||
* Make https request
|
||||
*
|
||||
* @description make a request to datasquirel.com
|
||||
*/
|
||||
try {
|
||||
let imageName = imageInputFile.name.replace(/\..*/, "");
|
||||
let imageDataBase64;
|
||||
let imageSize;
|
||||
let canvas = document.createElement("canvas");
|
||||
const MIME_TYPE = imageInputFile.type;
|
||||
const QUALITY = 0.95;
|
||||
const MAX_WIDTH = maxWidth ? maxWidth : null;
|
||||
const file = imageInputFile;
|
||||
const blobURL = URL.createObjectURL(file);
|
||||
const img = new Image();
|
||||
/** ********************* Add source to new image */
|
||||
img.src = blobURL;
|
||||
imageDataBase64 = await new Promise((res, rej) => {
|
||||
/** ********************* Handle Errors in loading image */
|
||||
img.onerror = function () {
|
||||
URL.revokeObjectURL(this.src);
|
||||
console.log("Cannot load image");
|
||||
function imageInputFileToBase64(_a) {
|
||||
return __awaiter(this, arguments, void 0, function* ({ imageInputFile, maxWidth, imagePreviewNode, }) {
|
||||
/**
|
||||
* Make https request
|
||||
*
|
||||
* @description make a request to datasquirel.com
|
||||
*/
|
||||
try {
|
||||
let imageName = imageInputFile.name.replace(/\..*/, "");
|
||||
let imageDataBase64;
|
||||
let imageSize;
|
||||
let canvas = document.createElement("canvas");
|
||||
const MIME_TYPE = imageInputFile.type;
|
||||
const QUALITY = 0.95;
|
||||
const MAX_WIDTH = maxWidth ? maxWidth : null;
|
||||
const file = imageInputFile;
|
||||
const blobURL = URL.createObjectURL(file);
|
||||
const img = new Image();
|
||||
/** ********************* Add source to new image */
|
||||
img.src = blobURL;
|
||||
imageDataBase64 = yield new Promise((res, rej) => {
|
||||
/** ********************* Handle Errors in loading image */
|
||||
img.onerror = function () {
|
||||
URL.revokeObjectURL(this.src);
|
||||
console.log("Cannot load image");
|
||||
};
|
||||
/** ********************* Handle new image when loaded */
|
||||
img.onload = function (e) {
|
||||
const imgEl = e.target;
|
||||
URL.revokeObjectURL(imgEl.src);
|
||||
if (MAX_WIDTH) {
|
||||
const scaleSize = MAX_WIDTH / img.naturalWidth;
|
||||
canvas.width =
|
||||
img.naturalWidth < MAX_WIDTH
|
||||
? img.naturalWidth
|
||||
: MAX_WIDTH;
|
||||
canvas.height =
|
||||
img.naturalWidth < MAX_WIDTH
|
||||
? img.naturalHeight
|
||||
: img.naturalHeight * scaleSize;
|
||||
}
|
||||
else {
|
||||
canvas.width = img.naturalWidth;
|
||||
canvas.height = img.naturalHeight;
|
||||
}
|
||||
const ctx = canvas.getContext("2d");
|
||||
ctx === null || ctx === void 0 ? void 0 : ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
|
||||
const srcEncoded = canvas.toDataURL(MIME_TYPE, QUALITY);
|
||||
if (imagePreviewNode) {
|
||||
imagePreviewNode.src = srcEncoded;
|
||||
}
|
||||
res(srcEncoded);
|
||||
};
|
||||
});
|
||||
imageSize = yield new Promise((res, rej) => {
|
||||
canvas.toBlob((blob) => {
|
||||
res(blob === null || blob === void 0 ? void 0 : blob.size);
|
||||
}, MIME_TYPE, QUALITY);
|
||||
});
|
||||
return {
|
||||
imageBase64: imageDataBase64 === null || imageDataBase64 === void 0 ? void 0 : imageDataBase64.replace(/.*?base64,/, ""),
|
||||
imageBase64Full: imageDataBase64,
|
||||
imageName: imageName,
|
||||
imageSize: imageSize,
|
||||
};
|
||||
/** ********************* Handle new image when loaded */
|
||||
img.onload = function (e) {
|
||||
const imgEl = e.target;
|
||||
URL.revokeObjectURL(imgEl.src);
|
||||
if (MAX_WIDTH) {
|
||||
const scaleSize = MAX_WIDTH / img.naturalWidth;
|
||||
canvas.width =
|
||||
img.naturalWidth < MAX_WIDTH
|
||||
? img.naturalWidth
|
||||
: MAX_WIDTH;
|
||||
canvas.height =
|
||||
img.naturalWidth < MAX_WIDTH
|
||||
? img.naturalHeight
|
||||
: img.naturalHeight * scaleSize;
|
||||
}
|
||||
else {
|
||||
canvas.width = img.naturalWidth;
|
||||
canvas.height = img.naturalHeight;
|
||||
}
|
||||
const ctx = canvas.getContext("2d");
|
||||
ctx === null || ctx === void 0 ? void 0 : ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
|
||||
const srcEncoded = canvas.toDataURL(MIME_TYPE, QUALITY);
|
||||
if (imagePreviewNode) {
|
||||
imagePreviewNode.src = srcEncoded;
|
||||
}
|
||||
res(srcEncoded);
|
||||
}
|
||||
catch (error) {
|
||||
console.log("Image Processing Error! =>", error.message);
|
||||
return {
|
||||
imageBase64: undefined,
|
||||
imageBase64Full: undefined,
|
||||
imageName: undefined,
|
||||
imageSize: undefined,
|
||||
};
|
||||
});
|
||||
imageSize = await new Promise((res, rej) => {
|
||||
canvas.toBlob((blob) => {
|
||||
res(blob === null || blob === void 0 ? void 0 : blob.size);
|
||||
}, MIME_TYPE, QUALITY);
|
||||
});
|
||||
return {
|
||||
imageBase64: imageDataBase64 === null || imageDataBase64 === void 0 ? void 0 : imageDataBase64.replace(/.*?base64,/, ""),
|
||||
imageBase64Full: imageDataBase64,
|
||||
imageName: imageName,
|
||||
imageSize: imageSize,
|
||||
};
|
||||
}
|
||||
catch (error) {
|
||||
console.log("Image Processing Error! =>", error.message);
|
||||
return {
|
||||
imageBase64: undefined,
|
||||
imageBase64Full: undefined,
|
||||
imageName: undefined,
|
||||
imageSize: undefined,
|
||||
};
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
+82
-68
@@ -1,76 +1,90 @@
|
||||
"use strict";
|
||||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
||||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
||||
return new (P || (P = Promise))(function (resolve, reject) {
|
||||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
||||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
||||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
||||
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
||||
});
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.default = imageInputToBase64;
|
||||
/**
|
||||
* # Image Input Element to Base 64
|
||||
*/
|
||||
export default async function imageInputToBase64({ imageInput, maxWidth, mimeType, }) {
|
||||
var _a, _b;
|
||||
/**
|
||||
* Make https request
|
||||
*
|
||||
* @description make a request to datasquirel.com
|
||||
*/
|
||||
try {
|
||||
let imagePreviewNode = document.querySelector(`[data-imagepreview='image']`);
|
||||
let imageName = (_a = imageInput.files) === null || _a === void 0 ? void 0 : _a[0].name.replace(/\..*/, "");
|
||||
let imageDataBase64;
|
||||
const MIME_TYPE = mimeType ? mimeType : "image/jpeg";
|
||||
const QUALITY = 0.95;
|
||||
const MAX_WIDTH = maxWidth ? maxWidth : null;
|
||||
const file = (_b = imageInput.files) === null || _b === void 0 ? void 0 : _b[0];
|
||||
const blobURL = file ? URL.createObjectURL(file) : undefined;
|
||||
const img = new Image();
|
||||
if (blobURL) {
|
||||
img.src = blobURL;
|
||||
imageDataBase64 = await new Promise((res, rej) => {
|
||||
/** ********************* Handle Errors in loading image */
|
||||
img.onerror = function () {
|
||||
URL.revokeObjectURL(this.src);
|
||||
window.alert("Cannot load image!");
|
||||
function imageInputToBase64(_a) {
|
||||
return __awaiter(this, arguments, void 0, function* ({ imageInput, maxWidth, mimeType, }) {
|
||||
var _b, _c;
|
||||
/**
|
||||
* Make https request
|
||||
*
|
||||
* @description make a request to datasquirel.com
|
||||
*/
|
||||
try {
|
||||
let imagePreviewNode = document.querySelector(`[data-imagepreview='image']`);
|
||||
let imageName = (_b = imageInput.files) === null || _b === void 0 ? void 0 : _b[0].name.replace(/\..*/, "");
|
||||
let imageDataBase64;
|
||||
const MIME_TYPE = mimeType ? mimeType : "image/jpeg";
|
||||
const QUALITY = 0.95;
|
||||
const MAX_WIDTH = maxWidth ? maxWidth : null;
|
||||
const file = (_c = imageInput.files) === null || _c === void 0 ? void 0 : _c[0];
|
||||
const blobURL = file ? URL.createObjectURL(file) : undefined;
|
||||
const img = new Image();
|
||||
if (blobURL) {
|
||||
img.src = blobURL;
|
||||
imageDataBase64 = yield new Promise((res, rej) => {
|
||||
/** ********************* Handle Errors in loading image */
|
||||
img.onerror = function () {
|
||||
URL.revokeObjectURL(this.src);
|
||||
window.alert("Cannot load image!");
|
||||
};
|
||||
img.onload = function (e) {
|
||||
const imgEl = e.target;
|
||||
URL.revokeObjectURL(imgEl.src);
|
||||
const canvas = document.createElement("canvas");
|
||||
if (MAX_WIDTH) {
|
||||
const scaleSize = MAX_WIDTH / img.naturalWidth;
|
||||
canvas.width =
|
||||
img.naturalWidth < MAX_WIDTH
|
||||
? img.naturalWidth
|
||||
: MAX_WIDTH;
|
||||
canvas.height =
|
||||
img.naturalWidth < MAX_WIDTH
|
||||
? img.naturalHeight
|
||||
: img.naturalHeight * scaleSize;
|
||||
}
|
||||
else {
|
||||
canvas.width = img.naturalWidth;
|
||||
canvas.height = img.naturalHeight;
|
||||
}
|
||||
const ctx = canvas.getContext("2d");
|
||||
ctx === null || ctx === void 0 ? void 0 : ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
|
||||
const srcEncoded = canvas.toDataURL(MIME_TYPE, QUALITY);
|
||||
if (imagePreviewNode) {
|
||||
document
|
||||
.querySelectorAll(`[data-imagepreview='image']`)
|
||||
.forEach((_img) => {
|
||||
const _imgEl = _img;
|
||||
_imgEl.src = srcEncoded;
|
||||
});
|
||||
}
|
||||
res(srcEncoded);
|
||||
};
|
||||
});
|
||||
return {
|
||||
imageBase64: imageDataBase64 === null || imageDataBase64 === void 0 ? void 0 : imageDataBase64.replace(/.*?base64,/, ""),
|
||||
imageBase64Full: imageDataBase64,
|
||||
imageName: imageName,
|
||||
};
|
||||
img.onload = function (e) {
|
||||
const imgEl = e.target;
|
||||
URL.revokeObjectURL(imgEl.src);
|
||||
const canvas = document.createElement("canvas");
|
||||
if (MAX_WIDTH) {
|
||||
const scaleSize = MAX_WIDTH / img.naturalWidth;
|
||||
canvas.width =
|
||||
img.naturalWidth < MAX_WIDTH
|
||||
? img.naturalWidth
|
||||
: MAX_WIDTH;
|
||||
canvas.height =
|
||||
img.naturalWidth < MAX_WIDTH
|
||||
? img.naturalHeight
|
||||
: img.naturalHeight * scaleSize;
|
||||
}
|
||||
else {
|
||||
canvas.width = img.naturalWidth;
|
||||
canvas.height = img.naturalHeight;
|
||||
}
|
||||
const ctx = canvas.getContext("2d");
|
||||
ctx === null || ctx === void 0 ? void 0 : ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
|
||||
const srcEncoded = canvas.toDataURL(MIME_TYPE, QUALITY);
|
||||
if (imagePreviewNode) {
|
||||
document
|
||||
.querySelectorAll(`[data-imagepreview='image']`)
|
||||
.forEach((_img) => {
|
||||
const _imgEl = _img;
|
||||
_imgEl.src = srcEncoded;
|
||||
});
|
||||
}
|
||||
res(srcEncoded);
|
||||
};
|
||||
});
|
||||
return {
|
||||
imageBase64: imageDataBase64 === null || imageDataBase64 === void 0 ? void 0 : imageDataBase64.replace(/.*?base64,/, ""),
|
||||
imageBase64Full: imageDataBase64,
|
||||
imageName: imageName,
|
||||
};
|
||||
}
|
||||
else {
|
||||
return {};
|
||||
}
|
||||
}
|
||||
else {
|
||||
catch ( /** @type {*} */error) {
|
||||
console.log("Image Processing Error! =>", error.message);
|
||||
return {};
|
||||
}
|
||||
}
|
||||
catch ( /** @type {*} */error) {
|
||||
console.log("Image Processing Error! =>", error.message);
|
||||
return {};
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
+48
-34
@@ -1,3 +1,15 @@
|
||||
"use strict";
|
||||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
||||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
||||
return new (P || (P = Promise))(function (resolve, reject) {
|
||||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
||||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
||||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
||||
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
||||
});
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.default = inputFileToBase64;
|
||||
/**
|
||||
* Input File to base64
|
||||
* ==============================================================================
|
||||
@@ -6,40 +18,42 @@
|
||||
* HTML file input elements usually return an array of input objects, so be sure to select the target
|
||||
* file from the array.
|
||||
*/
|
||||
export default async function inputFileToBase64({ inputFile, allowedRegex, }) {
|
||||
var _a;
|
||||
const allowedTypesRegex = allowedRegex ? allowedRegex : /image\/*|\/pdf/;
|
||||
if (!((_a = inputFile === null || inputFile === void 0 ? void 0 : inputFile.type) === null || _a === void 0 ? void 0 : _a.match(allowedTypesRegex))) {
|
||||
window.alert(`We currently don't support ${inputFile.type} file types. Support is coming soon. For now we support only images and PDFs.`);
|
||||
return {
|
||||
fileName: inputFile.name,
|
||||
};
|
||||
}
|
||||
try {
|
||||
let fileName = inputFile.name.replace(/\..*/, "");
|
||||
const fileData = await new Promise((resolve, reject) => {
|
||||
var reader = new FileReader();
|
||||
reader.readAsDataURL(inputFile);
|
||||
reader.onload = function () {
|
||||
var _a;
|
||||
resolve((_a = reader.result) === null || _a === void 0 ? void 0 : _a.toString());
|
||||
function inputFileToBase64(_a) {
|
||||
return __awaiter(this, arguments, void 0, function* ({ inputFile, allowedRegex, }) {
|
||||
var _b;
|
||||
const allowedTypesRegex = allowedRegex ? allowedRegex : /image\/*|\/pdf/;
|
||||
if (!((_b = inputFile === null || inputFile === void 0 ? void 0 : inputFile.type) === null || _b === void 0 ? void 0 : _b.match(allowedTypesRegex))) {
|
||||
window.alert(`We currently don't support ${inputFile.type} file types. Support is coming soon. For now we support only images and PDFs.`);
|
||||
return {
|
||||
fileName: inputFile.name,
|
||||
};
|
||||
reader.onerror = function (/** @type {*} */ error) {
|
||||
console.log("Error: ", error.message);
|
||||
}
|
||||
try {
|
||||
let fileName = inputFile.name.replace(/\..*/, "");
|
||||
const fileData = yield new Promise((resolve, reject) => {
|
||||
var reader = new FileReader();
|
||||
reader.readAsDataURL(inputFile);
|
||||
reader.onload = function () {
|
||||
var _a;
|
||||
resolve((_a = reader.result) === null || _a === void 0 ? void 0 : _a.toString());
|
||||
};
|
||||
reader.onerror = function (/** @type {*} */ error) {
|
||||
console.log("Error: ", error.message);
|
||||
};
|
||||
});
|
||||
return {
|
||||
fileBase64: fileData === null || fileData === void 0 ? void 0 : fileData.replace(/.*?base64,/, ""),
|
||||
fileBase64Full: fileData,
|
||||
fileName: fileName,
|
||||
fileSize: inputFile.size,
|
||||
fileType: inputFile.type,
|
||||
};
|
||||
});
|
||||
return {
|
||||
fileBase64: fileData === null || fileData === void 0 ? void 0 : fileData.replace(/.*?base64,/, ""),
|
||||
fileBase64Full: fileData,
|
||||
fileName: fileName,
|
||||
fileSize: inputFile.size,
|
||||
fileType: inputFile.type,
|
||||
};
|
||||
}
|
||||
catch (error) {
|
||||
console.log("File Processing Error! =>", error.message);
|
||||
return {
|
||||
fileName: inputFile.name,
|
||||
};
|
||||
}
|
||||
}
|
||||
catch (error) {
|
||||
console.log("File Processing Error! =>", error.message);
|
||||
return {
|
||||
fileName: inputFile.name,
|
||||
};
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user