Feat/whatsapp/convert-to-jpeg#1884
Conversation
All images sent via the Baileys integration are now pre-processed and converted to JPEG format using the `sharp` library. This ensures better compatibility and prevents potential issues with unsupported formats. - Images from URLs are now downloaded via axios before processing, which allows for the use of a proxy. - The default filename and mimetype are updated to `image.jpg` and `image/jpeg` to reflect the conversion.
There was a problem hiding this comment.
Hey there - I've reviewed your changes and they look great!
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location> `src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts:2470` </location>
<code_context>
+ };
+ }
+
+ const response = await axios.get(mediaMessage.media, config);
+ imageBuffer = Buffer.from(response.data, 'binary');
+ } else {
</code_context>
<issue_to_address>
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.
</issue_to_address>
### Comment 2
<location> `src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts:2477` </location>
<code_context>
+ }
+
+ mediaInput = await sharp(imageBuffer).jpeg().toBuffer();
+ mediaMessage.fileName ??= 'image.jpg';
+ mediaMessage.mimetype = 'image/jpeg';
+ } else {
</code_context>
<issue_to_address>
Redundant fileName assignment logic exists below.
Consolidate the fileName assignment to a single location to improve clarity and maintain consistency.
Suggested implementation:
```typescript
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.fileName` elsewhere in this function or code path, it should be removed to ensure the assignment is consolidated to this single location.
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
| }; | ||
| } | ||
|
|
||
| const response = await axios.get(mediaMessage.media, config); |
There was a problem hiding this comment.
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.
| } | ||
|
|
||
| mediaInput = await sharp(imageBuffer).jpeg().toBuffer(); | ||
| mediaMessage.fileName ??= 'image.jpg'; |
There was a problem hiding this comment.
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.fileName elsewhere in this function or code path, it should be removed to ensure the assignment is consolidated to this single location.

All images sent via the Baileys integration are now pre-processed and converted to JPEG format using the
sharplibrary. This ensures better compatibility and prevents potential issues with unsupported formats.image.jpgandimage/jpegto reflect the conversion.Summary by Sourcery
Preprocess WhatsApp image attachments by fetching them via axios (respecting local proxy settings), converting buffers to JPEG using sharp, and updating file metadata to ensure compatibility.
New Features:
Enhancements: