Skip to content

Commit

Permalink
fix: Fixed send content from URL
Browse files Browse the repository at this point in the history
  • Loading branch information
edgardmessias committed Nov 20, 2020
1 parent b764893 commit 325538b
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 122 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,106 +52,38 @@ MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMNMMNMNMMMNMMNNMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMNNNNMMNNNMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
*/
import axios, { AxiosResponse } from 'axios';
import axios from 'axios';

export async function downloadFileImgHttp(
export async function downloadFileToBase64(
_path: string,
_mines: object
_mines: string[]
): Promise<string | false> {
if (typeof _mines !== 'object') {
console.error(`set mines object not "${typeof _mines}" `);
if (!Array.isArray(_mines)) {
console.error(`set mines string array, not "${typeof _mines}" `);
return false;
}

const _Path = {
Protocol: '^(https?:\\/\\/)g?',
Domain: '((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|',
IP: '((\\d{1,3}\\.){3}\\d{1,3}))',
Port: '(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*',
Query: '(\\?[;&a-z\\d%_.~+=-]*)?',
End: '(\\#[-a-z\\d_]*)?$',
Reg: () => {
return new RegExp(
_Path.Protocol +
_Path.Domain +
_Path.IP +
_Path.Port +
_Path.Query +
_Path.End,
'i'
);
},
tipes: {
i: 'image/png',
'/': 'image/jpg',
U: 'image/webp',
R: 'image/gif',
},
mineType: (_m: { [x: string]: any }) => {
var _b = {},
_obj = void 0;
for (let _i in _Path.tipes) {
for (let _k in _m) {
if (Object.values(_Path.tipes).indexOf(_m[_k]) <= -1) {
return `invalid mime type ${_m[_k]}`;
}
if (_Path.tipes[_i] == _m[_k]) {
_b[_i] = _Path.tipes[_i];
_obj = _b;
}
}
}
try {
const response = await axios.get(_path, {
responseType: 'arraybuffer',
});

return _obj;
},
XML: async (_path: any) => {
return new Promise<AxiosResponse<any>>(async (resolve, reject) => {
try {
var _f = await axios({
url: _path,
method: 'GET',
responseType: 'arraybuffer',
});
if (typeof _f === 'object' && _f.status == 200) {
resolve(_f);
} else {
throw new Error('invalid url');
}
} catch (e) {
reject(e);
}
});
},
};
const mimeType = response.headers['content-type'];
if (!_mines.includes(mimeType)) {
console.error(`Content-Type "${mimeType}" of ${_path} is not allowed`);
return false;
}

if (_Path.Reg().test(_path)) {
let _k = _Path.mineType(_mines);
let _final = await _Path
.XML(_path)
.then((_R) => {
//_R.data <Buffer 89 50 4e 47 0d 0a 1a 0a 00........
let __b64S = new Buffer(_R['data'], 'binary').toString('base64'),
_m = void 0;
for (let j in _k) {
if (j == __b64S.charAt(0)) {
_m = _k[j];
}
}
if (_m) {
return `data:${_m};base64,${__b64S}`;
} else {
return false;
}
})
.catch((_e) => {
console.error('Error url ', _e);
return false;
});
return _final as string | false;
} else {
return false;
const content = Buffer.from(response.data, 'binary').toString('base64');

return `data:${mimeType};base64,${content}`;
} catch (error) {
console.error(error);
}

return false;
}

export function MINES() {
const obj = [
'audio/aac',
Expand Down
2 changes: 1 addition & 1 deletion src/api/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
*/
export { fileToBase64 } from './file-to-base64';
export { base64MimeType } from './base64-mimetype';
export { downloadFileImgHttp, MINES } from './download-image-file';
export { downloadFileToBase64, MINES } from './download-file';
export { stickerSelect, resizeImg } from './select-sticker';
export { scrapeImgReload } from './scrape-img-reload';
export { scrapeImg } from './scrape-img-qr';
Expand Down
4 changes: 2 additions & 2 deletions src/api/layers/profile.layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ import { HostLayer } from './host.layer';
import {
base64MimeType,
fileToBase64,
downloadFileImgHttp,
downloadFileToBase64,
resizeImg,
} from '../helpers';

Expand Down Expand Up @@ -114,7 +114,7 @@ export class ProfileLayer extends HostLayer {
* @param name
*/
public async setProfilePic(path: string) {
let b64 = await downloadFileImgHttp(path, [
let b64 = await downloadFileToBase64(path, [
'image/png',
'image/jpg',
'image/webp',
Expand Down
65 changes: 36 additions & 29 deletions src/api/layers/sender.layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ import * as path from 'path';
import { Page } from 'puppeteer';
import {
base64MimeType,
downloadFileImgHttp,
downloadFileToBase64,
fileToBase64,
MINES,
stickerSelect,
Expand Down Expand Up @@ -156,7 +156,7 @@ export class SenderLayer extends ListenerLayer {
caption?: string
) {
return new Promise(async (resolve, reject) => {
let base64 = await downloadFileImgHttp(filePath, [
let base64 = await downloadFileToBase64(filePath, [
'image/png',
'image/jpg',
'image/webp',
Expand Down Expand Up @@ -331,6 +331,19 @@ export class SenderLayer extends ListenerLayer {
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);
}

filename = filenameFromMimeType(filename, mimeType);

var type = 'FileFromBase64';
var result = await this.page.evaluate(
({ to, base64, filename, caption, type }) => {
Expand All @@ -349,46 +362,40 @@ export class SenderLayer extends ListenerLayer {
/**
* Sends file from path
* @param to Chat id
* @param path File path
* @param filePath File path
* @param filename
* @param caption
*/
public async sendFile(
to: string,
path: string,
filename: string,
filePath: string,
filename?: string,
caption?: string
) {
return new Promise(async (resolve, reject) => {
var extension = path.split('.').pop();
filename = filename + '.' + extension;

let b64 = await downloadFileImgHttp(path, MINES()),
let base64 = await downloadFileToBase64(filePath, MINES()),
obj: { erro: boolean; to: string; text: string };
if (!b64) {
b64 = await fileToBase64(path);

if (!base64) {
base64 = await fileToBase64(filePath);
}
if (b64) {
var type = 'sendFile';
var result = await this.page.evaluate(
({ to, b64, filename, caption, type }) => {
return WAPI.sendFile(b64, to, filename, caption, type);
},
{ to, b64, filename, caption, type }
);
if (result['erro'] == true) {
reject(result);
} else {
resolve(result);
}
} else {

if (!base64) {
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.sendFileFromBase64(to, base64, filename, caption)
.then(resolve)
.catch(reject);
});
}

Expand Down Expand Up @@ -502,7 +509,7 @@ export class SenderLayer extends ListenerLayer {
* @param to chatId '[email protected]'
*/
public async sendImageAsStickerGif(to: string, path: string) {
let b64 = await downloadFileImgHttp(path, ['image/gif', 'image/webp']);
let b64 = await downloadFileToBase64(path, ['image/gif', 'image/webp']);
if (!b64) {
b64 = await fileToBase64(path);
}
Expand Down Expand Up @@ -545,7 +552,7 @@ export class SenderLayer extends ListenerLayer {
* @param to chatId '[email protected]'
*/
public async sendImageAsSticker(to: string, path: string) {
let b64 = await downloadFileImgHttp(path, [
let b64 = await downloadFileToBase64(path, [
'image/png',
'image/jpg',
'image/webp',
Expand Down

0 comments on commit 325538b

Please sign in to comment.