Skip to content

Commit

Permalink
feat: check values sendImageFromBase64 - ts
Browse files Browse the repository at this point in the history
  • Loading branch information
jonalan7 committed Mar 17, 2021
1 parent e03edc1 commit f44dc08
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 50 deletions.
2 changes: 1 addition & 1 deletion src/api/helpers/layers-interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export function checkValuesSender(data: any) {
let result =
data[i].type && data[i].value && infoType(data[i].value, data[i].type)
? true
: false;
: !data[i].value && data[i].isUser === false ? true : false;
if (!result) {
return {
erro: true,
Expand Down
135 changes: 86 additions & 49 deletions src/api/layers/sender.layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,14 @@ export class SenderLayer extends ListenerLayer {
type: type,
value: to,
function: typeFunction,
isUser: true,
},
{
param: 'content',
type: type,
value: content,
function: typeFunction,
isUser: true,
},
];
const validating = checkValuesSender(check);
Expand Down Expand Up @@ -141,18 +143,21 @@ export class SenderLayer extends ListenerLayer {
type: type,
value: chatId,
function: typeFunction,
isUser: true,
},
{
param: 'url',
type: type,
value: url,
function: typeFunction,
isUser: true,
},
{
param: 'title',
type: type,
value: title,
function: typeFunction,
isUser: false,
},
];
const validating = checkValuesSender(check);
Expand All @@ -173,6 +178,87 @@ export class SenderLayer extends ListenerLayer {
});
}

/**
* 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
): Promise<SendFileResult> {
return new Promise(async (resolve, reject) => {
const typeFunction = 'sendImageFromBase64';
const type = 'string';
const check = [
{
param: 'to',
type: type,
value: to,
function: typeFunction,
isUser: true,
},
{
param: 'base64',
type: type,
value: base64,
function: typeFunction,
isUser: true,
},
{
param: 'filename',
type: type,
value: filename,
function: typeFunction,
isUser: false,
},
];
const validating = checkValuesSender(check);
if (typeof validating === 'object') {
return reject(validating);
}

let mimeType = base64MimeType(base64);

if (!mimeType) {
const obj = {
erro: true,
to: to,
text: 'Invalid base64!',
};
return reject(obj);
}

if (!mimeType.includes('image')) {
const obj = {
erro: true,
to: to,
text: 'Not an image, allowed formats png, jpeg and webp',
};
return reject(obj);
}

filename = filenameFromMimeType(filename, mimeType);

const 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);
}
});
}


public async sendMessageOptions(
chat: any,
content: any,
Expand Down Expand Up @@ -242,55 +328,6 @@ export class SenderLayer extends ListenerLayer {
});
}

/**
* 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
): Promise<SendFileResult> {
return new Promise(async (resolve, reject) => {
let mimeType = base64MimeType(base64);

if (!mimeType) {
const obj = {
erro: true,
to: to,
text: 'Invalid base64!',
};
return reject(obj);
}

if (!mimeType.includes('image')) {
const obj = {
erro: true,
to: to,
text: 'Not an image, allowed formats png, jpeg and webp',
};
return reject(obj);
}

filename = filenameFromMimeType(filename, mimeType);

const 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);
}
});
}

/**
* Sends message with thumbnail
Expand Down

0 comments on commit f44dc08

Please sign in to comment.