This commit is contained in:
Benjamin Toby
2025-01-10 20:35:05 +01:00
parent 9192dae0b5
commit a3561da53d
286 changed files with 13862 additions and 42590 deletions
+14
View File
@@ -0,0 +1,14 @@
import imageInputFileToBase64 from "./imageInputFileToBase64";
import imageInputToBase64 from "./imageInputToBase64";
/**
* ==========================
* Main Export
* ==========================
*/
declare const datasquirelClient: {
media: {
imageInputToBase64: typeof imageInputToBase64;
imageInputFileToBase64: typeof imageInputFileToBase64;
};
};
export default datasquirelClient;
+34
View File
@@ -0,0 +1,34 @@
"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_1.default,
imageInputFileToBase64: imageInputFileToBase64_1.default,
};
/**
* ==========================
* Media Functions Object
* ==========================
*/
const auth = {
imageInputToBase64: imageInputToBase64_1.default,
imageInputFileToBase64: imageInputFileToBase64_1.default,
};
/**
* ==========================
* Main Export
* ==========================
*/
const datasquirelClient = {
media: media,
};
exports.default = datasquirelClient;
+11
View File
@@ -0,0 +1,11 @@
import { ImageInputFileToBase64FunctionReturn } from "../../package-shared/types";
type Param = {
imageInputFile: File;
maxWidth?: number;
imagePreviewNode?: HTMLImageElement;
};
/**
* # Image input File top Base64
*/
export default function imageInputFileToBase64({ imageInputFile, maxWidth, imagePreviewNode, }: Param): Promise<ImageInputFileToBase64FunctionReturn>;
export {};
+92
View File
@@ -0,0 +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
*/
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,
};
}
catch (error) {
console.log("Image Processing Error! =>", error.message);
return {
imageBase64: undefined,
imageBase64Full: undefined,
imageName: undefined,
imageSize: undefined,
};
}
});
}
+15
View File
@@ -0,0 +1,15 @@
type FunctionReturn = {
imageBase64?: string;
imageBase64Full?: string;
imageName?: string;
};
type Param = {
imageInput: HTMLInputElement;
maxWidth?: number;
mimeType?: string;
};
/**
* # Image Input Element to Base 64
*/
export default function imageInputToBase64({ imageInput, maxWidth, mimeType, }: Param): Promise<FunctionReturn>;
export {};
+90
View File
@@ -0,0 +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
*/
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,
};
}
else {
return {};
}
}
catch ( /** @type {*} */error) {
console.log("Image Processing Error! =>", error.message);
return {};
}
});
}
+21
View File
@@ -0,0 +1,21 @@
type FunctionReturn = {
fileBase64?: string;
fileBase64Full?: string;
fileName?: string;
fileSize?: number;
fileType?: string;
};
type Param = {
inputFile: File;
allowedRegex?: RegExp;
};
/**
* Input File to base64
* ==============================================================================
*
* @description This function takes in a *SINGLE* input file from a HTML file input element.
* HTML file input elements usually return an array of input objects, so be sure to select the target
* file from the array.
*/
export default function inputFileToBase64({ inputFile, allowedRegex, }: Param): Promise<FunctionReturn>;
export {};
+59
View File
@@ -0,0 +1,59 @@
"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
* ==============================================================================
*
* @description This function takes in a *SINGLE* input file from a HTML file input element.
* HTML file input elements usually return an array of input objects, so be sure to select the target
* file from the array.
*/
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,
};
}
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,
};
}
catch (error) {
console.log("File Processing Error! =>", error.message);
return {
fileName: inputFile.name,
};
}
});
}