fix: normalize audio/opus content type to audio/ogg for WhatsApp attachments#223
Conversation
📝 WalkthroughWalkthroughThe pull request consolidates audio MIME type normalization logic from the WhatsApp service into the Attachment model and ActiveStorage initialization layer. A new helper method normalizes audio/opus to audio/ogg content types, invoked both when accessing download URLs and globally via an ActiveStorage patch, with the WhatsApp-specific normalization removed. Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
@coderabbitai review |
✅ Actions performedReview triggered.
|
There was a problem hiding this comment.
Pull request overview
This PR fixes an issue where the Marcel gem detects OGG files with Opus codec as audio/opus instead of audio/ogg, causing problems with WhatsApp Cloud API which expects audio/ogg for OGG container files. The fix implements a two-pronged approach: an ActiveStorage initializer to prevent new files from being persisted with the wrong content type, and lazy normalization in the Attachment model to fix existing blobs.
Changes:
- Added an ActiveStorage initializer to normalize
audio/opustoaudio/oggduring content type identification for.oggfiles - Moved content type normalization from WhatsApp service to the Attachment model's
download_urlmethod for broader applicability - Removed the service-specific normalization method from WhatsApp Cloud service
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| config/initializers/active_storage_opus_fix.rb | New initializer that prepends a module to ActiveStorage::Blob to normalize audio/opus → audio/ogg during content type identification for .ogg files |
| app/models/attachment.rb | Added normalize_opus_blob_content_type! method and integrated it into download_url to lazily normalize existing blobs |
| app/services/whatsapp/providers/whatsapp_cloud_service.rb | Removed the normalize_opus_content_type method as normalization now happens in the Attachment model |
| spec/models/attachment_spec.rb | Added test to verify audio/opus normalization to audio/ogg when accessing download_url |
| spec/services/whatsapp/providers/whatsapp_cloud_service_spec.rb | Added test to verify normalization works correctly with WhatsApp voice messages |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@app/models/attachment.rb`:
- Around line 191-199: The check in normalize_opus_blob_content_type! currently
uses blob.filename.to_s.end_with?('.ogg') which is case-sensitive and will miss
CAPITALIZED extensions; change the conditional to perform a case-insensitive
extension check (for example compare a downcased filename or use
case-insensitive extname comparison) so that filenames like "sample.OGG" are
detected and blob.update_column(:content_type, 'audio/ogg') still runs for those
cases.
ℹ️ Review info
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (5)
app/models/attachment.rbapp/services/whatsapp/providers/whatsapp_cloud_service.rbconfig/initializers/active_storage_opus_fix.rbspec/models/attachment_spec.rbspec/services/whatsapp/providers/whatsapp_cloud_service_spec.rb
💤 Files with no reviewable changes (1)
- app/services/whatsapp/providers/whatsapp_cloud_service.rb
Pull Request Template
Description
Please include a summary of the change and issue(s) fixed. Also, mention relevant motivation, context, and any dependencies that this change requires.
Fixes # (issue)
Type of change
Please delete options that are not relevant.
How Has This Been Tested?
Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration.
Checklist:
This change is
Summary by CodeRabbit
Release Notes
Bug Fixes
Tests