-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Feat/whatsapp/convert-to-jpeg #1884
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2448,9 +2448,43 @@ export class BaileysStartupService extends ChannelStartupService { | |
| try { | ||
| const type = mediaMessage.mediatype === 'ptv' ? 'video' : mediaMessage.mediatype; | ||
|
|
||
| let mediaInput: any; | ||
| if (mediaMessage.mediatype === 'image') { | ||
| let imageBuffer: Buffer; | ||
| if (isURL(mediaMessage.media)) { | ||
| let config: any = { responseType: 'arraybuffer' }; | ||
|
|
||
| if (this.localProxy?.enabled) { | ||
| config = { | ||
| ...config, | ||
| httpsAgent: makeProxyAgent({ | ||
| host: this.localProxy.host, | ||
| port: this.localProxy.port, | ||
| protocol: this.localProxy.protocol, | ||
| username: this.localProxy.username, | ||
| password: this.localProxy.password, | ||
| }), | ||
| }; | ||
| } | ||
|
|
||
| const response = await axios.get(mediaMessage.media, config); | ||
| imageBuffer = Buffer.from(response.data, 'binary'); | ||
| } else { | ||
| imageBuffer = Buffer.from(mediaMessage.media, 'base64'); | ||
| } | ||
|
|
||
| mediaInput = await sharp(imageBuffer).jpeg().toBuffer(); | ||
| mediaMessage.fileName ??= 'image.jpg'; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: Redundant fileName assignment logic exists below. Consolidate the fileName assignment to a single location to improve clarity and maintain consistency. Suggested implementation: mediaInput = await sharp(imageBuffer).jpeg().toBuffer();
mediaMessage.fileName ??= 'image.jpg';
mediaMessage.mimetype = 'image/jpeg';
} else {
mediaInput = isURL(mediaMessage.media)
? { url: mediaMessage.media }
: Buffer.from(mediaMessage.media, 'base64');If there is another assignment to |
||
| mediaMessage.mimetype = 'image/jpeg'; | ||
| } else { | ||
| mediaInput = isURL(mediaMessage.media) | ||
| ? { url: mediaMessage.media } | ||
| : Buffer.from(mediaMessage.media, 'base64'); | ||
| } | ||
|
|
||
| const prepareMedia = await prepareWAMessageMedia( | ||
| { | ||
| [type]: isURL(mediaMessage.media) ? { url: mediaMessage.media } : Buffer.from(mediaMessage.media, 'base64'), | ||
| [type]: mediaInput, | ||
| } as any, | ||
| { upload: this.client.waUploadToServer }, | ||
| ); | ||
|
|
@@ -2464,7 +2498,7 @@ export class BaileysStartupService extends ChannelStartupService { | |
| } | ||
|
|
||
| if (mediaMessage.mediatype === 'image' && !mediaMessage.fileName) { | ||
| mediaMessage.fileName = 'image.png'; | ||
| mediaMessage.fileName = 'image.jpg'; | ||
| } | ||
|
|
||
| if (mediaMessage.mediatype === 'video' && !mediaMessage.fileName) { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
issue: Network errors from axios.get are not explicitly handled.
Catch errors from axios.get to prevent unhandled promise rejections when the image URL is unreachable or returns a non-200 status.