Skip to content

Commit

Permalink
feat: Send image from base64 (#319)
Browse files Browse the repository at this point in the history
  • Loading branch information
edgardmessias authored Nov 16, 2020
1 parent 879606d commit 9085132
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 39 deletions.
12 changes: 8 additions & 4 deletions src/api/helpers/download-image-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string | false> {
if (typeof _mines !== 'object') {
console.error(`set mines object not "${typeof _mines}" `);
return false;
Expand Down Expand Up @@ -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<AxiosResponse<any>>(async (resolve, reject) => {
try {
var _f = await axios({
url: _path,
Expand Down Expand Up @@ -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;
}
Expand Down
71 changes: 71 additions & 0 deletions src/api/helpers/filename-from-mimetype.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN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Md d- mNh-`.`+mN/ yMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMN/ . +NMMN- oNMMMMMNdN. dMMMd:`/. ds.dNMMMMMMm::M- dMMNy/dMMN/ yMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMN/ +NMMMN- /yyyyyys d. dMMMMNo` dNy-+ymmmho-+NN- dMMMMMMMMN/ yMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMNyNMMMMN+::::::::::m+/mMMMMMMd: dMMNho///+ymMMN+/mMMMMMMMMNs/hMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMNMMMMMMMMMMMMMMMMMMMMMMMMMMMMNsmMMMMMMMMMMMMMMNNNNMMNNNMMNNNNMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMNMMMMMMMMMMMMMMNMMNMNMMMNMMNNMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMNMMNMNMMMNMMNNMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMNNNNMMNNNMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
*/

import * as path from 'path';
import * as mime from 'mime-types';

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;
}
108 changes: 73 additions & 35 deletions src/api/layers/sender.layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
}
});
}
Expand Down

0 comments on commit 9085132

Please sign in to comment.