From 90851322e9cedddb16299dbc93c675af084033c0 Mon Sep 17 00:00:00 2001 From: Edgard Lorraine Messias Date: Mon, 16 Nov 2020 10:43:58 -0300 Subject: [PATCH] feat: Send image from base64 (#319) --- src/api/helpers/download-image-file.ts | 12 ++- src/api/helpers/filename-from-mimetype.ts | 71 ++++++++++++++ src/api/layers/sender.layer.ts | 108 +++++++++++++++------- 3 files changed, 152 insertions(+), 39 deletions(-) create mode 100644 src/api/helpers/filename-from-mimetype.ts diff --git a/src/api/helpers/download-image-file.ts b/src/api/helpers/download-image-file.ts index 1e65cae7f..0a17ae6fe 100644 --- a/src/api/helpers/download-image-file.ts +++ b/src/api/helpers/download-image-file.ts @@ -52,9 +52,12 @@ MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMNMMNMNMMMNMMNNMMMMMMMMMMMM MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMNNNNMMNNNMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM */ -import axios from 'axios'; +import axios, { AxiosResponse } from 'axios'; -export async function downloadFileImgHttp(_path: string, _mines: object) { +export async function downloadFileImgHttp( + _path: string, + _mines: object +): Promise { if (typeof _mines !== 'object') { console.error(`set mines object not "${typeof _mines}" `); return false; @@ -102,7 +105,7 @@ export async function downloadFileImgHttp(_path: string, _mines: object) { return _obj; }, XML: async (_path: any) => { - return new Promise(async (resolve, reject) => { + return new Promise>(async (resolve, reject) => { try { var _f = await axios({ url: _path, @@ -142,8 +145,9 @@ export async function downloadFileImgHttp(_path: string, _mines: object) { }) .catch((_e) => { console.error('Error url ', _e); + return false; }); - return _final; + return _final as string | false; } else { return false; } diff --git a/src/api/helpers/filename-from-mimetype.ts b/src/api/helpers/filename-from-mimetype.ts new file mode 100644 index 000000000..688f256f4 --- /dev/null +++ b/src/api/helpers/filename-from-mimetype.ts @@ -0,0 +1,71 @@ +/* +NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN +MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM +mMMMMMMMMMNNNmmNNNMMNMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM +mmNMMNMMMMNNNNNmmmddhdddNMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM +mddNMMNy:/odNmmddmmNNmdhhddmNMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM +NmmdNMNd:--+dNmmddhhddmmhsyhhmdmmNMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM +NmNmdNmy:.-oyNmmmhmdhho+sososyhhhddNMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM +NmmNdh+-`.:oyNNdmmdmmdo-://oysssyhhhdmNMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM +Nmmmoyyyo+osdNmdmmddNNhs+/::/+osyssydyhdNMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM +NNmhsymMMNmmmmdmdNNddNmsso+++////ossssyyhdmNMMMMMMMMMMMMMMMMMMMMM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export function filenameFromMimeType( + filename: string, + mimeType: string +): string { + const filenameExtension = path.extname(filename); + const mimeExtension = mime.extension(mimeType); + + if (!mimeExtension || filenameExtension === mimeExtension) { + return filename; + } + + return path.basename(filename, filenameExtension) + '.' + mimeExtension; +} diff --git a/src/api/layers/sender.layer.ts b/src/api/layers/sender.layer.ts index a3ea3f454..a215b5dc2 100644 --- a/src/api/layers/sender.layer.ts +++ b/src/api/layers/sender.layer.ts @@ -52,14 +52,16 @@ MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMNMMNMNMMMNMMNNMMMMMMMMMMMM MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMNNNNMMNNNMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM */ +import * as path from 'path'; import { Page } from 'puppeteer'; import { base64MimeType, - fileToBase64, downloadFileImgHttp, - stickerSelect, + fileToBase64, MINES, + stickerSelect, } from '../helpers'; +import { filenameFromMimeType } from '../helpers/filename-from-mimetype'; import { Chat, Message } from '../model'; import { ChatState } from '../model/enum'; import { ListenerLayer } from './listener.layer'; @@ -227,56 +229,92 @@ export class SenderLayer extends ListenerLayer { /** * Sends image message * @param to Chat id - * @param imgBase64 + * @param filePath File path or http link * @param filename * @param caption */ public async sendImage( to: string, - path: string, - filename: string, + filePath: string, + filename?: string, caption?: string ) { return new Promise(async (resolve, reject) => { - let data = await downloadFileImgHttp(path, [ + let base64 = await downloadFileImgHttp(filePath, [ 'image/png', 'image/jpg', 'image/webp', ]); - if (!data) { - data = await fileToBase64(path); + + if (!base64) { + base64 = await fileToBase64(filePath); } - var extension = path.split('.').pop(); - filename = filename + '.' + extension; - if (data) { - const mimeInfo = base64MimeType(data); - if (!mimeInfo || mimeInfo.includes('image')) { - var result = await this.page.evaluate( - ({ to, data, filename, caption }) => { - return WAPI.sendImage(data, to, filename, caption); - }, - { to, data, filename, caption } - ); - if (result['erro'] == true) { - reject(result); - } else { - resolve(result); - } - } else { - var obj = { - erro: true, - to: to, - text: 'Not an image, allowed formats png, jpeg and webp', - }; - reject(obj); - } - } else { + + if (!base64) { var obj = { erro: true, to: to, - text: 'No such file or directory, open "' + path + '"', + text: 'No such file or directory, open "' + filePath + '"', }; - reject(obj); + return reject(obj); + } + + if (!filename) { + filename = path.basename(filePath); + } + + this.sendImageFromBase64(to, base64, filename, caption) + .then(resolve) + .catch(reject); + }); + } + + /** + * Sends image message + * @param to Chat id + * @param base64 File path, http link or base64Encoded + * @param filename + * @param caption + */ + public async sendImageFromBase64( + to: string, + base64: string, + filename: string, + caption?: string + ) { + return new Promise(async (resolve, reject) => { + let mimeType = base64MimeType(base64); + + if (!mimeType) { + var obj = { + erro: true, + to: to, + text: 'Invalid base64!', + }; + return reject(obj); + } + + if (!mimeType.includes('image')) { + var obj = { + erro: true, + to: to, + text: 'Not an image, allowed formats png, jpeg and webp', + }; + return reject(obj); + } + + filename = filenameFromMimeType(filename, mimeType); + + var result = await this.page.evaluate( + ({ to, base64, filename, caption }) => { + return WAPI.sendImage(base64, to, filename, caption); + }, + { to, base64, filename, caption } + ); + if (result['erro'] == true) { + reject(result); + } else { + resolve(result); } }); }