Skip to content

Adds support for HTTP URLs for documents, audio, images, and sticky notes…#283

Open
cleitonme wants to merge 1 commit intoasternic:mainfrom
cleitonme:suporteurl
Open

Adds support for HTTP URLs for documents, audio, images, and sticky notes…#283
cleitonme wants to merge 1 commit intoasternic:mainfrom
cleitonme:suporteurl

Conversation

@cleitonme
Copy link
Copy Markdown
Contributor

Previously, the system only supported media using Base64 encoding for documents, audio, images, and stickers.

This update introduces support for public HTTP/HTTPS URLs, allowing media to be sent directly from a link.


🔄 Changes

  • ✅ Base64 support remains unchanged (fully compatible)

  • 🆕 Added support for direct URLs for:

    • 📄 Documents
    • 🎧 Audio
    • 🖼️ Images
    • 🏷️ Stickers

⚡ Benefits

  • 🚀 Improved performance (no Base64 conversion required)
  • 💾 Reduced memory and CPU usage
  • 🔗 Easier integration with externally hosted files
  • 📦 Faster media sending, especially for large files

📌 Notes

  • URLs must be publicly accessible
  • The server must allow direct file download
  • Base64 format is still supported and was not removed

💡 Example

Before (Base64):

{
  "file": "data:application/pdf;base64,JVBERi0xLjcKJc..."
}

Now (URL):

{
  "file": "https://example.com/file.pdf"
}

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request enhances the media sending capabilities by allowing documents, audio, and stickers to be fetched directly from HTTP URLs. It also introduces a media caching layer for document and audio uploads and updates several file size limits. Review feedback identifies a critical compilation error in the audio handler where the mime variable is used before declaration. Additionally, it is recommended to use the request context instead of a background context for upload operations to support proper request cancellation.

Comment on lines +1063 to +1073
mime = ct
}
// else mantém o default já definido (ogg ou mpeg)
}
filedata = data
} else {
s.Respond(w, r, http.StatusBadRequest, errors.New("audio data should start with \"data:audio/\""))
s.Respond(w, r, http.StatusBadRequest, errors.New("audio data should start with \"data:audio/\" or be a valid HTTP URL"))
return
}

uploaded, err = mediaCache.GetOrUploadAudio(context.Background(), clientManager.GetWhatsmeowClient(txtid), filedata, mime)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

This block contains a critical issue: the variable mime is used at lines 1063 and 1073 but is not declared until line 1086. This will result in a compilation error (undefined: mime).

Furthermore, the mime type resolution logic (including the defaults based on the PTT setting) should be executed before the mediaCache.GetOrUploadAudio call to ensure the correct metadata is used during the upload. Also, consider using r.Context() instead of context.Background() for the upload call to respect request cancellation.

return
}

uploaded, err = mediaCache.GetOrUploadDocument(context.Background(), clientManager.GetWhatsmeowClient(txtid), filedata, func() string {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Use r.Context() instead of context.Background() when calling mediaCache.GetOrUploadDocument. This allows the upload process to be cancelled if the HTTP request is closed by the client, which is a best practice for handling long-running operations in web handlers.

Suggested change
uploaded, err = mediaCache.GetOrUploadDocument(context.Background(), clientManager.GetWhatsmeowClient(txtid), filedata, func() string {
uploaded, err = mediaCache.GetOrUploadDocument(r.Context(), clientManager.GetWhatsmeowClient(txtid), filedata, func() string {

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.

1 participant