@devix-tecnologia/value-objects
Version:
Coleção de objetos de valores para implementação em sistemas.
133 lines (132 loc) • 4.02 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ImagemBase64 = void 0;
const image_formats_js_1 = require("./image-formats.js");
/**
* Cria objeto de valor para imagens em base64.
* Valida se a string fornecida é uma imagem base64 válida.
* Detecta o formato da imagem.
* Aceita tanto strings base64 puras quanto data URIs.
*/
class ImagemBase64 {
constructor(content) {
this._content = '';
this._format = 'unknown';
this._isValid = false;
this._isDataUri = false;
this._content = content;
this.validate();
}
get type() {
return 'BASE64_IMAGE';
}
get version() {
return ImagemBase64.VERSION;
}
get validation() {
return 'Validação de string base64 e assinatura de formato de imagem';
}
get formatted() {
return this.DataUri;
}
get format() {
this.isValid({ raiseException: true });
return this._format;
}
get content() {
this.isValid({ raiseException: true });
return this._isDataUri ? this._content.split(',')[1] : this._content;
}
get formatInfo() {
this.isValid({ raiseException: true });
return image_formats_js_1.imageFormats[this._format];
}
get DataUri() {
this.isValid({ raiseException: true });
if (this._isDataUri) {
return this._content;
}
return `data:image/${this._format.toLowerCase()};base64,${this._content}`;
}
toJSON() {
if (!this._isValid) {
return {
type: this.type,
isValid: false,
version: this.version,
};
}
return {
type: this.type,
formatted: this.formatted,
format: this._format,
isValid: true,
version: this.version,
mimeType: `image/${this._format.toLowerCase()}`,
isDataUri: this._isDataUri,
};
}
toString() {
if (!this._isValid)
return '';
return this.formatted;
}
isValid(config = { raiseException: false }) {
if (config.raiseException && !this._isValid) {
throw new Error('Invalid base64 image');
}
return this._isValid;
}
equals(other) {
if (!this._isValid)
return false;
try {
const otherImage = other instanceof ImagemBase64
? other
: new ImagemBase64(other);
if (!otherImage.isValid())
return false;
return this.content === otherImage.content;
}
catch (_a) {
return false;
}
}
validate() {
if (!this._content || typeof this._content !== 'string') {
return;
}
// Verifica se é data URI
if (this._content.startsWith('data:image/')) {
this._isDataUri = true;
const [header, base64Content] = this._content.split(',');
if (!base64Content || !this.isBase64String(base64Content)) {
return;
}
const format = header.split('data:image/')[1].split(';')[0].toUpperCase();
this._format = format === 'JPG' ? 'JPEG' : format;
this._isValid = true;
return;
}
// Verifica se é base64 puro
if (!this.isBase64String(this._content)) {
return;
}
// Detecta o formato
this._format = (0, image_formats_js_1.findFormatByPrefix)(this._content);
this._isValid = this._format !== 'unknown';
}
isBase64String(str) {
if (!str)
return false;
try {
const regex = /^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)?$/;
return regex.test(str.trim());
}
catch (_a) {
return false;
}
}
}
exports.ImagemBase64 = ImagemBase64;
ImagemBase64.VERSION = '1.0.0';