fix(backend): correct M4A embedded cover extraction and validation#3705
Conversation
MP4Cover.FORMAT_PNG was referenced via a non-existent MP4.Cover attribute, breaking cover extraction for m4a files. Add unit tests and guard extract_embedded_cover so failures do not abort the upload path.
Greptile SummaryThis PR fixes embedded audio cover extraction and persistence. The main changes are:
Confidence Score: 4/5This is close, but the cover failure path should be fixed before merging.
backend/utils/audio_tags.py Important Files Changed
Reviews (2): Last reviewed commit: "fix(backend): validate embedded cover fo..." | Re-trigger Greptile |
…ailures Restrict extracted audio cover bytes to JPEG/PNG via magic-byte sniffing instead of trusting the declared MP4 imageformat, so a GIF (or missing format) is no longer mislabeled and persisted as image/jpeg.
| try: | ||
| cover = extract_embedded_cover(audio_full_path) | ||
| except Exception as exc: |
There was a problem hiding this comment.
When extract_embedded_cover raises after the tag scan reported an embedded cover, this catch returns None, the same value used for a file with no cover. The upload and scan paths then set has_embedded_cover=False, so a track with an unreadable or unexpectedly failing embedded cover is still saved successfully with the cover removed from metadata. That keeps the extraction-failure case indistinguishable from the no-cover case for callers.
Description
When uploading a soundtrack in
.m4aformat, the embedded cover doesn't show up because has error extracting the cover.This PR Fixes embedded soundtrack cover handling for
.m4aand other audio formats._extract_picture_from_mp4referenced a non-existentMP4.Coverattribute, causing cover extraction to fail on valid M4A files. Cover MIME is now derived from the actual image bytes instead of broken or unreliable container metadata.image/jpegwith a.jpgextension, so clients could not render them. Embedded cover bytes are now validated via magic-byte sniffing; only JPEG and PNG are accepted.has_embedded_coverwas true but extraction or persistence failed, the track was saved with missing cover art and incorrect metadata. Upload and scan callers now log the failure and sethas_embedded_cover=False.Checklist
Screenshots (if applicable)
N/A (backend-only fix)
AI assistance disclosure
This PR was developed with Cursor AI assistance (writing unit tests and MIME type detection).
I verified the fix against real M4A, FLAC, MP3, OGG and WAV files.