-
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
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.
Reviewer's GuideIntroduces an image preprocessing pipeline in the Baileys WhatsApp service: images are downloaded or decoded, converted to JPEG via sharp (with optional proxy support), default filename and mimetype updated to .jpg/jpeg, and the mediaInput is unified in the prepareWAMessageMedia call. Sequence diagram for image preprocessing and sending in WhatsApp Baileys integrationsequenceDiagram
participant Service as BaileysStartupService
participant Proxy as Proxy (optional)
participant Axios as axios
participant Sharp as sharp
participant WA as WhatsApp API
Service->>Axios: Download image from URL (with proxy if enabled)
Axios-->>Service: Return image buffer
Service->>Sharp: Convert image buffer to JPEG
Sharp-->>Service: Return JPEG buffer
Service->>WA: Send JPEG image with updated filename/mimetype
Class diagram for BaileysStartupService image handling changesclassDiagram
class BaileysStartupService {
+localProxy
+client
+sendMediaMessage(mediaMessage)
}
class mediaMessage {
+mediatype
+media
+fileName
+mimetype
}
BaileysStartupService --> mediaMessage : uses
class ProxyConfig {
+host
+port
+protocol
+username
+password
}
BaileysStartupService --> ProxyConfig : uses (if enabled)
class sharp {
+jpeg()
+toBuffer()
}
BaileysStartupService --> sharp : converts image to JPEG
class axios {
+get(url, config)
}
BaileysStartupService --> axios : downloads image
class prepareWAMessageMedia {
+upload
}
BaileysStartupService --> prepareWAMessageMedia : prepares media for WhatsApp
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
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.
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.
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.
| } | ||
|
|
||
| mediaInput = await sharp(imageBuffer).jpeg().toBuffer(); | ||
| mediaMessage.fileName ??= 'image.jpg'; |
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.
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.
|
Aguardando o pessoal da evo aceita o pull pois a situação tá bem no envio de imagens. |
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: