Skip to content

Commit

Permalink
fix: Set Profile Icon for receive urls
Browse files Browse the repository at this point in the history
  • Loading branch information
icleitoncosta committed Jul 19, 2022
1 parent 08b06f4 commit c444938
Showing 1 changed file with 46 additions and 2 deletions.
48 changes: 46 additions & 2 deletions src/api/layers/group.layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@

import { Page } from 'puppeteer';
import { CreateConfig } from '../../config/create-config';
import { evaluateAndReturn } from '../helpers';
import {
evaluateAndReturn,
base64MimeType,
fileToBase64,
downloadFileToBase64,
} from '../helpers';
import { Id } from '../model';
import { GroupProperty } from '../model/enum';
import { RetrieverLayer } from './retriever.layer';
Expand Down Expand Up @@ -306,7 +311,46 @@ export class GroupLayer extends RetrieverLayer {
* @param base64 Image in base64 ( data:image/jpeg;base64,..... )
* @returns empty object
*/
public async setGroupIcon(groupId: string, base64: string) {
public async setGroupIcon(groupId: string, pathOrBase64: string) {
let base64: string = '';
if (pathOrBase64.startsWith('data:')) {
base64 = pathOrBase64;
} else {
let fileContent = await downloadFileToBase64(pathOrBase64, [
'image/gif',
'image/png',
'image/jpg',
'image/jpeg',
'image/webp',
]);
if (!fileContent) {
fileContent = await fileToBase64(pathOrBase64);
}
if (fileContent) {
base64 = fileContent;
}
}

if (!base64) {
const error = new Error('Empty or invalid file or base64');
Object.assign(error, {
code: 'empty_file',
});
throw error;
}

const mimeInfo = base64MimeType(base64);

if (!mimeInfo || !mimeInfo.includes('image')) {
const error = new Error(
'Not an image, allowed formats png, jpeg and webp'
);
Object.assign(error, {
code: 'invalid_image',
});
throw error;
}

return await evaluateAndReturn(
this.page,
({ groupId, base64 }) => WPP.group.setIcon(groupId, base64),
Expand Down

0 comments on commit c444938

Please sign in to comment.