@wppconnect-team/wppconnect
Version:
WPPConnect is an open source project developed by the JavaScript community with the aim of exporting functions from WhatsApp Web to the node, which can be used to support the creation of any interaction, such as customer service, media sending, intelligen
91 lines (90 loc) • 3.26 kB
JavaScript
;
/*
* This file is part of WPPConnect.
*
* WPPConnect is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* WPPConnect is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with WPPConnect. If not, see <https://www.gnu.org/licenses/>.
*/
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.fileToBase64 = fileToBase64;
exports.Mine = Mine;
const mimeTypes = __importStar(require("mime-types"));
const fileType = __importStar(require("file-type"));
const fs = __importStar(require("fs"));
/**
* Converts given file into base64 string
* @param path file path
* @param mime Optional, will retrieve file mime automatically if not defined (Example: 'image/png')
*/
async function fileToBase64(path, mime) {
if (fs.existsSync(path)) {
const base64 = fs.readFileSync(path, { encoding: 'base64' });
if (mime === undefined) {
mime = mimeTypes.lookup(path);
}
if (!mime) {
const result = await fileType.fromFile(path);
mime = result?.mime;
}
if (!mime) {
mime = 'application/octet-stream';
}
const data = `data:${mime};base64,${base64}`;
return data;
}
else {
return false;
}
}
async function Mine(path) {
if (fs.existsSync(path)) {
const mime = await mimeTypes.lookup(path);
return mime;
}
else {
return false;
}
}