Skip to content

fix(audiobooks): extract scan covers and harden dedupe - #103

Merged
Quick104 merged 2 commits into
mainfrom
fix/audiobook-local-cover-scan
Jun 16, 2026
Merged

Quick104 merged 2 commits into
mainfrom
fix/audiobook-local-cover-scan

Conversation

@RXWatcher

@RXWatcher RXWatcher commented Jun 9, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Extract embedded audiobook covers during the initial scanner reconcile, without overwriting an existing sidecar/enriched poster.
  • Harden scan-time audiobook dedupe for case-only file path differences within the same library.
  • Normalize punctuation in the existing strict title dedupe path while still requiring same author, narrator, year, and near-identical duration.

Test Plan

  • go test ./internal/scanner -count=1

Summary by CodeRabbit

  • New Features

    • Automatic extraction and storage of cover art embedded in audiobook files when no cover exists.
  • Improvements

    • Enhanced audiobook deduplication with smarter, normalized title fuzzy matching (including punctuation/formatting tolerance).
    • Tighter duplicate detection across folders using case-insensitive file-path matching scoped to the folder.
    • Added a database index on media_files(media_folder_id, lower(file_path)) to speed lookups.
  • Tests

    • Added/expanded tests for embedded-cover extraction behavior, title matching/deduplication edge cases, and case-folded path-lookup variants.

@coderabbitai

coderabbitai Bot commented Jun 9, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

Caution

Review failed

Pull request was closed or merged during review

📝 Walkthrough

Walkthrough

This PR enhances audiobook library scanning by introducing normalized title-based deduplication, constraining file-path matching to media folders with case-insensitive lookup, and adding embedded cover extraction from audio files. The changes improve duplicate detection accuracy and enrich metadata without altering public APIs.

Changes

Audiobook Deduplication and Embedded Cover Handling

Layer / File(s) Summary
Title Normalization and Fuzzy Matching Helpers
internal/scanner/audiobook.go
Introduces regex and helper functions normalizeAudiobookDedupeTitle and audiobookDedupeTitlesMatch to normalize titles by stripping narrator/unabridged suffixes, replacing non-alphanumeric runs with spaces, collapsing whitespace, and lowercasing, then comparing normalized titles using exact or prefix-containment rules.
File-Path Deduplication with Folder Scoping
internal/scanner/audiobook_scan.go
Updates upsertAudiobookMediaItem call site, modifies findAudiobookByFilePath to accept folder ID, constrains SQL lookup by media_folder_id, performs case-insensitive path matching using both canonical and lowercased variants, and adds audiobookLookupPaths helper to generate deduplicated lookup paths.
Cross-Folder Duplicate Detection with Title Matching
internal/scanner/audiobook_scan.go
Introduces audiobookDuplicateCandidateSQL for multi-row candidate selection by author/narrator and year/duration tolerance; refactors findAudiobookDuplicate from single-row QueryRow to multi-row Query loop that applies audiobookDedupeTitlesMatch to identify first matching content ID across folder boundaries.
Embedded Cover Extraction and Storage
internal/scanner/audiobook_scan.go
Adds audiobookPosterExec interface for SQL execution with pgconn.CommandTag results; implements applyAudiobookEmbeddedCover to extract poster and thumbhash from the first audio file and conditionally update media_items.poster_path and poster_thumbhash only when existing values are NULL/empty; integrates into reconcileAudiobookFolder workflow.
Test Coverage for Deduplication and Cover Storage
internal/scanner/audiobook_test.go
Introduces fakeAudiobookPosterExec test double that records SQL execution calls and returns pgconn.CommandTag; adds TestApplyAudiobookEmbeddedCoverStoresPosterDuringScan validating cover extraction and caching, TestApplyAudiobookEmbeddedCoverPreservesExistingPoster verifying no update when poster exists, TestAudiobookDuplicateCandidateSQLFiltersTitleBeforeLimit asserting SQL structure, TestAudiobookDedupeTitlesMatchPunctuationVariants confirming punctuation-only title differences are equivalent, TestAudiobookDedupeTitlesKeepDifferentBooksSeparate verifying distinct books remain separate, and TestAudiobookLookupPathsIncludeCaseFoldedVariants testing case-folded path variants.
Database Index for Case-Insensitive Path Lookup
migrations/sql/20260609120500_media_files_lower_file_path_index.sql
Adds PostgreSQL index idx_media_files_folder_lower_file_path on media_files(media_folder_id, lower(file_path)) with conditional creation and drop for idempotent migrations.

🎯 3 (Moderate) | ⏱️ ~25 minutes

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 7.14% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'fix(audiobooks): extract scan covers and harden dedupe' is directly related to the main changes: extracting embedded audiobook covers and hardening deduplication logic with case-insensitive path matching and normalized title matching.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/audiobook-local-cover-scan

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

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.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@migrations/sql/20260609120500_media_files_lower_file_path_index.sql`:
- Around line 3-4: The migration should create the index without blocking writes
by using CREATE INDEX CONCURRENTLY and run outside a transaction; update the
migration that creates idx_media_files_folder_lower_file_path to use CREATE
INDEX CONCURRENTLY and add the goose directive (-- +goose NO TRANSACTION) at the
top so goose runs it non-transactionally, and do the analogous change if there
is a corresponding DROP INDEX to use DROP INDEX CONCURRENTLY.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 166872ac-b252-4b79-aca0-b874583cd648

📥 Commits

Reviewing files that changed from the base of the PR and between cb309ca and f445ab2d3301f5324439af08d9c23740b168f4dc.

📒 Files selected for processing (4)
  • internal/scanner/audiobook.go
  • internal/scanner/audiobook_scan.go
  • internal/scanner/audiobook_test.go
  • migrations/sql/20260609120500_media_files_lower_file_path_index.sql

@Quick104
Quick104 force-pushed the fix/audiobook-local-cover-scan branch from f445ab2 to d3b59cc Compare June 11, 2026 00:41
@fluxis fluxis added the v1.0 Silo v1 scope - auto-adds to the Silo v1 project label Jun 12, 2026
@fluxis fluxis added this to the v1 milestone Jun 12, 2026
@fluxis fluxis added v1.0-proposed Proposed for the v1 scope lock; awaiting triage v1.0 Silo v1 scope - auto-adds to the Silo v1 project and removed v1.0 Silo v1 scope - auto-adds to the Silo v1 project v1.0-proposed Proposed for the v1 scope lock; awaiting triage labels Jun 13, 2026
@Quick104
Quick104 merged commit 75962e0 into main Jun 16, 2026
1 check was pending
@Quick104
Quick104 deleted the fix/audiobook-local-cover-scan branch June 16, 2026 21:58
Quick104 added a commit that referenced this pull request Jun 17, 2026
* fix(audiobooks): extract scan covers and harden dedupe

* fix(audiobooks): address scan cover review

---------

Co-authored-by: rxwatcher <[email protected]>
Co-authored-by: Quick <[email protected]>
@zZebrahz zZebrahz removed this from the v1.0 milestone Sep 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

v1.0 Silo v1 scope - auto-adds to the Silo v1 project

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

4 participants