Files
silo-server/internal/api/handlers/libraries.go
T
c4cbcddeae feat(manga): manga library type — series grouping, reading loop, AniList/MangaDex metadata + status badge (#138)
* docs: design spec for manga library type (host sub-project)

Forks the ebooks library type into a 'manga' type: series detected from the
folder tree as a first-class type='manga' item, .cbz/.cbr chapters stay
readable ebook items linked via a new manga_chapters table, browse shows series
cards, enrichment targets the series item at content level 'manga'. Hands off to
a follow-on plugin spec for the manga metadata source.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* docs: implementation plan for manga library type (host)

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* feat(scanner): manga filename index/volume parser

* feat(scanner): manga series-name-from-folder detection

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* docs(plan): align manga DB/scanner tasks to scanner pure-planner pattern (no test-DB)

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* test(scanner): manga parser corpus regression

Add TestParseMangaIndexCorpus — 36 real-world scanlation filenames
covering bare chapter, decimal chapter, v/vol-prefix volume, and
c/ch-prefix chapter patterns; asserts <5% miss rate.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* feat(db): manga_chapters link table

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* feat(scanner): manga_chapters repository + pure chapter-write mapping

Adds mangaChapterWrite (pure, unit-tested), upsertMangaChapter, and
listMangaChapters following the ebook/audiobook thin-SQL pattern.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* feat(scanner): recognize manga library type

Add isMangaLibraryType helper (unexported, matching the style of
isEbookLibraryType / isAudiobookLibraryType) with a corresponding
TestIsMangaLibraryType unit test.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* feat(api): manga library content level

Map library type "manga" to content level ["manga"] in
metadataContentLevelsForLibraryType so that seedDefaultChain seeds a
manga-level metadata provider chain when a manga library is created.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* feat(scanner): route manga libraries to a manga scan path

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* feat(scanner): group manga chapters under a manga series item

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* fix(scanner): give manga series item a library membership so it browses

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* feat(catalog): browse manga libraries as series

Accept "manga" as a valid media_scope so a manga library browses only its
type='manga' series items; the per-chapter type='ebook' items are naturally
excluded because MediaScopeItemTypes("manga") expands to {"manga"}. Add the
manga default library sections (scoped to media_scope='manga') so the library
feed shows series cards. Refresh the two media_scope validation error messages.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* feat(catalog): manga series detail lists chapters

For a type='manga' item, attach its chapters to the detail response via a new
MangaDetailExtension. fetchMangaChapters joins manga_chapters to media_items on
the chapter content ID, scopes to the series, and orders by chapter_index
(NULLS LAST) then sort_title — matching the scanner's chapter ordering.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* feat(web): manga detail types + library browse scoping

Add MangaChapter/MangaDetailExtension TS types mirroring the host
catalog structs, wire manga? onto ItemDetail, and admit "manga" as a
QueryDefinition.media_scope. Scope manga libraries to media_scope=manga
in browse (host expands it to type=manga series items) while reusing the
ebook sort universe via getLibrarySortRelevanceScope. Add isMangaLibraryType.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* feat(web): manga series detail with volume-grouped chapter list

Add MangaContent detail view: a DetailHero series header plus a chapter
list grouped by volume. groupMangaChapters (pure, unit-tested) buckets
chapters by their volume token, orders chapters within a group by
chapter_index (nulls last) and orders groups by their minimum index;
loose (volume-less) chapters collapse into a trailing "Chapters" group.
Each chapter links to the existing ebook reader by content_id alone
(file_id is optional — the reader resolves the file server-side), reusing
buildMediaPlayHref. Admit "manga" into ItemDetail.type and wire the
detail switch. Continue-reading is deferred (needs per-chapter progress
fan-out / a last-read timestamp not in the current payload).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* fix(web): handle manga in playable-type + collection filter-scope unions

Adding "manga" to the shared ItemDetail["type"] and
QueryDefinition["media_scope"] unions leaked into consumers with narrower
local types, breaking the production tsc build. Fixes:

- mediaNavigation: admit "manga" into PlayableMediaType. Manga series are
  not directly playable (you open the detail page and read a chapter,
  itself an ebook item), so buildMediaPlayHref falls through to the item
  href for them, like series/season.
- FilterRuleEditor: add "manga" to FilterRuleMediaScope and relabel
  "watched" -> "Read" for manga as well as ebook (manga is read).
- CollectionGuidedRulesEditor: add "manga" to GuidedFormState.mediaScope,
  a "Manga" media-type option, ebook-like "Read Status" labels, and map
  manga -> ebook sort-relevance scope (manga has no dedicated sort scope).
- CatalogFilterBar (cascading leak surfaced after the above): add a
  "Manga" scope option and map manga -> ebook sort-relevance scope in both
  scope handlers.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* feat(web): offer manga as a library type in the create dialog

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* feat(scanner): strip scene-release junk from manga series names

Add cleanMangaSeriesName which repeatedly strips trailing parenthetical
groups (year, year-range, Digital, release-group tags) then trims any
dangling dash, so folder names like "404 Demons (Digital) (Oak)" resolve
to "404 Demons". Wire it into mangaSeriesFromPath so both the series
title and the mangaSeriesGroupKey identity key use the cleaned value.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* feat(web): flat volume/chapter manga list; nest only multi-chapter volumes

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* fix(scanner): parse manga index after stripping series-name prefix

Numbers inside a series title (e.g. "404 Demons", "365 Days to the
Wedding") were wrongly grabbed as the chapter number because
parseMangaIndex matched the first bare number in the full filename.
mangaIndexForFile now strips the series-name prefix before delegating
to parseMangaIndex, so only the number that follows the title is used.
reconcileMangaFile in manga_scan.go is updated to call mangaIndexForFile
instead of parseMangaIndex directly.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* fix(scanner): stop missing-file reconcile from deleting manga series items

Manga series items are file-less virtual parents; the shared
ReconcileFolderMembership swept them every scan because they have no
media_file. Exclude type='manga' from file-presence membership reconciliation,
and add a manga-scan step that deletes only series with zero remaining chapters.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* fix(ebooks): exclude manga chapters from individual ebook enrichment

Manga chapters are type='ebook' parts of a series; the ebook enrichment sweep
was searching each one against book sources (Gutenberg/Anna's/etc.) and failing
in a pointless storm. Exclude items with a manga_chapters link; series-level
enrichment is handled separately.

* docs: design spec for manga metadata plugin + series enrichment (sub-project 2)

New silo-plugin-manga-metadata (AniList, high-confidence matching) + a host
MangaEnricher for type='manga' series; default-enabled metadata source for manga
libraries.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* docs: implementation plan for manga metadata plugin + series enrichment

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* feat(db): manga_enrichment_state table

Mirrors ebook_enrichment_state: dedicated failure counter for the manga
enrichment sweep so it does not contend with media_items.refresh_failures.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* feat(manga): series enricher (claims type='manga', resolves manga chain)

* feat(manga): sync_manga_metadata task + enricher wiring

* feat(catalog): expose manga chapter/volume counts in browse

Add manga_chapter_count and manga_volume_count to browse cards so the
frontend can render a Vols N / Ch N chip on manga series. The counts come
from two index-backed correlated subqueries over manga_chapters in the
browse SELECT (mangaCountColumns), scanned positionally before added_at and
nilled out for non-manga rows. Threaded through models.MediaItem and exposed
on the itemListResponse JSON card.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* fix(sections): scope manga home recent sections to type=manga series

A manga library mixes type='manga' series with type='ebook' chapters, so
the auto-generated home 'Recently Added/Released in <Library>' rows surfaced
the junk chapter filenames. Add GeneratedHomeLibraryRecentConfigScoped which
emits the modern QueryDefinition shape (library_ids + media_scope) so a manga
library's generated home rows filter to type='manga' only. Other library
types are unchanged.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* fix(catalog): exclude manga chapters from browse/section/search surfaces

Manga CHAPTER items (type='ebook' rows linked into a type='manga' series
via manga_chapters) were leaking into catalog browse, section resolution,
and search as standalone items showing junk filenames. They are internal
sub-units of the series and only the series should appear.

There is no single shared item-listing chokepoint: browse, the query/preview
executor, and search each build their own WHERE. Add a shared, index-backed
anti-join predicate (manga_chapters.chapter_content_id is the PK) via
mangaChapterExclusionWhere and wire it into all three builders. By-id fetch
paths that legitimately resolve chapters (ebook reader, continue-reading,
series detail chapter list) use separate queries and are unaffected.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* fix(scanner): use #NN as the manga volume for Vol.YYYY #NN releases

mangaVolYearIssue early-return was returning the year token (e.g. "Vol.2003")
as the volume label, which the frontend couldn't prettify to "Volume N".
Now returns "v<issue>" (e.g. "v04") so the existing frontend regex ^v?(\d+)$
renders it as "Volume 4" correctly.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* feat(web): manga count chip on posters

Add an optional manga_chapter_count / manga_volume_count to the browse
item type and render a top-right "Vols N" / "Ch N" chip on ItemCard,
strictly gated on type==='manga'. The label prefers "Vols" when the
volume count dominates, "Ch" otherwise; the chip is hidden when the
chapter count is missing or non-positive. No other card type renders it.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* fix(web): manga reader back returns to series (no loop)

The ebook reader's back action defaulted to the chapter's own item
detail (/item/<chapter>), whose back returned to the reader — an
infinite loop for manga chapters. The reader now honors an explicit
backTo search param when present, navigating there instead. Absent for
normal ebooks, so their back behavior is unchanged. Only manga chapter
rows pass backTo, keeping the fix manga-only.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* feat(web): manga chapter row actions (read/mark-read/download)

Each manga chapter/volume row now offers Read (the existing reader link,
now carrying a backTo to the series), Mark-read (the shared watched-state
mutation per chapter content_id), and Download (lazily fetches the
chapter's file versions on demand and opens the shared
DownloadVersionPicker, gated on user.download_allowed). The
volume-unit / loose-chapter / section structure from buildMangaList is
unchanged. Scoped to MangaContent only; EbookContent is untouched.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* fix(web): validate reader backTo param is a safe in-app relative path

Prevents open-redirect / javascript:-URI XSS from a crafted ?backTo= URL.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* feat(catalog): include per-chapter read state in manga detail

Manga chapters are ebook items, so a chapter is "read" when the viewer's
ebook_reader_progress row crosses the finished threshold. fetchMangaChapters
now LEFT JOINs that table scoped to the AccessFilter's user_id/profile_id and
exposes a per-chapter Read bool on MangaChapter, threaded through
buildMangaExtension. The detail payload previously carried no read state, so
the row toggle always started unread.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* feat(web): manga rows reflect read state on load

MangaChapter now carries an optional read flag from the detail payload, and
MangaRow seeds its mark-read toggle from chapter.read instead of always
starting unread. The optimistic toggle + shared watched mutation are
unchanged; only the initial value is seeded.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* fix(sections): exclude manga chapters from recently-added/released/random + other library-listing sections

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* feat(sections): manga recently-added/released cards show the latest volume's cover

* fix(manga): keep enrichment honest about no-match vs enriched, batch 50->200

- sweep stats now separate enriched / no_match / failed: a stamped no-match
  was counted (and logged) as an enrichment, which masked a collapse of the
  real match rate during the backfill
- batch size 50 -> 200 (SILO_MANGA_ENRICH_BATCH overrides): with the plugin
  serving GetMetadata from its search cache an item costs one rate-limited
  AniList request, so a sweep still fits the 5-minute task interval

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(manga): size enrich batch to the 5-minute interval at AniList's real budget

140 items x ~2.1s/request fits the interval; an overlong sweep makes the task
manager drop the next trigger and the effective rate falls below the AniList
budget.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(catalog): manga count chip data missing from library browse

manga_chapter_count/manga_volume_count were only added to BrowseRepository,
but /library/{id}?tab=library flows through previewQuerySource ->
QueryExecutor.PreviewPage, which selects qualifiedListItemColumns and scans
with scanItems - so manga cards never carried the counts and the Vols/Ch
poster chip stayed hidden.

Append mangaCountColumns to the preview-page SELECT and scan them via a new
scanItemsWithMangaCounts (nil for non-manga rows, mirroring scanBrowseItems).
Extract listItemScanDests so the three scan variants share one destination
list instead of duplicating the 48-column scan.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* feat(web): manga chip reads 'X Volumes · X Chapters', menu verbs say Read

- chip: show distinct-volume and loose-chapter counts side by side instead
  of the single 'Vols N'/'Ch N' heuristic; mangaCountColumns now counts
  DISTINCT volume tokens (rows sharing a volume are one volume) and only
  un-volumed rows as chapters
- watched-state labels: type='manga' fell through to the video default, so
  the card dot menu and detail page said 'Mark Watched' - manga now uses
  the ebook reading verbs (Mark Read / Mark Unread, 'Marked as read' toast)
- format MangaContent.test.tsx (pre-existing prettier miss)

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* feat(manga): backdrop enrichment - banner hero art + backdrop-only backfill

- cache remote backdrops like posters (cacheRemoteImages generalizes the
  poster-only path; failures keep the provider URL, which still renders)
- claim arm for enriched items missing a backdrop: fetched by stored
  provider ID (search skipped - no rate spend, no re-match risk) and only
  the backdrop is written; stamping after the attempt keeps banner-less
  series from being re-claimed every sweep
- backfill = one-time SQL clearing last_refreshed for poster-set/
  backdrop-empty manga

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* feat(manga): reading-loop UX - continue CTA, next chapter, series-aware cards, file details

Fixes the four high-priority findings from the manga UX review plus a
file-inspector request:

- H1: series hero gets a Continue / Start Reading / Read Again CTA
  targeting the first unread chapter (firstUnreadChapter over the ordered
  list), plus an overflow menu (View Details, admin Refresh Metadata)
- H2: the reader resolves its owning manga series (chapter detail now
  carries series_id/series_title) and offers next-chapter navigation: a
  header next button and an end-of-book floating CTA at >=99.5% progress;
  back defaults to the series even without a backTo param
- H3: chapter rows show a persistent read check + muted title, and the
  mark-read mutation carries series_id so the series detail cache
  invalidates (read states no longer revert on revisit)
- H4: continue-reading cards for manga chapters present the series:
  sections payload resolves chapter->series linkage, the card heading/image
  link to the series, and meta lines launch the reader
- View Details: manga series menus (card dot menu + detail overflow) open
  a file inspector showing folder paths and per-chapter file names/sizes
  via GET /catalog/items/{id}/manga-files; paths are stripped for viewers
  without file-path visibility (item-versions policy)

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* feat(manga): UX mediums - richer detail page, smarter list, manga sort scope

Second batch from the manga UX review (M1-M7):

- M1: multi-chapter volume sections are collapsible (fully read sections
  start collapsed) with sticky headers, and long series get a 'Jump to
  <next unread>' anchor above the list
- M2: the series hero shows the author line (HeroCrewLine learns Author
  credits with person links; DetailHero now renders crewLine and genre
  chips independently) and Volumes/Chapters badges
- M3: browse-card count chip abbreviates to '12 Vol - 3 Ch' so it fits
  narrow cards without occluding covers
- M4: manga gets its own sort scope: Duration/Bitrate (meaningless for
  file-less series rows) disappear, reading labels (Date Read / Reads)
  apply, Author stays
- M5: global search labels manga results 'Manga' instead of the raw type
- M6: chapters carry the viewer's reading fraction; part-read rows show an
  inline progress bar + percent
- M7: chapter rows show the extracted cover thumbnail (presigned
  poster_url on the chapters payload) instead of a generic icon

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(manga): UX lows - volume token dedupe, comic reader chrome, empty-state hint

- buildMangaList buckets volumes by canonical numeric token so mixed
  release naming (v01 + 1) yields one Volume 1 instead of duplicates
- cbz/cbr readers start with the side panel closed and hide prose-only
  chrome (reading ruler, TTS, typography/font controls, hyphenation,
  writing mode) while keeping comic-relevant settings (theme, brightness,
  margin, right-to-left, spread, flow)
- manga empty state mentions chapters appear after the library scan

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* feat(manga): publication status badge via new SDK status field

- vendor the unpublished plugin SDK (adds MetadataItem.status) under
  internal/compat/ with a relative go.mod replace, following the
  zishang520-webtransport-go convention; swap to the published module
  before the upstream PR
- map plugin status into MetadataResult.ShowStatus, persist it during
  manga enrichment, and show it as the hero status badge (show_status was
  already on the detail payload and MetadataBadges)

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* feat(manga): generalize backdrop pass to secondary fields (backdrop + status)

The backdrop-only claim arm becomes a secondary-fields pass: enriched items
missing a backdrop and/or publication status are claimed, fetched by stored
provider ID, and only the missing secondary fields are written. Lets the
new status field backfill across the already-enriched library instead of
applying only to future enrichments.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(metadata): merge ShowStatus through MergeMetadata/MergeGlobalMetadata

The new MetadataResult.ShowStatus never reached the accumulated result the
manga enricher persists from - the field-by-field merges didn't know it, so
the status backfill pass obtained nothing. Regression-tested on both paths.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(manga): keep scanner identity IDs out of the metadata flow

filterMangaProviderIDs passed the scanner's manga_series identity row
through, so the search-skip-when-already-matched guard saw provider IDs on
every item and never searched: unmatched items went straight to a by-ID
fetch with no usable ID and were stamped as terminal no-match without a
single provider request (and the MangaDex fallback was never consulted).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* chore: gitignore docker-compose.override.yml (local deployment override)

The override unpublishes the bundled redis/postgres host ports
(ports: !override []). It is a per-deployment, local-only file: ignoring it
keeps a rebase from main and git clean -fd from disturbing it, and keeps it
out of any PR. Its accidental absence once exposed Redis to the internet.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(manga): code-review fixes — no-match guard, sort comparator, volume-count consistency

- enrichWithProviders: set accumulator.HasMetadata after a provider result
  merges (MergeMetadata doesn't propagate it). Without this, a confident
  match carrying only genres/authors/status/year but no cover and no overview
  failed the no-match check and was discarded + terminally stamped.
- byChapterIndex: both un-indexed chapters yield POSITIVE_INFINITY, so the
  subtraction was Infinity-Infinity=NaN (Array.sort treats NaN as 0, leaving
  order undefined). Compare explicitly for a stable order.
- MangaContent volume/chapter badges: derive counts from the rendered
  buildMangaList entries (which canonicalize v01 ≡ 1) instead of raw distinct
  volume tokens, so the badge can no longer say '2 Volumes' over one row.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* docs(manga): clarify the enrichment claim's secondary arm is admin-reset-only

The secondary arm (poster present, backdrop/status missing) requires
last_refreshed IS NULL, so it is only reachable when an operator resets
last_refreshed to backfill a newly-added field — not an automatic periodic
re-check (which would re-fetch banner-less series every sweep). Documents the
intent so it does not read as dead code.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(manga): collapse continue-reading chapters per series; batch provider-id lookup

- Continue Reading now collapses multiple in-progress chapters of the same
  manga into one card (most recently read kept), mirroring the episode→series
  collapse. The reading section resolves chapter→series linkage into itemMeta
  (applyMangaChapterSeriesMeta) and runs the shared
  collapseContinueWatchingSeriesCandidates, which the reading path previously
  skipped.
- claimBatch resolves provider IDs for the whole batch in one query via the
  new ProviderIDRepository.GetByContentIDs (content_id = ANY), replacing the
  per-item GetByContentID N+1.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* feat(web): manga publication-status chip on browse cards + more legible chips

- Color-coded publication status pill (Ongoing/Completed/Hiatus/Cancelled/
  Upcoming) in the manga card's top-left corner, mirroring the vol/chapter
  count chip top-right. Strictly manga-gated; show_status was already on the
  browse payload.
- New .glass-chip (78% surface vs glass-subtle's 40%) for the manga count +
  status pills so the labels stay legible over busy cover art.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* build(manga): depend on published silo-plugin-sdk v0.7.0

Replace the vendored internal/compat/silo-plugin-sdk copy with a normal
dependency on the published SDK module at v0.7.0, which adds
MetadataItem.status (publication/airing status) consumed by the manga
status badge at internal/metadata/plugin_provider.go.

- go.mod: pin v0.7.0, drop the local-path replace directive
- remove the vendored internal/compat/silo-plugin-sdk tree
- Dockerfile: drop the vendored-SDK COPY
- strip the manga design docs/plans from docs/superpowers (internal)

Requires Silo-Server/silo-plugin-sdk#4 merged and tagged v0.7.0.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* fix(manga): exclude chapters from the matcher's unmatched-item lister

Manga chapters are type='ebook' items that stay status='pending' by
design - provider metadata lives on the type='manga' series item. The
scan-final RetryUnmatchedItemsByFolderAndPathPrefix listed all of them
and ran a rate-limited ebook-plugin search per chapter: 31,564 chapters
x ~1s = 8h46m appended to a 2-minute manga library scan (observed
live), every one a guaranteed no-match. Earlier runs never survived to
completion, so the library's last_scanned_at stayed NULL forever.

Add the same manga_chapters NOT EXISTS guard the ebook enricher's
claim query already uses. Verified live: the same library now scans in
27s with retried_items=0.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(scanner): never probe-repair ebook/comic files (ebook+manga detail-page killer)

NeedsCriticalProbeRepair was always true for BaseType 'ebook' files (epub, pdf,
cbz, cbr — incl. manga chapters): buildEbookMediaFile leaves ProbeUpdatedAt nil
and they have no audio/video, so probeEnsurer.Ensure spawned ffprobe per file on
every detail/watch load and never converged (ffprobe errors on zip/rar, result
never persisted). Short-circuit probe-repair for ebook base type — they're read
directly and never use the transcode/playback probe pipeline.

SHARED fix: benefits both the ebooks and manga library types.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* perf+fix(ebooks): parallelize detail extension + preserve finished read-state

- buildEbookExtension ran its 3 related-content queries (series, also-by-author,
  similar) sequentially; run them concurrently like buildAudiobookExtension so
  ebook detail latency is the slowest query, not their sum.
- PGEbookReaderProgressStore.Upsert did an unconditional SET progress=EXCLUDED;
  a routine autosave (e.g. reopening a finished book) could drop it below the
  0.9 finished threshold and silently un-mark it read (and clear the manga
  chapter checkmark, which rides on the same row). Guard: once finished,
  progress only moves on an explicit unread (row delete); below threshold it
  tracks freely.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* perf(manga): batch chapter presign, index volume counts, quiet scan log

- fetchMangaChapters presigned each chapter poster individually; a long-running
  series has hundreds of chapters. Batch them in one PresignImageURLs call, and
  add the missing rows.Err() check (was silently returning partial lists).
- The browse manga count chip's count(DISTINCT volume) subquery wasn't covered
  by manga_chapters_series (series_content_id, chapter_index); add
  idx_manga_chapters_series_volume (series_content_id, volume) so both count
  subqueries are index-only.
- Downgrade the per-chapter "manga scan: indexed" log from Info to Debug (one
  line per .cbz; the 500-file progress log already covers operator visibility).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* fix(manga): address PR #138 code-review findings

Folds PR #142 into the manga branch (already done via fast-forward) and
remediates the issues surfaced in the #138 code review.

Correctness:
- Preserve the scanner's manga_series identity anchor through enrichment.
  ReplaceByContentID's DELETE was unconditional, so the first successful
  enrichment wiped the manga_series provider-id row the scanner relies on
  for idempotency, causing duplicate series + metadata loss on the next
  scan. excludedProviderIDs now also means "not deleted", and the DELETE
  preserves those rows. (internal/catalog/provider_id_repo.go)
- Fall back to the series cover when the latest chapter has no poster.
  Poster columns default to '' (not NULL), so the manga series-card poster
  override blanked cards via a plain COALESCE; wrap operands in NULLIF.
  (internal/sections/fetcher.go)
- Keep backTo a real query param on reader links when libraryId is absent.
  It was string-concatenated with '&', producing a malformed URL on
  deep-links; route it through the query helper instead.
  (web/src/lib/mediaNavigation.ts, EbookReader.tsx, MangaContent.tsx)

Quality:
- Hide manga chapters from favorites/watchlist browse, matching the
  exclusion enforced on every other listing surface.
  (internal/catalog/favorites_browse.go)
- Centralize the manga chapter exclusion predicate into a single exported
  catalog.MangaChapterExclusionWhere, removing four duplicated copies.
  (catalog, sections, ebooks)
- Skip the two manga count subqueries on browse scopes that cannot contain
  manga (non-manga type filters), substituting NULL placeholders.
  (internal/catalog/browse.go)
- Normalize provider publication status (AniList/MangaDex/SDK variants)
  into a stable label set so show_status carries one manga value-domain.
  (internal/manga/enrichment.go)

Adds unit tests for the poster NULLIF contract, browse gating, and status
normalization.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* chore: regenerate go.sum after rebase onto main

Drops stale silo-plugin-sdk v0.6.0 and other leftover hashes from the
intermediate rebased states; go.mod is now on the published v0.7.0 tag.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* fix(scanner): adapt manga scan to ebookFileShouldSkip 3-value signature

main changed ebookFileShouldSkip to also return the existing content ID;
the manga scan path only needs the unchanged flag, so discard the new
return. Resolves a silent semantic conflict from the rebase onto main.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

---------

Co-authored-by: Silo Server Developer <warmasterx555@gmail.com>
Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-16 20:13:10 -04:00

2564 lines
89 KiB
Go

package handlers
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"log/slog"
"net/http"
"os"
"path/filepath"
"slices"
"sort"
"strconv"
"strings"
"time"
"github.com/go-chi/chi/v5"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/oklog/ulid/v2"
"github.com/Silo-Server/silo-server/internal/access"
"github.com/Silo-Server/silo-server/internal/adminjob"
apimw "github.com/Silo-Server/silo-server/internal/api/middleware"
"github.com/Silo-Server/silo-server/internal/auth"
"github.com/Silo-Server/silo-server/internal/cache"
"github.com/Silo-Server/silo-server/internal/catalog"
evt "github.com/Silo-Server/silo-server/internal/events"
"github.com/Silo-Server/silo-server/internal/libraryingest"
"github.com/Silo-Server/silo-server/internal/metadata"
"github.com/Silo-Server/silo-server/internal/models"
"github.com/Silo-Server/silo-server/internal/plugins"
"github.com/Silo-Server/silo-server/internal/scanner"
"github.com/Silo-Server/silo-server/internal/scantrigger"
"github.com/Silo-Server/silo-server/internal/sections"
"github.com/Silo-Server/silo-server/internal/userstore"
)
// LibraryHandler handles HTTP endpoints for library (media folder) management
// and scan triggering.
type LibraryHandler struct {
folderRepo *catalog.FolderRepository
ingester libraryIngester
userRepo *auth.UserRepository
pool *pgxpool.Pool
refresher AdminMetadataRefresher
chainCacheInvalidator interface{ InvalidateChainCache() }
JobRepo AdminJobCreator
ChainRepo *metadata.ChainRepository
PluginInstallations pluginInstallationLister
SkippedRootRepo *metadata.SkippedRootRepository
StaleIDRepo *metadata.StaleMediaIDRepository
MovieMatchQueueRepo libraryMovieMatchQueue
SeriesMatchQueueRepo librarySeriesMatchQueue
RawMatchBacklogRepo libraryRawMatchBacklog
TVSeriesRootQueue bool
ScannedGroupRepo *scanner.ScannedGroupRepository
GroupOverrideRepo *scanner.MediaGroupOverrideRepository
ObservedLocationRepo *scanner.ObservedLocationRepository
SectionRepo *sections.Repository
StoreProvider userstore.UserStoreProvider
S3Meta LibraryImageStore
PresignTTL time.Duration
appCtx context.Context
EventBus cache.EventBus
EventsHub *evt.Hub
ScanRegistry *evt.ScanRegistry
ScanQueue libraryScanQueuer
}
// LibraryImageStore provides S3 operations for library poster images.
type LibraryImageStore interface {
PutObject(ctx context.Context, bucket, key string, data []byte) error
DeleteObject(ctx context.Context, bucket, key string) error
PresignGetURL(ctx context.Context, bucket, key string, expiry time.Duration) (string, error)
Bucket() string
}
// pluginInstallationLister provides access to plugin installations and capabilities
// for seeding default provider chains from manifest metadata.
type pluginInstallationLister interface {
ListEnabled(ctx context.Context) ([]*plugins.Installation, error)
ListCapabilities(ctx context.Context, installationID int) ([]*plugins.Capability, error)
}
type libraryIngester interface {
IngestFolder(ctx context.Context, folder *models.MediaFolder) (*libraryingest.Result, error)
IngestSubtree(ctx context.Context, folder *models.MediaFolder, subtreePath string) (*libraryingest.Result, error)
IngestFile(ctx context.Context, folder *models.MediaFolder, filePath string) (*libraryingest.Result, error)
CancelLibrary(folderID int) int
}
type libraryScanQueuer interface {
EnqueueLibraryScan(ctx context.Context, folderID int, trigger string) (bool, error)
EnqueueScan(ctx context.Context, folderID int, mode, path, trigger string) (bool, error)
CancelAcceptedByLibrary(ctx context.Context, libraryID int) (int, error)
CancelByLibrary(ctx context.Context, libraryID int) (int, error)
}
type libraryMovieMatchQueue interface {
SyncForFolder(ctx context.Context, folderID int) error
DeleteByFolder(ctx context.Context, folderID int) (int, error)
CountByFolder(ctx context.Context, folderID int) (int, error)
ListByFolder(ctx context.Context, folderID int, limit int, offset int) ([]models.MovieMatchQueueEntry, int, error)
}
type librarySeriesMatchQueue interface {
SyncForFolder(ctx context.Context, folderID int) error
DeleteByFolder(ctx context.Context, folderID int) (int, error)
CountByFolder(ctx context.Context, folderID int) (int, error)
ListByFolder(ctx context.Context, folderID int, limit int, offset int) ([]models.SeriesRootMatchQueueEntry, int, error)
}
type libraryRawMatchBacklog interface {
CountUnmatchedMatchBacklogByFolder(ctx context.Context, folderID int, mode scanner.RawMatchBacklogMode) (int, error)
ListUnmatchedMatchBacklogByFolder(ctx context.Context, folderID int, mode scanner.RawMatchBacklogMode, limit int, offset int) ([]*models.MediaFile, int, error)
SuppressUnmatchedMatchBacklogByFolder(ctx context.Context, folderID int, mode scanner.RawMatchBacklogMode) (int, error)
RetryUnmatchedMatchBacklogByFolder(ctx context.Context, folderID int, mode scanner.RawMatchBacklogMode) (int, error)
}
// NewLibraryHandler creates a new LibraryHandler backed by the given folder
// repository and ingest executor. The ingester may be nil if scan endpoints are not needed.
func NewLibraryHandler(
folderRepo *catalog.FolderRepository,
ingester libraryIngester,
userRepo *auth.UserRepository,
pool *pgxpool.Pool,
refresher AdminMetadataRefresher,
appCtx ...context.Context,
) *LibraryHandler {
ctx := context.Background()
if len(appCtx) > 0 && appCtx[0] != nil {
ctx = appCtx[0]
}
var scannedGroupRepo *scanner.ScannedGroupRepository
var groupOverrideRepo *scanner.MediaGroupOverrideRepository
var observedLocationRepo *scanner.ObservedLocationRepository
if pool != nil {
scannedGroupRepo = scanner.NewScannedGroupRepository(pool)
groupOverrideRepo = scanner.NewMediaGroupOverrideRepository(pool)
observedLocationRepo = scanner.NewObservedLocationRepository(pool)
}
return &LibraryHandler{
folderRepo: folderRepo,
ingester: ingester,
userRepo: userRepo,
pool: pool,
refresher: refresher,
ScannedGroupRepo: scannedGroupRepo,
GroupOverrideRepo: groupOverrideRepo,
ObservedLocationRepo: observedLocationRepo,
appCtx: ctx,
}
}
func (h *LibraryHandler) SetChainCacheInvalidator(invalidator interface{ InvalidateChainCache() }) {
if h == nil {
return
}
h.chainCacheInvalidator = invalidator
}
// validMetadataLanguages is the set of ISO 639-1 codes accepted for
// per-library metadata language. Kept in sync with the frontend LANGUAGES list.
var validMetadataLanguages = map[string]bool{
"en": true, "es": true, "fr": true, "de": true, "it": true, "pt": true,
"nl": true, "pl": true, "ru": true, "zh": true, "ja": true, "ko": true,
"ar": true, "tr": true, "sv": true, "da": true, "no": true, "fi": true,
"hu": true, "cs": true, "ro": true, "he": true, "th": true, "vi": true,
"el": true, "bg": true, "hr": true, "sk": true, "sl": true, "uk": true,
"id": true, "ms": true, "hi": true, "ta": true, "te": true, "bn": true,
"fa": true,
}
// --- Request/Response types ---
// createLibraryRequest represents the JSON body for POST /libraries.
type createLibraryRequest struct {
Paths []string `json:"paths"`
Type string `json:"type"`
Name string `json:"name"`
MetadataLanguage string `json:"metadata_language,omitempty"`
ChapterThumbnailsEnabled bool `json:"chapter_thumbnails_enabled,omitempty"`
IntroDetectionEnabled bool `json:"intro_detection_enabled,omitempty"`
}
// updateLibraryRequest represents the JSON body for PUT /libraries/{id}.
type updateLibraryRequest struct {
Paths *[]string `json:"paths,omitempty"`
Type *string `json:"type,omitempty"`
Name *string `json:"name,omitempty"`
Enabled *bool `json:"enabled,omitempty"`
MetadataLanguage *string `json:"metadata_language,omitempty"`
AutoTranslateMetadata *bool `json:"auto_translate_metadata,omitempty"`
ChapterThumbnailsEnabled *bool `json:"chapter_thumbnails_enabled,omitempty"`
IntroDetectionEnabled *bool `json:"intro_detection_enabled,omitempty"`
}
// scanRequest represents the JSON body for POST /scan.
type scanRequest struct {
LibraryID *int `json:"library_id,omitempty"`
Path string `json:"path,omitempty"`
}
type scanResponse struct {
Status string `json:"status"`
Mode string `json:"mode"`
LibraryID int `json:"library_id"`
}
// scanCancelRequest represents the JSON body for POST /scan/cancel.
type scanCancelRequest struct {
LibraryID int `json:"library_id"`
}
type scanCancelResponse struct {
Cancelled int `json:"cancelled"`
LibraryID int `json:"library_id"`
}
// libraryResponse represents a library (media folder) in JSON responses.
type libraryResponse struct {
ID int `json:"id"`
Paths []string `json:"paths"`
Type string `json:"type"`
Name string `json:"name"`
Enabled bool `json:"enabled"`
MetadataLanguage string `json:"metadata_language"`
AutoTranslateMetadata bool `json:"auto_translate_metadata"`
ChapterThumbnailsEnabled bool `json:"chapter_thumbnails_enabled"`
ChapterThumbnailsSupported bool `json:"chapter_thumbnails_supported"`
IntroDetectionEnabled bool `json:"intro_detection_enabled"`
SortOrder int `json:"sort_order"`
PosterURL string `json:"poster_url,omitempty"`
LastScannedAt *time.Time `json:"last_scanned_at,omitempty"`
ScanWarningCode *string `json:"scan_warning_code,omitempty"`
ScanWarningMessage *string `json:"scan_warning_message,omitempty"`
ScanWarningAt *time.Time `json:"scan_warning_at,omitempty"`
}
type libraryMountCheckRootResponse struct {
Path string `json:"path"`
Reachable bool `json:"reachable"`
ErrorCode *string `json:"error_code"`
ErrorMessage *string `json:"error_message"`
}
type libraryMountCheckResponse struct {
Status string `json:"status"`
LibraryID int `json:"library_id"`
LibraryName string `json:"library_name"`
Healthy bool `json:"healthy"`
CheckedAt time.Time `json:"checked_at"`
Summary string `json:"summary"`
Roots []libraryMountCheckRootResponse `json:"roots"`
}
type librarySkippedRootResponse struct {
LibraryID int `json:"library_id"`
LibraryName string `json:"library_name"`
RootPath string `json:"root_path"`
Reason string `json:"reason"`
SampleFilePath string `json:"sample_file_path"`
FileCount int `json:"file_count"`
FirstSeenAt time.Time `json:"first_seen_at"`
LastSeenAt time.Time `json:"last_seen_at"`
}
type staleMediaIDResponse struct {
ContentID string `json:"content_id"`
LibraryID int `json:"library_id"`
LibraryName string `json:"library_name"`
Title string `json:"title"`
Year int `json:"year"`
ContentType string `json:"content_type"`
Provider string `json:"provider"`
ProviderID string `json:"provider_id"`
FirstSeenAt string `json:"first_seen_at"`
LastSeenAt string `json:"last_seen_at"`
}
type libraryRootResponse struct {
LibraryID int `json:"library_id"`
LibraryName string `json:"library_name"`
RootPath string `json:"root_path"`
State string `json:"state"`
InferredType string `json:"inferred_type"`
TypeConfidence string `json:"type_confidence"`
Title string `json:"title"`
Year int `json:"year"`
TmdbID string `json:"tmdb_id,omitempty"`
ImdbID string `json:"imdb_id,omitempty"`
TvdbID string `json:"tvdb_id,omitempty"`
ObservedFiles int `json:"observed_file_count"`
SampleFilePath string `json:"sample_file_path,omitempty"`
Evidence json.RawMessage `json:"evidence_json,omitempty"`
OverrideSource string `json:"override_source,omitempty"`
FirstSeenAt time.Time `json:"first_seen_at"`
LastSeenAt time.Time `json:"last_seen_at"`
ActiveOverride *rootOverride `json:"active_override,omitempty"`
}
type rootOverride struct {
ForcedType string `json:"forced_type,omitempty"`
ForcedTitle string `json:"forced_title,omitempty"`
ForcedYear int `json:"forced_year,omitempty"`
ForcedTmdbID string `json:"forced_tmdb_id,omitempty"`
ForcedImdbID string `json:"forced_imdb_id,omitempty"`
ForcedTvdbID string `json:"forced_tvdb_id,omitempty"`
Note string `json:"note,omitempty"`
}
type libraryRootsListResponse struct {
Items []libraryRootResponse `json:"items"`
Total int `json:"total"`
}
type rootOverrideUpsertRequest struct {
LibraryID int `json:"library_id"`
RootPath string `json:"root_path"`
ForcedType string `json:"forced_type,omitempty"`
ForcedTitle string `json:"forced_title,omitempty"`
ForcedYear int `json:"forced_year,omitempty"`
ForcedTmdbID string `json:"forced_tmdb_id,omitempty"`
ForcedImdbID string `json:"forced_imdb_id,omitempty"`
ForcedTvdbID string `json:"forced_tvdb_id,omitempty"`
Note string `json:"note,omitempty"`
}
type rootOverrideDeleteRequest struct {
LibraryID int `json:"library_id"`
RootPath string `json:"root_path"`
}
func groupOverrideLookupKey(groupKeyVersion int, contentGroupKey string) string {
return strconv.Itoa(groupKeyVersion) + "|" + contentGroupKey
}
// toLibraryResponse converts a MediaFolder model to a libraryResponse.
func toLibraryResponse(f *models.MediaFolder) libraryResponse {
paths := f.Paths
if paths == nil {
paths = []string{}
}
return libraryResponse{
ID: f.ID,
Paths: paths,
Type: f.Type,
Name: f.Name,
Enabled: f.Enabled,
MetadataLanguage: f.MetadataLanguage,
AutoTranslateMetadata: f.AutoTranslateMetadata,
ChapterThumbnailsEnabled: f.ChapterThumbnailsEnabled,
ChapterThumbnailsSupported: false,
IntroDetectionEnabled: f.IntroDetectionEnabled,
SortOrder: f.SortOrder,
LastScannedAt: f.LastScannedAt,
ScanWarningCode: f.ScanWarningCode,
ScanWarningMessage: f.ScanWarningMessage,
ScanWarningAt: f.ScanWarningAt,
}
}
// toLibraryResponseWithPoster converts a MediaFolder model to a libraryResponse
// and presigns the poster URL if a poster path is set.
func (h *LibraryHandler) toLibraryResponseWithPoster(ctx context.Context, f *models.MediaFolder) libraryResponse {
resp := toLibraryResponse(f)
resp.ChapterThumbnailsSupported = h.S3Meta != nil
if f.PosterPath != "" && h.S3Meta != nil {
ttl := h.PresignTTL
if ttl <= 0 {
ttl = 4 * time.Hour
}
url, err := h.S3Meta.PresignGetURL(ctx, h.S3Meta.Bucket(), f.PosterPath, ttl)
if err == nil {
resp.PosterURL = url
}
}
return resp
}
// userLibraryResponse is a simplified library view for non-admin users.
type userLibraryResponse struct {
ID int `json:"id"`
Name string `json:"name"`
Type string `json:"type"`
SortOrder int `json:"sort_order"`
PosterURL string `json:"poster_url,omitempty"`
}
// --- Handler methods ---
// HandleListUserLibraries handles GET /user/libraries.
// It returns only enabled libraries the current user has access to, with
// simplified fields (no paths, last scan metadata, etc.).
func (h *LibraryHandler) HandleListUserLibraries(w http.ResponseWriter, r *http.Request) {
var folders []*models.MediaFolder
var err error
if scope, ok := access.GetScope(r.Context()); ok {
if scope.LibrariesRestricted {
folders, err = h.folderRepo.ListByIDs(r.Context(), scope.AllowedLibraryIDs)
} else {
folders, err = h.folderRepo.GetEnabled(r.Context())
}
} else {
userID := apimw.GetUserID(r.Context())
if h.userRepo != nil {
user, userErr := h.userRepo.GetByID(r.Context(), userID)
if userErr != nil {
slog.Error("looking up user for library access", "error", userErr)
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to look up user")
return
}
if user.LibraryIDs != nil {
folders, err = h.folderRepo.ListByIDs(r.Context(), user.LibraryIDs)
} else {
folders, err = h.folderRepo.GetEnabled(r.Context())
}
} else {
folders, err = h.folderRepo.GetEnabled(r.Context())
}
}
if err != nil {
slog.Error("listing user libraries", "error", err)
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to list libraries")
return
}
resp := make([]userLibraryResponse, 0, len(folders))
for _, f := range folders {
entry := userLibraryResponse{
ID: f.ID,
Name: f.Name,
Type: f.Type,
SortOrder: f.SortOrder,
}
if f.PosterPath != "" && h.S3Meta != nil {
ttl := h.PresignTTL
if ttl <= 0 {
ttl = 4 * time.Hour
}
if url, err := h.S3Meta.PresignGetURL(r.Context(), h.S3Meta.Bucket(), f.PosterPath, ttl); err == nil {
entry.PosterURL = url
}
}
resp = append(resp, entry)
}
writeJSON(w, http.StatusOK, resp)
}
// HandleListLibraries handles GET /libraries.
func (h *LibraryHandler) HandleListLibraries(w http.ResponseWriter, r *http.Request) {
folders, err := h.folderRepo.List(r.Context())
if err != nil {
slog.Error("listing libraries", "error", err)
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to list libraries")
return
}
resp := make([]libraryResponse, 0, len(folders))
for _, f := range folders {
resp = append(resp, h.toLibraryResponseWithPoster(r.Context(), f))
}
writeJSON(w, http.StatusOK, resp)
}
// reorderLibrariesRequest is the JSON body for PUT /libraries/reorder.
type reorderLibrariesRequest struct {
Entries []catalog.FolderReorderEntry `json:"entries"`
}
// HandleReorderLibraries handles PUT /libraries/reorder.
func (h *LibraryHandler) HandleReorderLibraries(w http.ResponseWriter, r *http.Request) {
var req reorderLibrariesRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
writeError(w, http.StatusBadRequest, "bad_request", "Invalid request body")
return
}
if err := h.folderRepo.Reorder(r.Context(), req.Entries); err != nil {
slog.Error("reordering libraries", "error", err)
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to reorder libraries")
return
}
w.WriteHeader(http.StatusNoContent)
}
// HandleListSkippedRoots handles GET /libraries/skipped-roots.
func (h *LibraryHandler) HandleListSkippedRoots(w http.ResponseWriter, r *http.Request) {
if h.SkippedRootRepo == nil {
writeJSON(w, http.StatusOK, []librarySkippedRootResponse{})
return
}
folders, err := h.folderRepo.List(r.Context())
if err != nil {
slog.Error("listing libraries for skipped roots", "error", err)
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to list libraries")
return
}
folderNames := make(map[int]string, len(folders))
for _, folder := range folders {
folderNames[folder.ID] = folder.Name
}
roots, err := h.SkippedRootRepo.ListAll(r.Context())
if err != nil {
slog.Error("listing skipped roots", "error", err)
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to list skipped roots")
return
}
resp := make([]librarySkippedRootResponse, 0, len(roots))
for _, root := range roots {
resp = append(resp, librarySkippedRootResponse{
LibraryID: root.MediaFolderID,
LibraryName: folderNames[root.MediaFolderID],
RootPath: root.RootPath,
Reason: root.Reason,
SampleFilePath: root.SampleFilePath,
FileCount: root.FileCount,
FirstSeenAt: root.FirstSeenAt,
LastSeenAt: root.LastSeenAt,
})
}
writeJSON(w, http.StatusOK, resp)
}
// HandleCreateLibrary handles POST /libraries.
func (h *LibraryHandler) HandleCreateLibrary(w http.ResponseWriter, r *http.Request) {
var req createLibraryRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
writeError(w, http.StatusBadRequest, "bad_request", "Invalid request body")
return
}
if len(req.Paths) == 0 || req.Type == "" || req.Name == "" {
writeError(w, http.StatusBadRequest, "bad_request", "Paths, type, and name are required")
return
}
if req.MetadataLanguage != "" && !validMetadataLanguages[req.MetadataLanguage] {
writeError(w, http.StatusBadRequest, "bad_request", "Invalid metadata_language; must be a valid ISO 639-1 code")
return
}
if req.ChapterThumbnailsEnabled && h.S3Meta == nil {
writeError(w, http.StatusBadRequest, "bad_request", "Chapter thumbnails require configured public asset S3 storage")
return
}
folder, err := h.folderRepo.Create(r.Context(), catalog.CreateFolderInput{
Paths: req.Paths,
Type: req.Type,
Name: req.Name,
MetadataLanguage: req.MetadataLanguage,
ChapterThumbnailsEnabled: req.ChapterThumbnailsEnabled,
IntroDetectionEnabled: req.IntroDetectionEnabled,
})
if err != nil {
if errors.Is(err, catalog.ErrDuplicatePath) {
writeError(w, http.StatusConflict, "conflict", "A library with this path already exists")
return
}
slog.Error("creating library", "error", err)
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to create library")
return
}
// Seed default sections for the new library.
if h.SectionRepo != nil {
if seedErr := h.SectionRepo.SeedDefaults(r.Context(), "library", &folder.ID, sections.DefaultLibrarySectionsForType(&folder.ID, folder.Type)); seedErr != nil {
slog.Warn("seed default sections for new library", "library_id", folder.ID, "error", seedErr)
}
if sections.IsAudiobookLibraryType(folder.Type) {
if _, seedErr := h.SectionRepo.EnsureHomeContinueListeningSection(r.Context()); seedErr != nil {
slog.Warn("ensure home continue listening section", "library_id", folder.ID, "error", seedErr)
}
}
if _, seedErr := h.SectionRepo.CreateGeneratedHomeLibraryRecentSections(r.Context(), folder.ID, folder.Name, folder.Type); seedErr != nil {
slog.Warn("seed generated home sections for new library", "library_id", folder.ID, "error", seedErr)
}
}
// Seed default provider chain from plugin manifest defaults.
if h.ChainRepo != nil {
entries := h.seedDefaultChain(r.Context(), req.Type)
if len(entries) > 0 {
if seedErr := h.ChainRepo.SetChain(r.Context(), folder.ID, entries); seedErr != nil {
slog.Warn("seed default chain failed", "folder_id", folder.ID, "error", seedErr)
}
}
}
// Kick off an initial scan so content appears immediately.
if h.ScanQueue != nil {
if _, err := h.ScanQueue.EnqueueLibraryScan(r.Context(), folder.ID, "library_created"); err != nil {
slog.Warn("queue initial library scan failed", "library_id", folder.ID, "error", err)
}
} else {
initialScanID := ulid.Make().String()
h.recordAcceptedScan(initialScanID, &scantrigger.Target{
Folder: folder,
Mode: scantrigger.ModeLibrary,
Trigger: "library_created",
})
h.runFolderScanAsync(initialScanID, folder, "library_created")
}
writeJSON(w, http.StatusCreated, h.toLibraryResponseWithPoster(r.Context(), folder))
}
// HandleUpdateLibrary handles PUT /libraries/{id}.
func (h *LibraryHandler) HandleUpdateLibrary(w http.ResponseWriter, r *http.Request) {
id, err := parseIDParam(r)
if err != nil {
writeError(w, http.StatusBadRequest, "bad_request", "Invalid library ID")
return
}
var req updateLibraryRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
writeError(w, http.StatusBadRequest, "bad_request", "Invalid request body")
return
}
if req.MetadataLanguage != nil && *req.MetadataLanguage != "" && !validMetadataLanguages[*req.MetadataLanguage] {
writeError(w, http.StatusBadRequest, "bad_request", "Invalid metadata_language; must be a valid ISO 639-1 code")
return
}
if req.ChapterThumbnailsEnabled != nil && *req.ChapterThumbnailsEnabled && h.S3Meta == nil {
writeError(w, http.StatusBadRequest, "bad_request", "Chapter thumbnails require configured public asset S3 storage")
return
}
// Fetch the folder before updating so we can detect path changes.
oldFolder, err := h.folderRepo.GetByID(r.Context(), id)
if err != nil {
if errors.Is(err, catalog.ErrFolderNotFound) {
writeError(w, http.StatusNotFound, "not_found", "Library not found")
return
}
slog.Error("fetching library for update", "error", err)
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to fetch library")
return
}
err = h.folderRepo.Update(r.Context(), id, catalog.UpdateFolderInput{
Paths: req.Paths,
Type: req.Type,
Name: req.Name,
Enabled: req.Enabled,
MetadataLanguage: req.MetadataLanguage,
AutoTranslateMetadata: req.AutoTranslateMetadata,
ChapterThumbnailsEnabled: req.ChapterThumbnailsEnabled,
IntroDetectionEnabled: req.IntroDetectionEnabled,
})
if err != nil {
if errors.Is(err, catalog.ErrFolderNotFound) {
writeError(w, http.StatusNotFound, "not_found", "Library not found")
return
}
if errors.Is(err, catalog.ErrDuplicatePath) {
writeError(w, http.StatusConflict, "conflict", "A library with this path already exists")
return
}
slog.Error("updating library", "error", err)
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to update library")
return
}
// Fetch the updated folder to return it.
folder, err := h.folderRepo.GetByID(r.Context(), id)
if err != nil {
slog.Error("fetching updated library", "error", err)
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to fetch updated library")
return
}
if h.SectionRepo != nil && oldFolder.Name != folder.Name {
if syncErr := h.SectionRepo.SyncGeneratedHomeLibraryRecentTitles(r.Context(), id, oldFolder.Name, folder.Name); syncErr != nil {
slog.Warn("sync generated home section titles", "library_id", id, "error", syncErr)
}
}
// Rescan when paths have changed (folders added or removed).
if req.Paths != nil && !slices.Equal(oldFolder.Paths, *req.Paths) {
if h.ScanQueue != nil {
if _, err := h.ScanQueue.EnqueueLibraryScan(r.Context(), folder.ID, "library_paths_changed"); err != nil {
slog.Warn("queue library path-change scan failed", "library_id", folder.ID, "error", err)
}
} else {
updateScanID := ulid.Make().String()
h.recordAcceptedScan(updateScanID, &scantrigger.Target{
Folder: folder,
Mode: scantrigger.ModeLibrary,
Trigger: "library_paths_changed",
})
h.runFolderScanAsync(updateScanID, folder, "library_paths_changed")
}
}
writeJSON(w, http.StatusOK, h.toLibraryResponseWithPoster(r.Context(), folder))
}
// HandleDeleteLibrary handles DELETE /libraries/{id}.
func (h *LibraryHandler) HandleDeleteLibrary(w http.ResponseWriter, r *http.Request) {
id, err := parseIDParam(r)
if err != nil {
writeError(w, http.StatusBadRequest, "bad_request", "Invalid library ID")
return
}
if h.JobRepo == nil {
writeError(w, http.StatusServiceUnavailable, "unavailable", "Library delete jobs are not configured")
return
}
folder, err := h.folderRepo.GetByID(r.Context(), id)
if err != nil {
if errors.Is(err, catalog.ErrFolderNotFound) {
writeError(w, http.StatusNotFound, "not_found", "Library not found")
return
}
slog.Error("fetching library before delete", "library_id", id, "error", err)
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to load library")
return
}
wasEnabled := folder.Enabled
if wasEnabled {
disabled := false
if err := h.folderRepo.Update(r.Context(), folder.ID, catalog.UpdateFolderInput{Enabled: &disabled}); err != nil {
slog.Error("disabling library before delete", "library_id", folder.ID, "error", err)
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to prepare library deletion")
return
}
folder.Enabled = false
}
job, err := h.JobRepo.Create(r.Context(), adminjob.CreateJobInput{
JobType: adminjob.JobTypeDeleteLibrary,
CreatedByUserID: currentAdminUserID(r),
RequestPayload: adminjob.DeleteLibraryRequest{
LibraryID: folder.ID,
LibraryName: folder.Name,
},
Message: "Queued library deletion",
})
if err != nil {
if wasEnabled {
enabled := true
if revertErr := h.folderRepo.Update(r.Context(), folder.ID, catalog.UpdateFolderInput{Enabled: &enabled}); revertErr != nil {
slog.Error("re-enabling library after failed delete queue",
"library_id", folder.ID,
"queue_error", err,
"revert_error", revertErr,
)
}
}
var conflict *adminjob.ActiveJobConflictError
if errors.As(err, &conflict) {
jobsHandler := NewAdminJobsHandler(nil, nil)
writeAdminJobConflict(w, "A library deletion is already queued or running", conflict.Job, jobsHandler, r)
return
}
slog.Error("queuing library delete job", "library_id", id, "error", err)
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to queue library delete")
return
}
if h.ingester != nil {
cancelled := h.ingester.CancelLibrary(folder.ID)
slog.Info("library delete: cancelled running scans", "library_id", folder.ID, "cancelled", cancelled)
}
if h.ScanQueue != nil {
queuedCancelled, err := h.ScanQueue.CancelAcceptedByLibrary(r.Context(), folder.ID)
if err != nil {
slog.Warn("library delete: failed to cancel queued scans", "library_id", folder.ID, "error", err)
} else if queuedCancelled > 0 {
slog.Info("library delete: cancelled queued scans", "library_id", folder.ID, "cancelled", queuedCancelled)
}
}
publishEventJob(r.Context(), h.EventsHub, "job.created", job)
writeJSON(w, http.StatusAccepted, adminJobToResponse(r, job, nil))
}
// HandleCheckLibraryMount handles POST /libraries/{id}/check-mount.
// It verifies that each configured library root exists and can be listed.
func (h *LibraryHandler) HandleCheckLibraryMount(w http.ResponseWriter, r *http.Request) {
id, err := parseIDParam(r)
if err != nil {
writeError(w, http.StatusBadRequest, "bad_request", "Invalid library ID")
return
}
folder, err := h.folderRepo.GetByID(r.Context(), id)
if err != nil {
if errors.Is(err, catalog.ErrFolderNotFound) {
writeError(w, http.StatusNotFound, "not_found", "Library not found")
return
}
slog.Error("fetching library for mount check", "error", err)
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to fetch library")
return
}
resp := checkLibraryMount(folder)
if resp.Healthy && folder.ScanWarningCode != nil && *folder.ScanWarningCode == "empty_root" {
if err := h.folderRepo.ClearScanWarning(r.Context(), folder.ID); err != nil {
if errors.Is(err, catalog.ErrFolderNotFound) {
writeError(w, http.StatusNotFound, "not_found", "Library not found")
return
}
slog.Error("clearing empty-root warning after successful mount check", "library_id", folder.ID, "error", err)
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to clear library warning")
return
}
}
writeJSON(w, http.StatusOK, resp)
}
// HandleScan handles POST /scan. It accepts either a library_id, a path, or both
// and dispatches to full-library, subtree, or single-file scanning.
func (h *LibraryHandler) HandleScan(w http.ResponseWriter, r *http.Request) {
var req scanRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
writeError(w, http.StatusBadRequest, "bad_request", "Invalid request body")
return
}
target, err := scantrigger.NewResolver(h.folderRepo).Resolve(r.Context(), scantrigger.Request{
LibraryID: req.LibraryID,
Path: req.Path,
})
if err != nil {
var reqErr *scantrigger.RequestError
if errors.As(err, &reqErr) {
writeError(w, reqErr.Status, reqErr.Code, reqErr.Message)
return
}
slog.Error("resolving scan target", "error", err)
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to resolve scan target")
return
}
if h.ScanQueue != nil {
if _, err := h.ScanQueue.EnqueueScan(r.Context(), target.Folder.ID, target.Mode, target.Path, target.Trigger); err != nil {
slog.Error("queueing library scan", "library_id", target.Folder.ID, "error", err)
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to queue scan")
return
}
} else if h.ingester != nil {
scanID := ulid.Make().String()
h.recordAcceptedScan(scanID, target)
switch target.Mode {
case scantrigger.ModeFile:
h.runFileScanAsync(scanID, target.Folder, target.Path, target.Trigger)
case scantrigger.ModeSubtree:
h.runSubtreeScanAsync(scanID, target.Folder, target.Path, target.Trigger)
default:
h.runFolderScanAsync(scanID, target.Folder, target.Trigger)
}
} else {
writeError(w, http.StatusServiceUnavailable, "unavailable", "Scanner not available")
return
}
writeJSON(w, http.StatusAccepted, scanResponse{
Status: "accepted",
Mode: target.Mode,
LibraryID: target.Folder.ID,
})
}
// HandleScanCancel handles POST /scan/cancel. It cancels all running scans
// for a given library.
func (h *LibraryHandler) HandleScanCancel(w http.ResponseWriter, r *http.Request) {
var req scanCancelRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
writeError(w, http.StatusBadRequest, "bad_request", "Invalid request body")
return
}
if req.LibraryID <= 0 {
writeError(w, http.StatusBadRequest, "bad_request", "library_id is required")
return
}
if h.ingester == nil && h.ScanQueue == nil {
writeError(w, http.StatusServiceUnavailable, "unavailable", "Scanner not available")
return
}
cancelled := 0
if h.ScanQueue != nil {
queuedCancelled, err := h.ScanQueue.CancelByLibrary(r.Context(), req.LibraryID)
if err != nil {
slog.Error("cancel library scans", "library_id", req.LibraryID, "error", err)
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to cancel scans")
return
}
cancelled += queuedCancelled
}
if h.ingester != nil {
cancelled += h.ingester.CancelLibrary(req.LibraryID)
}
for _, run := range h.cancelActiveScans(req.LibraryID) {
h.publishScanEvent(r.Context(), "scan.cancelled", run)
}
slog.Info("scan: cancelled running scans",
"library_id", req.LibraryID,
"cancelled", cancelled,
)
writeJSON(w, http.StatusOK, scanCancelResponse{
Cancelled: cancelled,
LibraryID: req.LibraryID,
})
}
func (h *LibraryHandler) runFolderScanAsync(scanID string, folder *models.MediaFolder, trigger string) {
go func() {
h.markScanRunning(scanID)
slog.Info("scan: starting library scan",
"trigger", trigger,
"library_id", folder.ID,
"name", folder.Name,
"paths", folder.Paths,
)
start := time.Now()
result, ingestErr := h.ingester.IngestFolder(h.appCtx, folder)
if ingestErr != nil {
if errors.Is(ingestErr, context.Canceled) {
h.markScanCancelled(scanID)
slog.Info("scan: library scan canceled",
"trigger", trigger,
"library_id", folder.ID,
"elapsed", time.Since(start).Round(time.Millisecond),
)
return
}
h.markScanFailed(scanID, ingestErr)
slog.Error("scan: library ingest failed",
"trigger", trigger,
"library_id", folder.ID,
"paths", folder.Paths,
"error", ingestErr,
"elapsed", time.Since(start).Round(time.Millisecond),
)
return
}
h.markScanCompleted(scanID, result)
slog.Info("scan: library ingest complete",
"trigger", trigger,
"library_id", folder.ID,
"name", folder.Name,
"new", scanMetric(result, func(r *scanner.ScanResult) int { return r.New }),
"updated", scanMetric(result, func(r *scanner.ScanResult) int { return r.Updated }),
"unchanged", scanMetric(result, func(r *scanner.ScanResult) int { return r.Unchanged }),
"missing", scanMetric(result, func(r *scanner.ScanResult) int { return r.Missing }),
"files_deleted", scanMetric(result, func(r *scanner.ScanResult) int { return r.FilesDeleted }),
"memberships_removed", scanMetric(result, func(r *scanner.ScanResult) int { return r.MembershipsRemoved }),
"items_deleted", scanMetric(result, func(r *scanner.ScanResult) int { return r.ItemsDeleted }),
"empty_root_guarded", scanBoolMetric(result, func(r *scanner.ScanResult) bool { return r.EmptyRootGuarded }),
"errors", scanMetric(result, func(r *scanner.ScanResult) int { return r.Errors }),
"matched_files", result.MatchedFiles,
"retried_items", result.RetriedItems,
"still_unmatched_warnings", result.StillUnmatchedWarnings,
"skipped", result.Skipped,
"elapsed", time.Since(start).Round(time.Millisecond),
)
}()
}
func (h *LibraryHandler) runSubtreeScanAsync(scanID string, folder *models.MediaFolder, subtreePath, trigger string) {
go func() {
h.markScanRunning(scanID)
slog.Info("scan: starting subtree scan",
"trigger", trigger,
"library_id", folder.ID,
"name", folder.Name,
"path", subtreePath,
)
start := time.Now()
result, ingestErr := h.ingester.IngestSubtree(h.appCtx, folder, subtreePath)
if ingestErr != nil {
if errors.Is(ingestErr, context.Canceled) {
h.markScanCancelled(scanID)
slog.Info("scan: subtree scan canceled",
"trigger", trigger,
"library_id", folder.ID,
"path", subtreePath,
"elapsed", time.Since(start).Round(time.Millisecond),
)
return
}
h.markScanFailed(scanID, ingestErr)
slog.Error("scan: subtree ingest failed",
"trigger", trigger,
"library_id", folder.ID,
"path", subtreePath,
"error", ingestErr,
"elapsed", time.Since(start).Round(time.Millisecond),
)
return
}
h.markScanCompleted(scanID, result)
slog.Info("scan: subtree ingest complete",
"trigger", trigger,
"library_id", folder.ID,
"name", folder.Name,
"path", subtreePath,
"new", scanMetric(result, func(r *scanner.ScanResult) int { return r.New }),
"updated", scanMetric(result, func(r *scanner.ScanResult) int { return r.Updated }),
"unchanged", scanMetric(result, func(r *scanner.ScanResult) int { return r.Unchanged }),
"missing", scanMetric(result, func(r *scanner.ScanResult) int { return r.Missing }),
"files_deleted", scanMetric(result, func(r *scanner.ScanResult) int { return r.FilesDeleted }),
"memberships_removed", scanMetric(result, func(r *scanner.ScanResult) int { return r.MembershipsRemoved }),
"items_deleted", scanMetric(result, func(r *scanner.ScanResult) int { return r.ItemsDeleted }),
"errors", scanMetric(result, func(r *scanner.ScanResult) int { return r.Errors }),
"matched_files", result.MatchedFiles,
"retried_items", result.RetriedItems,
"still_unmatched_warnings", result.StillUnmatchedWarnings,
"skipped", result.Skipped,
"elapsed", time.Since(start).Round(time.Millisecond),
)
}()
}
func (h *LibraryHandler) runFileScanAsync(scanID string, folder *models.MediaFolder, filePath, trigger string) {
go func() {
h.markScanRunning(scanID)
slog.Info("scan: starting file scan",
"trigger", trigger,
"library_id", folder.ID,
"name", folder.Name,
"path", filePath,
)
result, ingestErr := h.ingester.IngestFile(h.appCtx, folder, filePath)
if ingestErr != nil {
if errors.Is(ingestErr, context.Canceled) {
h.markScanCancelled(scanID)
slog.Info("scan: file scan canceled",
"trigger", trigger,
"library_id", folder.ID,
"path", filePath,
)
return
}
h.markScanFailed(scanID, ingestErr)
slog.Error("scan: file ingest failed",
"trigger", trigger,
"library_id", folder.ID,
"path", filePath,
"error", ingestErr,
)
return
}
h.markScanCompleted(scanID, result)
slog.Info("scan: file ingest complete",
"trigger", trigger,
"library_id", folder.ID,
"name", folder.Name,
"path", filePath,
"matched_files", result.MatchedFiles,
"retried_items", result.RetriedItems,
"still_unmatched_warnings", result.StillUnmatchedWarnings,
"skipped", result.Skipped,
)
}()
}
func (h *LibraryHandler) recordAcceptedScan(scanID string, target *scantrigger.Target) {
if h == nil || h.ScanRegistry == nil || target == nil || target.Folder == nil {
return
}
h.ScanRegistry.Upsert(evt.ScanRun{
ID: scanID,
LibraryID: target.Folder.ID,
Mode: target.Mode,
Path: target.Path,
Trigger: target.Trigger,
Status: "accepted",
})
if run, ok := h.ScanRegistry.Get(scanID); ok {
h.publishScanEvent(context.Background(), "scan.accepted", run)
}
}
func (h *LibraryHandler) markScanRunning(scanID string) {
if h == nil || h.ScanRegistry == nil {
return
}
run, ok := h.ScanRegistry.Get(scanID)
if !ok {
return
}
now := time.Now().UTC()
run.Status = "running"
run.StartedAt = &now
h.ScanRegistry.Upsert(run)
h.publishScanEvent(context.Background(), "scan.started", run)
}
func (h *LibraryHandler) markScanCompleted(scanID string, result *libraryingest.Result) {
if h == nil || h.ScanRegistry == nil {
return
}
run, ok := h.ScanRegistry.Get(scanID)
if !ok {
return
}
now := time.Now().UTC()
run.Status = "completed"
run.CompletedAt = &now
run.Result = scanRunResultFromIngest(result)
h.ScanRegistry.MarkTerminal(run)
h.publishScanEvent(context.Background(), "scan.completed", run)
}
func (h *LibraryHandler) markScanFailed(scanID string, err error) {
if h == nil || h.ScanRegistry == nil {
return
}
run, ok := h.ScanRegistry.Get(scanID)
if !ok {
return
}
now := time.Now().UTC()
run.Status = "failed"
run.CompletedAt = &now
if err != nil {
run.ErrorMessage = err.Error()
}
h.ScanRegistry.MarkTerminal(run)
h.publishScanEvent(context.Background(), "scan.failed", run)
}
func (h *LibraryHandler) markScanCancelled(scanID string) {
if h == nil || h.ScanRegistry == nil {
return
}
run, ok := h.ScanRegistry.Get(scanID)
if !ok {
return
}
now := time.Now().UTC()
run.Status = "cancelled"
run.CompletedAt = &now
h.ScanRegistry.MarkTerminal(run)
h.publishScanEvent(context.Background(), "scan.cancelled", run)
}
func (h *LibraryHandler) cancelActiveScans(libraryID int) []evt.ScanRun {
if h == nil || h.ScanRegistry == nil {
return nil
}
return h.ScanRegistry.CancelLibrary(libraryID, time.Now().UTC())
}
func (h *LibraryHandler) publishScanEvent(ctx context.Context, eventName string, run evt.ScanRun) {
if h == nil || h.EventsHub == nil {
return
}
_ = h.EventsHub.PublishJSON(ctx, evt.ChannelScans, eventName, run, evt.PublishOptions{
AdminOnly: true,
})
}
func scanRunResultFromIngest(result *libraryingest.Result) *evt.ScanRunResult {
if result == nil {
return nil
}
resp := &evt.ScanRunResult{
MatchedFiles: result.MatchedFiles,
RetriedItems: result.RetriedItems,
StillUnmatchedWarnings: result.StillUnmatchedWarnings,
}
if result.Skipped {
resp.Skipped = 1
}
if result.ScanResult != nil {
resp.New = result.ScanResult.New
resp.Updated = result.ScanResult.Updated
resp.Unchanged = result.ScanResult.Unchanged
resp.Missing = result.ScanResult.Missing
resp.FilesDeleted = result.ScanResult.FilesDeleted
resp.MembershipsRemoved = result.ScanResult.MembershipsRemoved
resp.ItemsDeleted = result.ScanResult.ItemsDeleted
resp.Errors = result.ScanResult.Errors
}
return resp
}
func scanMetric(result *libraryingest.Result, pick func(*scanner.ScanResult) int) int {
if result == nil || result.ScanResult == nil {
return 0
}
return pick(result.ScanResult)
}
func scanBoolMetric(result *libraryingest.Result, pick func(*scanner.ScanResult) bool) bool {
if result == nil || result.ScanResult == nil {
return false
}
return pick(result.ScanResult)
}
func checkLibraryMount(folder *models.MediaFolder) libraryMountCheckResponse {
resp := libraryMountCheckResponse{
Status: "ok",
LibraryID: folder.ID,
LibraryName: folder.Name,
Healthy: true,
CheckedAt: time.Now().UTC(),
Roots: make([]libraryMountCheckRootResponse, 0, len(folder.Paths)),
}
if len(folder.Paths) == 0 {
resp.Healthy = false
resp.Summary = "Library has no configured roots"
return resp
}
unreachable := 0
for _, path := range folder.Paths {
root := libraryMountCheckRootResponse{
Path: path,
Reachable: true,
}
info, err := os.Stat(path)
if err != nil {
code, message := classifyMountCheckError(err, false)
root.Reachable = false
root.ErrorCode = stringPtr(code)
root.ErrorMessage = stringPtr(message)
} else if !info.IsDir() {
root.Reachable = false
root.ErrorCode = stringPtr("not_directory")
root.ErrorMessage = stringPtr("Path is not a directory")
} else if _, err := os.ReadDir(path); err != nil {
code, message := classifyMountCheckError(err, true)
root.Reachable = false
root.ErrorCode = stringPtr(code)
root.ErrorMessage = stringPtr(message)
}
if !root.Reachable {
unreachable++
resp.Healthy = false
}
resp.Roots = append(resp.Roots, root)
}
switch unreachable {
case 0:
resp.Summary = "All configured roots are reachable"
case 1:
resp.Summary = fmt.Sprintf("1 of %d roots unreachable", len(folder.Paths))
default:
resp.Summary = fmt.Sprintf("%d of %d roots unreachable", unreachable, len(folder.Paths))
}
return resp
}
func classifyMountCheckError(err error, isRead bool) (string, string) {
switch {
case errors.Is(err, os.ErrNotExist):
return "not_found", "Path does not exist"
case errors.Is(err, os.ErrPermission):
return "permission_denied", "Permission denied"
case isRead:
return "read_failed", "Failed to read directory"
default:
return "stat_failed", "Failed to stat path"
}
}
func stringPtr(v string) *string {
return &v
}
func boolPtr(v bool) *bool {
return &v
}
func (h *LibraryHandler) publishCatalogStatsInvalidation(eventType, payload string) {
if h.EventBus == nil {
return
}
if err := h.EventBus.Publish(h.appCtx, cache.ChannelCatalog, cache.Event{Type: eventType, Payload: payload}); err != nil {
slog.Warn("scan: failed to publish catalog invalidation event",
"type", eventType,
"payload", payload,
"error", err,
)
}
}
type refreshLibraryMetadataRequest struct {
Mode string `json:"mode"`
}
type libraryMetadataMatchQueueStatusResponse struct {
LibraryID int `json:"library_id"`
MovieCount int `json:"movie_count"`
SeriesCount int `json:"series_count"`
RawFileCount int `json:"raw_file_count"`
TotalCount int `json:"total_count"`
}
type libraryMetadataMatchQueueActionResponse struct {
Status string `json:"status"`
LibraryID int `json:"library_id"`
MovieCancelled int `json:"movie_cancelled,omitempty"`
SeriesCancelled int `json:"series_cancelled,omitempty"`
RawFileCancelled int `json:"raw_file_cancelled,omitempty"`
RawFileRetried int `json:"raw_file_retried,omitempty"`
TotalCancelled int `json:"total_cancelled,omitempty"`
Queue libraryMetadataMatchQueueStatusResponse `json:"queue"`
}
type libraryMetadataMatchQueueDetailResponse struct {
libraryMetadataMatchQueueStatusResponse
Movies []libraryMovieMatchQueueEntryResponse `json:"movies"`
Series []librarySeriesMatchQueueEntryResponse `json:"series"`
RawFiles []libraryRawMatchBacklogEntryResponse `json:"raw_files"`
}
type libraryMovieMatchQueueEntryResponse struct {
MediaFileID int `json:"media_file_id"`
MediaFolderID int `json:"media_folder_id"`
FilePath string `json:"file_path"`
FirstQueuedAt time.Time `json:"first_queued_at"`
AvailableAt time.Time `json:"available_at"`
LastAttemptedAt *time.Time `json:"last_attempted_at,omitempty"`
AttemptCount int `json:"attempt_count"`
LastError string `json:"last_error,omitempty"`
UpdatedAt time.Time `json:"updated_at"`
}
type librarySeriesMatchQueueEntryResponse struct {
MediaFolderID int `json:"media_folder_id"`
ObservedRootPath string `json:"observed_root_path"`
FirstQueuedAt time.Time `json:"first_queued_at"`
AvailableAt time.Time `json:"available_at"`
LastAttemptedAt *time.Time `json:"last_attempted_at,omitempty"`
AttemptCount int `json:"attempt_count"`
LastError string `json:"last_error,omitempty"`
UpdatedAt time.Time `json:"updated_at"`
}
type libraryRawMatchBacklogEntryResponse struct {
MediaFileID int `json:"media_file_id"`
MediaFolderID int `json:"media_folder_id"`
FilePath string `json:"file_path"`
BaseTitle string `json:"base_title,omitempty"`
BaseYear int `json:"base_year,omitempty"`
BaseType string `json:"base_type,omitempty"`
LastAttemptedAt *time.Time `json:"last_attempted_at,omitempty"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
func (h *LibraryHandler) HandleListMetadataMatchQueues(w http.ResponseWriter, r *http.Request) {
if h.folderRepo == nil {
writeError(w, http.StatusServiceUnavailable, "unavailable", "Library repository is not configured")
return
}
folders, err := h.folderRepo.List(r.Context())
if err != nil {
slog.Error("metadata queue: failed to list libraries", "error", err)
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to list metadata matcher queues")
return
}
resp := make([]libraryMetadataMatchQueueStatusResponse, 0, len(folders))
for _, folder := range folders {
if folder == nil {
continue
}
status, err := h.metadataMatchQueueStatus(r.Context(), folder.ID)
if err != nil {
slog.Error("metadata queue: failed to load queue status", "library_id", folder.ID, "error", err)
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to load metadata matcher queue")
return
}
resp = append(resp, status)
}
writeJSON(w, http.StatusOK, resp)
}
func (h *LibraryHandler) HandleGetMetadataMatchQueue(w http.ResponseWriter, r *http.Request) {
if !h.metadataMatchBacklogConfigured() {
writeError(w, http.StatusServiceUnavailable, "unavailable", "Metadata matcher backlog is not configured")
return
}
id, err := parseIDParam(r)
if err != nil {
writeError(w, http.StatusBadRequest, "bad_request", "Invalid library ID")
return
}
if _, err := h.folderRepo.GetByID(r.Context(), id); err != nil {
if errors.Is(err, catalog.ErrFolderNotFound) {
writeError(w, http.StatusNotFound, "not_found", "Library not found")
return
}
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to fetch library")
return
}
limit := 10
if value := strings.TrimSpace(r.URL.Query().Get("limit")); value != "" {
if parsed, parseErr := strconv.Atoi(value); parseErr == nil && parsed > 0 && parsed <= 50 {
limit = parsed
}
}
status, err := h.metadataMatchQueueStatus(r.Context(), id)
if err != nil {
slog.Error("metadata queue: failed to load queue status", "library_id", id, "error", err)
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to load metadata matcher queue")
return
}
resp := libraryMetadataMatchQueueDetailResponse{
libraryMetadataMatchQueueStatusResponse: status,
Movies: []libraryMovieMatchQueueEntryResponse{},
Series: []librarySeriesMatchQueueEntryResponse{},
RawFiles: []libraryRawMatchBacklogEntryResponse{},
}
if h.MovieMatchQueueRepo != nil {
movies, _, err := h.MovieMatchQueueRepo.ListByFolder(r.Context(), id, limit, 0)
if err != nil {
slog.Error("metadata queue: failed to list movie queue", "library_id", id, "error", err)
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to list metadata matcher queue")
return
}
for _, entry := range movies {
resp.Movies = append(resp.Movies, libraryMovieMatchQueueEntryResponse{
MediaFileID: entry.MediaFileID,
MediaFolderID: entry.MediaFolderID,
FilePath: entry.FilePath,
FirstQueuedAt: entry.FirstQueuedAt,
AvailableAt: entry.AvailableAt,
LastAttemptedAt: entry.LastAttemptedAt,
AttemptCount: entry.AttemptCount,
LastError: entry.LastError,
UpdatedAt: entry.UpdatedAt,
})
}
}
if h.SeriesMatchQueueRepo != nil {
series, _, err := h.SeriesMatchQueueRepo.ListByFolder(r.Context(), id, limit, 0)
if err != nil {
slog.Error("metadata queue: failed to list series queue", "library_id", id, "error", err)
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to list metadata matcher queue")
return
}
for _, entry := range series {
resp.Series = append(resp.Series, librarySeriesMatchQueueEntryResponse{
MediaFolderID: entry.MediaFolderID,
ObservedRootPath: entry.ObservedRootPath,
FirstQueuedAt: entry.FirstQueuedAt,
AvailableAt: entry.AvailableAt,
LastAttemptedAt: entry.LastAttemptedAt,
AttemptCount: entry.AttemptCount,
LastError: entry.LastError,
UpdatedAt: entry.UpdatedAt,
})
}
}
if h.RawMatchBacklogRepo != nil {
rawFiles, _, err := h.RawMatchBacklogRepo.ListUnmatchedMatchBacklogByFolder(r.Context(), id, h.rawMatchBacklogMode(), limit, 0)
if err != nil {
slog.Error("metadata queue: failed to list raw backlog", "library_id", id, "error", err)
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to list metadata matcher backlog")
return
}
for _, file := range rawFiles {
if file == nil {
continue
}
resp.RawFiles = append(resp.RawFiles, libraryRawMatchBacklogEntryResponse{
MediaFileID: file.ID,
MediaFolderID: file.MediaFolderID,
FilePath: file.FilePath,
BaseTitle: file.BaseTitle,
BaseYear: file.BaseYear,
BaseType: file.BaseType,
LastAttemptedAt: file.MatchAttemptedAt,
CreatedAt: file.CreatedAt,
UpdatedAt: file.UpdatedAt,
})
}
}
writeJSON(w, http.StatusOK, resp)
}
func (h *LibraryHandler) HandleRetryMetadataMatchQueue(w http.ResponseWriter, r *http.Request) {
if !h.metadataMatchBacklogConfigured() {
writeError(w, http.StatusServiceUnavailable, "unavailable", "Metadata matcher backlog is not configured")
return
}
id, err := parseIDParam(r)
if err != nil {
writeError(w, http.StatusBadRequest, "bad_request", "Invalid library ID")
return
}
if _, err := h.folderRepo.GetByID(r.Context(), id); err != nil {
if errors.Is(err, catalog.ErrFolderNotFound) {
writeError(w, http.StatusNotFound, "not_found", "Library not found")
return
}
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to fetch library")
return
}
if h.SeriesMatchQueueRepo != nil {
if err := h.SeriesMatchQueueRepo.SyncForFolder(r.Context(), id); err != nil {
slog.Error("metadata queue: failed to retry series queue", "library_id", id, "error", err)
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to retry metadata matcher")
return
}
}
if h.MovieMatchQueueRepo != nil {
if err := h.MovieMatchQueueRepo.SyncForFolder(r.Context(), id); err != nil {
slog.Error("metadata queue: failed to retry movie queue", "library_id", id, "error", err)
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to retry metadata matcher")
return
}
}
rawFileRetried := 0
if h.RawMatchBacklogRepo != nil {
rawFileRetried, err = h.RawMatchBacklogRepo.RetryUnmatchedMatchBacklogByFolder(r.Context(), id, h.rawMatchBacklogMode())
if err != nil {
slog.Error("metadata queue: failed to retry raw backlog", "library_id", id, "error", err)
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to retry metadata matcher")
return
}
}
status, err := h.metadataMatchQueueStatus(r.Context(), id)
if err != nil {
slog.Error("metadata queue: failed to load retried queue status", "library_id", id, "error", err)
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to load metadata matcher queue")
return
}
writeJSON(w, http.StatusOK, libraryMetadataMatchQueueActionResponse{
Status: "queued",
LibraryID: id,
RawFileRetried: rawFileRetried,
Queue: status,
})
}
func (h *LibraryHandler) HandleCancelMetadataMatchQueue(w http.ResponseWriter, r *http.Request) {
if !h.metadataMatchBacklogConfigured() {
writeError(w, http.StatusServiceUnavailable, "unavailable", "Metadata matcher backlog is not configured")
return
}
id, err := parseIDParam(r)
if err != nil {
writeError(w, http.StatusBadRequest, "bad_request", "Invalid library ID")
return
}
if _, err := h.folderRepo.GetByID(r.Context(), id); err != nil {
if errors.Is(err, catalog.ErrFolderNotFound) {
writeError(w, http.StatusNotFound, "not_found", "Library not found")
return
}
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to fetch library")
return
}
seriesCancelled := 0
if h.SeriesMatchQueueRepo != nil {
seriesCancelled, err = h.SeriesMatchQueueRepo.DeleteByFolder(r.Context(), id)
if err != nil {
slog.Error("metadata queue: failed to cancel series queue", "library_id", id, "error", err)
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to cancel metadata matcher")
return
}
}
movieCancelled := 0
if h.MovieMatchQueueRepo != nil {
movieCancelled, err = h.MovieMatchQueueRepo.DeleteByFolder(r.Context(), id)
if err != nil {
slog.Error("metadata queue: failed to cancel movie queue", "library_id", id, "error", err)
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to cancel metadata matcher")
return
}
}
rawFileCancelled := 0
if h.RawMatchBacklogRepo != nil {
rawFileCancelled, err = h.RawMatchBacklogRepo.SuppressUnmatchedMatchBacklogByFolder(r.Context(), id, h.rawMatchBacklogMode())
if err != nil {
slog.Error("metadata queue: failed to suppress raw backlog", "library_id", id, "error", err)
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to cancel metadata matcher")
return
}
}
status, err := h.metadataMatchQueueStatus(r.Context(), id)
if err != nil {
slog.Error("metadata queue: failed to load cancelled queue status", "library_id", id, "error", err)
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to load metadata matcher queue")
return
}
writeJSON(w, http.StatusOK, libraryMetadataMatchQueueActionResponse{
Status: "cancelled",
LibraryID: id,
MovieCancelled: movieCancelled,
SeriesCancelled: seriesCancelled,
RawFileCancelled: rawFileCancelled,
TotalCancelled: movieCancelled + seriesCancelled + rawFileCancelled,
Queue: status,
})
}
func (h *LibraryHandler) metadataMatchBacklogConfigured() bool {
return h.folderRepo != nil &&
(h.MovieMatchQueueRepo != nil || h.SeriesMatchQueueRepo != nil || h.RawMatchBacklogRepo != nil)
}
func (h *LibraryHandler) metadataMatchQueueStatus(ctx context.Context, libraryID int) (libraryMetadataMatchQueueStatusResponse, error) {
resp := libraryMetadataMatchQueueStatusResponse{LibraryID: libraryID}
if h.MovieMatchQueueRepo != nil {
count, err := h.MovieMatchQueueRepo.CountByFolder(ctx, libraryID)
if err != nil {
return resp, err
}
resp.MovieCount = count
}
if h.SeriesMatchQueueRepo != nil {
count, err := h.SeriesMatchQueueRepo.CountByFolder(ctx, libraryID)
if err != nil {
return resp, err
}
resp.SeriesCount = count
}
if h.RawMatchBacklogRepo != nil {
count, err := h.RawMatchBacklogRepo.CountUnmatchedMatchBacklogByFolder(ctx, libraryID, h.rawMatchBacklogMode())
if err != nil {
return resp, err
}
resp.RawFileCount = count
}
resp.TotalCount = resp.MovieCount + resp.SeriesCount + resp.RawFileCount
return resp, nil
}
func (h *LibraryHandler) rawMatchBacklogMode() scanner.RawMatchBacklogMode {
if h.TVSeriesRootQueue && h.MovieMatchQueueRepo != nil {
return scanner.RawMatchBacklogMixed
}
if h.TVSeriesRootQueue {
return scanner.RawMatchBacklogNonSeries
}
return scanner.RawMatchBacklogGeneric
}
// HandleRefreshLibraryMetadata handles POST /libraries/{id}/refresh-metadata.
// It queues a background admin job to refresh metadata for items in the
// specified library. Quick mode is the default.
func (h *LibraryHandler) HandleRefreshLibraryMetadata(w http.ResponseWriter, r *http.Request) {
if h.JobRepo == nil {
writeError(w, http.StatusServiceUnavailable, "unavailable", "Library refresh jobs are not configured")
return
}
id, err := parseIDParam(r)
if err != nil {
writeError(w, http.StatusBadRequest, "bad_request", "Invalid library ID")
return
}
mode := adminjob.LibraryRefreshModeQuick
if r.Body != nil {
var req refreshLibraryMetadataRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil && !errors.Is(err, io.EOF) {
writeError(w, http.StatusBadRequest, "bad_request", "Invalid request body")
return
}
if req.Mode != "" {
switch adminjob.LibraryRefreshMode(req.Mode) {
case adminjob.LibraryRefreshModeQuick, adminjob.LibraryRefreshModeFull:
mode = adminjob.LibraryRefreshMode(req.Mode)
default:
writeError(w, http.StatusBadRequest, "bad_request", "Invalid refresh mode")
return
}
}
}
folder, err := h.folderRepo.GetByID(r.Context(), id)
if err != nil {
if errors.Is(err, catalog.ErrFolderNotFound) {
writeError(w, http.StatusNotFound, "not_found", "Library not found")
return
}
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to fetch library")
return
}
job, err := h.JobRepo.CreateLibraryRefresh(r.Context(), currentAdminUserID(r), adminjob.LibraryRefreshRequest{
LibraryID: folder.ID,
LibraryName: folder.Name,
Mode: mode,
}, fmt.Sprintf("Queued %s library metadata refresh", mode))
if err != nil {
var conflict *adminjob.ActiveJobConflictError
if errors.As(err, &conflict) {
jobsHandler := NewAdminJobsHandler(nil, nil)
writeAdminJobConflict(w, "A metadata refresh is already queued or running for this library", conflict.Job, jobsHandler, r)
return
}
slog.Error("library: queue library refresh failed", "library_id", id, "error", err)
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to queue library metadata refresh")
return
}
publishEventJob(r.Context(), h.EventsHub, "job.created", job)
writeJSON(w, http.StatusAccepted, adminJobToResponse(r, job, nil))
}
// HandleConfirmEmptyRootCleanup handles POST /libraries/{id}/confirm-empty-root-cleanup.
// It arms the next empty-root scan for destructive cleanup.
func (h *LibraryHandler) HandleConfirmEmptyRootCleanup(w http.ResponseWriter, r *http.Request) {
id, err := parseIDParam(r)
if err != nil {
writeError(w, http.StatusBadRequest, "bad_request", "Invalid library ID")
return
}
if err := h.folderRepo.AllowEmptyCleanupOnce(r.Context(), id); err != nil {
if errors.Is(err, catalog.ErrFolderNotFound) {
writeError(w, http.StatusNotFound, "not_found", "Library not found")
return
}
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to confirm cleanup")
return
}
writeJSON(w, http.StatusOK, map[string]string{
"status": "ok",
"message": "Empty-root cleanup confirmed for next scan",
})
}
// --- Library poster handlers ---
// HandleUploadPoster handles PUT /libraries/{id}/poster.
// Accepts a multipart form upload with a single "poster" file field.
func (h *LibraryHandler) HandleUploadPoster(w http.ResponseWriter, r *http.Request) {
if h.S3Meta == nil {
writeError(w, http.StatusServiceUnavailable, "unavailable", "Image storage is not configured")
return
}
id, err := parseIDParam(r)
if err != nil {
writeError(w, http.StatusBadRequest, "bad_request", "Invalid library ID")
return
}
folder, err := h.folderRepo.GetByID(r.Context(), id)
if err != nil {
if errors.Is(err, catalog.ErrFolderNotFound) {
writeError(w, http.StatusNotFound, "not_found", "Library not found")
return
}
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to fetch library")
return
}
// Parse multipart form (max 10 MB).
if err := r.ParseMultipartForm(10 << 20); err != nil {
writeError(w, http.StatusBadRequest, "bad_request", "Invalid multipart form")
return
}
file, header, err := r.FormFile("poster")
if err != nil {
writeError(w, http.StatusBadRequest, "bad_request", "Missing poster file")
return
}
defer file.Close()
// Validate content type.
ct := header.Header.Get("Content-Type")
ext := posterExtension(ct)
if ext == "" {
writeError(w, http.StatusBadRequest, "bad_request", "Unsupported image type; use JPEG, PNG, or WebP")
return
}
data, err := io.ReadAll(io.LimitReader(file, 10<<20+1))
if err != nil {
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to read upload")
return
}
if len(data) > 10<<20 {
writeError(w, http.StatusRequestEntityTooLarge, "too_large", "Poster must be under 10 MB")
return
}
// Delete old poster if it exists and has a different key.
s3Key := fmt.Sprintf("library-posters/%d%s", id, ext)
if folder.PosterPath != "" && folder.PosterPath != s3Key {
_ = h.S3Meta.DeleteObject(r.Context(), h.S3Meta.Bucket(), folder.PosterPath)
}
if err := h.S3Meta.PutObject(r.Context(), h.S3Meta.Bucket(), s3Key, data); err != nil {
slog.Error("uploading library poster", "library_id", id, "error", err)
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to upload poster")
return
}
if err := h.folderRepo.SetPosterPath(r.Context(), id, s3Key); err != nil {
slog.Error("saving library poster path", "library_id", id, "error", err)
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to save poster")
return
}
folder.PosterPath = s3Key
writeJSON(w, http.StatusOK, h.toLibraryResponseWithPoster(r.Context(), folder))
}
// HandleDeletePoster handles DELETE /libraries/{id}/poster.
func (h *LibraryHandler) HandleDeletePoster(w http.ResponseWriter, r *http.Request) {
if h.S3Meta == nil {
writeError(w, http.StatusServiceUnavailable, "unavailable", "Image storage is not configured")
return
}
id, err := parseIDParam(r)
if err != nil {
writeError(w, http.StatusBadRequest, "bad_request", "Invalid library ID")
return
}
folder, err := h.folderRepo.GetByID(r.Context(), id)
if err != nil {
if errors.Is(err, catalog.ErrFolderNotFound) {
writeError(w, http.StatusNotFound, "not_found", "Library not found")
return
}
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to fetch library")
return
}
if folder.PosterPath != "" {
_ = h.S3Meta.DeleteObject(r.Context(), h.S3Meta.Bucket(), folder.PosterPath)
if err := h.folderRepo.ClearPosterPath(r.Context(), id); err != nil {
slog.Error("clearing library poster path", "library_id", id, "error", err)
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to clear poster")
return
}
}
w.WriteHeader(http.StatusNoContent)
}
// posterExtension returns the file extension for a valid poster content type.
func posterExtension(contentType string) string {
switch contentType {
case "image/jpeg":
return ".jpg"
case "image/png":
return ".png"
case "image/webp":
return ".webp"
default:
return ""
}
}
// --- Provider chain types ---
// chainLevelEntry represents a single entry in a per-level provider chain response.
type chainLevelEntry struct {
PluginInstallationID int `json:"plugin_installation_id"`
CapabilityID string `json:"capability_id"`
ProviderSlug string `json:"provider_slug"`
Priority int `json:"priority"`
Enabled bool `json:"enabled"`
}
// setChainLevelRequest is the JSON body for PUT /libraries/{id}/providers.
type setChainLevelRequest struct {
Levels map[string][]chainEntryInput `json:"levels"`
}
// chainEntryInput is a single entry in a set-chain request.
type chainEntryInput struct {
PluginInstallationID int `json:"plugin_installation_id"`
CapabilityID string `json:"capability_id"`
Priority int `json:"priority"`
Enabled bool `json:"enabled"`
}
// HandleGetLibraryProviders handles GET /libraries/{id}/providers.
// It returns the provider chain for the given library grouped by content level.
func (h *LibraryHandler) HandleGetLibraryProviders(w http.ResponseWriter, r *http.Request) {
if h.ChainRepo == nil {
writeError(w, http.StatusServiceUnavailable, "unavailable", "Provider chain management is not configured")
return
}
id, err := parseIDParam(r)
if err != nil {
writeError(w, http.StatusBadRequest, "bad_request", "Invalid library ID")
return
}
// Verify the library exists.
if _, err := h.folderRepo.GetByID(r.Context(), id); err != nil {
if errors.Is(err, catalog.ErrFolderNotFound) {
writeError(w, http.StatusNotFound, "not_found", "Library not found")
return
}
slog.Error("fetching library for provider chain", "error", err)
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to fetch library")
return
}
entries, err := h.ChainRepo.GetAllChainEntries(r.Context(), id)
if err != nil {
slog.Error("getting provider chain", "error", err)
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to get provider chain")
return
}
// Group by content level — capability_id is the provider slug.
levels := make(map[string][]chainLevelEntry)
for _, e := range entries {
levels[e.ContentLevel] = append(levels[e.ContentLevel], chainLevelEntry{
PluginInstallationID: e.PluginInstallationID,
CapabilityID: e.CapabilityID,
ProviderSlug: e.CapabilityID,
Priority: e.Priority,
Enabled: e.Enabled,
})
}
writeJSON(w, http.StatusOK, map[string]any{"levels": levels})
}
// HandleSetLibraryProviders handles PUT /libraries/{id}/providers.
// It replaces the entire provider chain for the given library.
func (h *LibraryHandler) HandleSetLibraryProviders(w http.ResponseWriter, r *http.Request) {
if h.ChainRepo == nil {
writeError(w, http.StatusServiceUnavailable, "unavailable", "Provider chain management is not configured")
return
}
id, err := parseIDParam(r)
if err != nil {
writeError(w, http.StatusBadRequest, "bad_request", "Invalid library ID")
return
}
// Verify the library exists.
if _, err := h.folderRepo.GetByID(r.Context(), id); err != nil {
if errors.Is(err, catalog.ErrFolderNotFound) {
writeError(w, http.StatusNotFound, "not_found", "Library not found")
return
}
slog.Error("fetching library for provider chain update", "error", err)
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to fetch library")
return
}
var req setChainLevelRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
writeError(w, http.StatusBadRequest, "bad_request", "Invalid request body")
return
}
var entries []metadata.ChainEntry
for level, inputs := range req.Levels {
for _, input := range inputs {
entries = append(entries, metadata.ChainEntry{
PluginInstallationID: input.PluginInstallationID,
CapabilityID: input.CapabilityID,
CapabilityType: "metadata_provider.v1",
ContentLevel: level,
Priority: input.Priority,
Enabled: input.Enabled,
})
}
}
if err := h.ChainRepo.SetChain(r.Context(), id, entries); err != nil {
slog.Error("setting provider chain", "error", err)
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to set provider chain")
return
}
if h.chainCacheInvalidator != nil {
h.chainCacheInvalidator.InvalidateChainCache()
}
w.WriteHeader(http.StatusNoContent)
}
// seedDefaultChain builds a default provider chain from plugin manifest defaults
// for the given library type. Returns entries for all applicable content levels.
func (h *LibraryHandler) seedDefaultChain(ctx context.Context, libraryType string) []metadata.ChainEntry {
if h.ChainRepo == nil {
return nil
}
caps, err := metadata.ListEnabledMetadataCapabilities(ctx, h.ChainRepo.Pool())
if err != nil {
slog.Warn("seed chain: failed to list metadata capabilities", "error", err)
return nil
}
levels := metadataContentLevelsForLibraryType(libraryType)
if len(levels) == 0 {
return nil
}
var entries []metadata.ChainEntry
for _, level := range levels {
type candidate struct {
installationID int
capabilityID string
priority int
enabled bool
}
var candidates []candidate
for _, c := range caps {
defaultPriority := metadata.LookupDefaultPriority(ctx, h.ChainRepo.Pool(), c.PluginInstallationID, c.CapabilityID, level)
if defaultPriority > 0 {
candidates = append(candidates, candidate{
installationID: c.PluginInstallationID,
capabilityID: c.CapabilityID,
priority: defaultPriority,
enabled: true,
})
} else {
// Plugin doesn't declare this level — include but disabled.
candidates = append(candidates, candidate{
installationID: c.PluginInstallationID,
capabilityID: c.CapabilityID,
priority: 999,
enabled: false,
})
}
}
sort.Slice(candidates, func(i, j int) bool {
return candidates[i].priority < candidates[j].priority
})
for i, cand := range candidates {
entries = append(entries, metadata.ChainEntry{
PluginInstallationID: cand.installationID,
CapabilityID: cand.capabilityID,
CapabilityType: "metadata_provider.v1",
ContentLevel: level,
Priority: i,
Enabled: cand.enabled,
})
}
}
return entries
}
func metadataContentLevelsForLibraryType(libraryType string) []string {
switch libraryType {
case "series":
return []string{"series", "season", "episode"}
case "movies", "movie":
return []string{"movie"}
case "audiobooks", "audiobook":
return []string{"audiobook"}
case "ebooks", "ebook":
return []string{"ebook"}
case "manga":
return []string{"manga"}
case "mixed":
return []string{"movie", "series", "season", "episode", "audiobook", "ebook"}
default:
return nil
}
}
// HandleListStaleIDs handles GET /libraries/stale-ids.
func (h *LibraryHandler) HandleListStaleIDs(w http.ResponseWriter, r *http.Request) {
if h.StaleIDRepo == nil {
writeJSON(w, http.StatusOK, []staleMediaIDResponse{})
return
}
staleIDs, err := h.StaleIDRepo.ListAll(r.Context())
if err != nil {
slog.Error("listing stale media IDs", "error", err)
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to list stale IDs")
return
}
if len(staleIDs) == 0 {
writeJSON(w, http.StatusOK, []staleMediaIDResponse{})
return
}
// Collect unique content IDs for batch lookup.
contentIDs := make([]string, 0, len(staleIDs))
seen := make(map[string]bool, len(staleIDs))
for _, s := range staleIDs {
if !seen[s.ContentID] {
contentIDs = append(contentIDs, s.ContentID)
seen[s.ContentID] = true
}
}
// Batch-load item metadata and library associations.
type itemInfo struct {
Title string
Year int
ContentType string
LibraryID int
LibraryName string
}
items := make(map[string]itemInfo, len(contentIDs))
rows, err := h.pool.Query(r.Context(), `
SELECT mi.content_id, mi.title, mi.year, mi.type,
COALESCE(mf_lib.folder_id, 0),
COALESCE(mf_lib.folder_name, '')
FROM media_items mi
LEFT JOIN LATERAL (
SELECT mf2.media_folder_id AS folder_id, f.name AS folder_name
FROM media_files mf2
JOIN media_folders f ON f.id = mf2.media_folder_id
WHERE mf2.content_id = mi.content_id
LIMIT 1
) mf_lib ON true
WHERE mi.content_id = ANY($1)
`, contentIDs)
if err != nil {
slog.Error("loading items for stale IDs", "error", err)
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to load item data")
return
}
defer rows.Close()
for rows.Next() {
var cid, title, ctype, libName string
var year, libID int
if err := rows.Scan(&cid, &title, &year, &ctype, &libID, &libName); err != nil {
slog.Error("scanning item for stale IDs", "error", err)
continue
}
items[cid] = itemInfo{Title: title, Year: year, ContentType: ctype, LibraryID: libID, LibraryName: libName}
}
resp := make([]staleMediaIDResponse, 0, len(staleIDs))
for _, s := range staleIDs {
info := items[s.ContentID]
resp = append(resp, staleMediaIDResponse{
ContentID: s.ContentID,
LibraryID: info.LibraryID,
LibraryName: info.LibraryName,
Title: info.Title,
Year: info.Year,
ContentType: info.ContentType,
Provider: s.Provider,
ProviderID: s.ProviderID,
FirstSeenAt: s.FirstSeenAt.Format("2006-01-02T15:04:05Z"),
LastSeenAt: s.LastSeenAt.Format("2006-01-02T15:04:05Z"),
})
}
writeJSON(w, http.StatusOK, resp)
}
// HandleRematchStaleID handles POST /libraries/stale-ids/{contentID}/rematch.
// Deprecated: prefer the explicit admin match search/apply flow via
// POST /admin/items/{id}/match/search and POST /admin/items/{id}/match/apply.
func (h *LibraryHandler) HandleRematchStaleID(w http.ResponseWriter, r *http.Request) {
contentID := chi.URLParam(r, "contentID")
if contentID == "" {
writeError(w, http.StatusBadRequest, "bad_request", "Missing content ID")
return
}
// Clear the stale external IDs from the media_items row.
// We clear all provider IDs so the re-match starts fresh from title/year.
_, err := h.pool.Exec(r.Context(), `
UPDATE media_items
SET tmdb_id = '', tvdb_id = '', imdb_id = ''
WHERE content_id = $1
`, contentID)
if err != nil {
slog.Error("clearing stale IDs from media item", "content_id", contentID, "error", err)
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to clear IDs")
return
}
// Remove stale_media_ids records.
if h.StaleIDRepo != nil {
if err := h.StaleIDRepo.DeleteByContentID(r.Context(), contentID); err != nil {
slog.Error("deleting stale media ID records", "content_id", contentID, "error", err)
}
}
// Re-trigger metadata match.
if h.refresher != nil {
go func() {
if err := h.refresher.RefreshItem(h.appCtx, contentID); err != nil {
slog.Warn("metadata: rematch refresh failed", "content_id", contentID, "error", err)
}
}()
}
w.WriteHeader(http.StatusNoContent)
}
func (h *LibraryHandler) HandleListRoots(w http.ResponseWriter, r *http.Request) {
if h.ScannedGroupRepo == nil || h.folderRepo == nil {
writeError(w, http.StatusInternalServerError, "internal_error", "Group snapshots not configured")
return
}
q := r.URL.Query()
libraryID, err := strconv.Atoi(q.Get("library_id"))
if err != nil || libraryID <= 0 {
writeError(w, http.StatusBadRequest, "bad_request", "library_id is required")
return
}
limit := 100
if value := strings.TrimSpace(q.Get("limit")); value != "" {
if parsed, parseErr := strconv.Atoi(value); parseErr == nil && parsed > 0 && parsed <= 500 {
limit = parsed
}
}
offset := 0
if value := strings.TrimSpace(q.Get("offset")); value != "" {
if parsed, parseErr := strconv.Atoi(value); parseErr == nil && parsed >= 0 {
offset = parsed
}
}
state := strings.TrimSpace(q.Get("state"))
folder, err := h.folderRepo.GetByID(r.Context(), libraryID)
if err != nil {
writeError(w, http.StatusNotFound, "not_found", "Library not found")
return
}
groups, total, err := h.ScannedGroupRepo.ListByFolder(r.Context(), libraryID, state, limit, offset)
if err != nil {
slog.Error("listing scanned groups", "library_id", libraryID, "error", err)
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to list roots")
return
}
overrideByGroup := map[string]models.MediaGroupOverride{}
if h.GroupOverrideRepo != nil {
overrides, err := h.GroupOverrideRepo.ListByFolder(r.Context(), libraryID)
if err != nil {
slog.Warn("listing group overrides", "library_id", libraryID, "error", err)
} else {
for _, override := range overrides {
overrideByGroup[groupOverrideLookupKey(override.GroupKeyVersion, override.ContentGroupKey)] = override
}
}
}
items := make([]libraryRootResponse, 0, len(groups))
for _, group := range groups {
rootPath := strings.TrimSpace(group.SampleObservedRootPath)
if rootPath == "" {
rootPath = filepath.Dir(group.SampleFilePath)
}
resp := libraryRootResponse{
LibraryID: libraryID,
LibraryName: folder.Name,
RootPath: rootPath,
State: group.State,
InferredType: group.InferredType,
TypeConfidence: group.TypeConfidence,
Title: group.BaseTitle,
Year: group.BaseYear,
TmdbID: group.TmdbID,
ImdbID: group.ImdbID,
TvdbID: group.TvdbID,
ObservedFiles: group.ObservedFileCount,
SampleFilePath: group.SampleFilePath,
Evidence: append(json.RawMessage(nil), group.EvidenceJSON...),
OverrideSource: group.OverrideSource,
FirstSeenAt: group.FirstSeenAt,
LastSeenAt: group.LastSeenAt,
}
if override, ok := overrideByGroup[groupOverrideLookupKey(group.GroupKeyVersion, group.ContentGroupKey)]; ok {
resp.ActiveOverride = &rootOverride{
ForcedType: override.ForcedType,
ForcedTitle: override.ForcedTitle,
ForcedYear: override.ForcedYear,
ForcedTmdbID: override.ForcedTmdbID,
ForcedImdbID: override.ForcedImdbID,
ForcedTvdbID: override.ForcedTvdbID,
Note: override.Note,
}
}
items = append(items, resp)
}
writeJSON(w, http.StatusOK, libraryRootsListResponse{Items: items, Total: total})
}
func (h *LibraryHandler) HandleUpsertRootOverride(w http.ResponseWriter, r *http.Request) {
if h.GroupOverrideRepo == nil || h.ObservedLocationRepo == nil {
writeError(w, http.StatusInternalServerError, "internal_error", "Root overrides not configured")
return
}
var req rootOverrideUpsertRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
writeError(w, http.StatusBadRequest, "bad_request", "Invalid request body")
return
}
req.RootPath = filepath.Clean(strings.TrimSpace(req.RootPath))
if req.LibraryID <= 0 || req.RootPath == "" {
writeError(w, http.StatusBadRequest, "bad_request", "library_id and root_path are required")
return
}
location, err := h.ObservedLocationRepo.Get(r.Context(), req.LibraryID, req.RootPath)
if err != nil {
slog.Error("loading observed media location", "library_id", req.LibraryID, "root_path", req.RootPath, "error", err)
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to load root")
return
}
if location == nil || location.PrimaryContentGroupKey == "" {
if location != nil && location.ContentGroupCount > 1 {
writeError(w, http.StatusConflict, "ambiguous_root", "Root contains multiple logical groups; override the group after splitting or selecting a specific item")
return
}
writeError(w, http.StatusNotFound, "not_found", "Root not found")
return
}
userID := apimw.GetUserID(r.Context())
override := models.MediaGroupOverride{
MediaFolderID: req.LibraryID,
GroupKeyVersion: location.PrimaryGroupKeyVersion,
ContentGroupKey: location.PrimaryContentGroupKey,
ForcedType: strings.TrimSpace(req.ForcedType),
ForcedTitle: strings.TrimSpace(req.ForcedTitle),
ForcedYear: req.ForcedYear,
ForcedTmdbID: strings.TrimSpace(req.ForcedTmdbID),
ForcedImdbID: strings.TrimSpace(req.ForcedImdbID),
ForcedTvdbID: strings.TrimSpace(req.ForcedTvdbID),
Note: strings.TrimSpace(req.Note),
CreatedByUserID: nil,
UpdatedByUserID: nil,
}
if userID > 0 {
override.CreatedByUserID = &userID
override.UpdatedByUserID = &userID
}
if err := h.GroupOverrideRepo.Upsert(r.Context(), override); err != nil {
slog.Error("upserting group override", "library_id", req.LibraryID, "root_path", req.RootPath, "error", err)
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to save override")
return
}
w.WriteHeader(http.StatusNoContent)
}
func (h *LibraryHandler) HandleDeleteRootOverride(w http.ResponseWriter, r *http.Request) {
if h.GroupOverrideRepo == nil || h.ObservedLocationRepo == nil {
writeError(w, http.StatusInternalServerError, "internal_error", "Root overrides not configured")
return
}
var req rootOverrideDeleteRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
writeError(w, http.StatusBadRequest, "bad_request", "Invalid request body")
return
}
req.RootPath = filepath.Clean(strings.TrimSpace(req.RootPath))
if req.LibraryID <= 0 || req.RootPath == "" {
writeError(w, http.StatusBadRequest, "bad_request", "library_id and root_path are required")
return
}
location, err := h.ObservedLocationRepo.Get(r.Context(), req.LibraryID, req.RootPath)
if err != nil {
slog.Error("loading observed media location", "library_id", req.LibraryID, "root_path", req.RootPath, "error", err)
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to load root")
return
}
if location == nil || location.PrimaryContentGroupKey == "" {
if location != nil && location.ContentGroupCount > 1 {
writeError(w, http.StatusConflict, "ambiguous_root", "Root contains multiple logical groups; delete the override from a specific group instead")
return
}
writeError(w, http.StatusNotFound, "not_found", "Root not found")
return
}
if err := h.GroupOverrideRepo.Delete(r.Context(), req.LibraryID, location.PrimaryGroupKeyVersion, location.PrimaryContentGroupKey); err != nil {
slog.Error("deleting group override", "library_id", req.LibraryID, "root_path", req.RootPath, "error", err)
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to delete override")
return
}
w.WriteHeader(http.StatusNoContent)
}
// unmatchedItemResponse represents an item in the unmatched-items list.
type unmatchedItemResponse struct {
ContentID string `json:"content_id"`
Title string `json:"title"`
Year int `json:"year"`
ContentType string `json:"content_type"`
LibraryID int `json:"library_id"`
LibraryName string `json:"library_name"`
Status string `json:"status"`
}
type unmatchedItemsListResponse struct {
Items []unmatchedItemResponse `json:"items"`
Total int `json:"total"`
}
// HandleListUnmatchedItems handles GET /libraries/unmatched-items.
// Returns items that are in unmatched, pending, or ambiguous status, enriched with
// library context so the admin maintenance page can link to them.
func (h *LibraryHandler) HandleListUnmatchedItems(w http.ResponseWriter, r *http.Request) {
if h.pool == nil {
writeError(w, http.StatusInternalServerError, "internal_error", "Database not configured")
return
}
q := r.URL.Query()
limit := 100
if v := q.Get("limit"); v != "" {
if n, err := strconv.Atoi(v); err == nil && n > 0 && n <= 500 {
limit = n
}
}
offset := 0
if v := q.Get("offset"); v != "" {
if n, err := strconv.Atoi(v); err == nil && n >= 0 {
offset = n
}
}
// Optional case-insensitive search across title, library name, type, and
// status. Applied server-side so it spans the whole table, not just the
// current page. The displayed folder still comes from the lateral join below,
// but the search predicate checks every membership so multi-library items are
// found when any linked library name matches.
search := strings.TrimSpace(q.Get("q"))
filter := ""
filterArgs := []any{}
if search != "" {
filterArgs = append(filterArgs, "%"+search+"%")
filter = ` AND (
mi.title ILIKE $1
OR mi.type ILIKE $1
OR mi.status ILIKE $1
OR EXISTS (
SELECT 1
FROM media_item_libraries search_mil
JOIN media_folders search_f ON search_f.id = search_mil.media_folder_id
WHERE search_mil.content_id = mi.content_id
AND search_f.name ILIKE $1
)
)`
}
countSQL := `
SELECT COUNT(*)
FROM media_items mi
WHERE mi.status IN ('unmatched', 'pending', 'ambiguous')`
countSQL += filter
var total int
if err := h.pool.QueryRow(r.Context(), countSQL, filterArgs...).Scan(&total); err != nil {
slog.Error("counting unmatched items", "error", err)
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to count unmatched items")
return
}
listArgs := append(append([]any{}, filterArgs...), limit, offset)
listSQL := fmt.Sprintf(`
SELECT mi.content_id, mi.title, mi.year, mi.type, mi.status,
COALESCE(lib.folder_id, 0),
COALESCE(lib.folder_name, '')
FROM media_items mi
LEFT JOIN LATERAL (
SELECT mil.media_folder_id AS folder_id, f.name AS folder_name
FROM media_item_libraries mil
JOIN media_folders f ON f.id = mil.media_folder_id
WHERE mil.content_id = mi.content_id
LIMIT 1
) lib ON true
WHERE mi.status IN ('unmatched', 'pending', 'ambiguous')%s
ORDER BY mi.title ASC, mi.content_id ASC
LIMIT $%d OFFSET $%d
`, filter, len(filterArgs)+1, len(filterArgs)+2)
rows, err := h.pool.Query(r.Context(), listSQL, listArgs...)
if err != nil {
slog.Error("listing unmatched items", "error", err)
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to list unmatched items")
return
}
defer rows.Close()
items := make([]unmatchedItemResponse, 0)
for rows.Next() {
var item unmatchedItemResponse
if err := rows.Scan(&item.ContentID, &item.Title, &item.Year, &item.ContentType, &item.Status, &item.LibraryID, &item.LibraryName); err != nil {
slog.Error("scanning unmatched item", "error", err)
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to scan item")
return
}
items = append(items, item)
}
if err := rows.Err(); err != nil {
slog.Error("iterating unmatched items", "error", err)
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to iterate items")
return
}
writeJSON(w, http.StatusOK, unmatchedItemsListResponse{
Items: items,
Total: total,
})
}
// parseIDParam extracts and parses the "id" URL parameter as an integer.
func parseIDParam(r *http.Request) (int, error) {
idStr := chi.URLParam(r, "id")
return strconv.Atoi(idStr)
}