Adds support for HTTP URLs for documents, audio, images, and sticky notes…#283
Adds support for HTTP URLs for documents, audio, images, and sticky notes…#283cleitonme wants to merge 1 commit intoasternic:mainfrom
Conversation
There was a problem hiding this comment.
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.
| 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) |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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.
| uploaded, err = mediaCache.GetOrUploadDocument(context.Background(), clientManager.GetWhatsmeowClient(txtid), filedata, func() string { | |
| uploaded, err = mediaCache.GetOrUploadDocument(r.Context(), clientManager.GetWhatsmeowClient(txtid), filedata, func() string { |
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:
⚡ Benefits
📌 Notes
💡 Example
Before (Base64):
{ "file": "data:application/pdf;base64,JVBERi0xLjcKJc..." }Now (URL):
{ "file": "https://example.com/file.pdf" }