Skip to content

Conversation

@nestordavalos
Copy link
Contributor

@nestordavalos nestordavalos commented Aug 28, 2025

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.

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:

  • Convert all WhatsApp image media to JPEG format using sharp before sending
  • Download remote images via axios with optional proxy support for preprocessing

Enhancements:

  • Set default image filename to image.jpg and mimetype to image/jpeg after conversion

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.
@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented Aug 28, 2025

Reviewer's Guide

Introduces 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 integration

sequenceDiagram
    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
Loading

Class diagram for BaileysStartupService image handling changes

classDiagram
    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
Loading

File-Level Changes

Change Details Files
Image preprocessing pipeline with axios download, proxy support, and JPEG conversion
  • Added conditional handling for mediaMessage.mediatype === 'image'
  • Downloaded remote images via axios with optional proxy agent
  • Decoded base64 media when not a URL
  • Converted image buffers to JPEG using sharp
  • Set default fileName to 'image.jpg' and mimetype to 'image/jpeg'
src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts
Unified media input usage in prepareWAMessageMedia
  • Introduced mediaInput variable to hold processed or raw media
  • Replaced inline media URL/base64 object with mediaInput in prepareWAMessageMedia invocation
src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts
Updated default image filename extension to .jpg
  • Changed fallback filename in post-processing from 'image.png' to 'image.jpg'
src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a 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>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
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);
Copy link
Contributor

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';
Copy link
Contributor

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.

@bergpinheiro
Copy link
Contributor

Aguardando o pessoal da evo aceita o pull pois a situação tá bem no envio de imagens.

@DavidsonGomes DavidsonGomes merged commit 78f7618 into EvolutionAPI:develop Aug 29, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants