Files
silo-server/internal/scanner/podcast.go
baa33768d6 fix(scanner): stop reporting an unusable ffprobe as an empty folder (#468)
* fix(scanner): stop reporting an unusable ffprobe as an empty folder

parseAudiobookFolder and parsePodcastShow signalled "this folder holds no
audio files" by wrapping os.ErrNotExist, and their reconcile callers skipped
on that. exec also wraps fs.ErrNotExist when the configured ffprobe binary
cannot be run, so a wrong playback.ffmpeg_path made every candidate folder
look empty: the scan logged processed=N failed=0, indexed nothing, and gave
the operator no clue why the library stayed empty.

Introduce an errFolderHasNoMedia sentinel that deliberately does not wrap
os.ErrNotExist, and skip on that instead. A folder that disappears between
the scan walk and the parse still maps to the sentinel, so a mid-scan rename
or delete stays a quiet skip rather than a scan failure.

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

* fix(scanner): bound the all-failed scan summary instead of joining every failure

The sentinel change in this PR makes a previously-unreachable path reachable.
Before it, a misconfigured ffprobe made every candidate folder look empty, so
the scan skipped everything and `failed` stayed 0 — the `failedCount ==
processedCount` branch never fired. Now that an unusable ffprobe propagates as
a real failure, that branch is the expected outcome of a first scan with a bad
`playback.ffmpeg_path`, and it joins one wrapped error per failed folder.

On the 240k-folder library the scan code is written for, `errors.Join` over
that slice produces a ~64 MB error string (measured) that is written verbatim
into `scan_runs.error_message` and republished over the Redis events channel
and the admin SSE stream. The `failures` slice itself also grew unbounded for
the whole scan even when the all-failed guard could not fire (any rescan with
`skipped > 0`), holding hundreds of megabytes across a multi-hour scan before
discarding it.

Add a `scanFailures` collector that retains the first 20 failures and counts
the rest, joining them with a trailing "and N more failures (elided)". The
retained sample still names the cause, which is the entire purpose of the
summary. The same 64 MB case now produces 5.4 KB.

The ebook and manga scans have the identical shape and the same exposure via
their own probe failures, so all four call sites share the collector rather
than fixing the two audio paths alone. `failMu` now guards only `cancelErr`
and is renamed `cancelMu` to match.

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

---------

Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
Co-authored-by: Quick <31828688+Quick104@users.noreply.github.com>
2026-07-26 23:35:36 -04:00

113 lines
3.1 KiB
Go

package scanner
import (
"context"
"errors"
"fmt"
"os"
"path/filepath"
"sort"
)
// parsedPodcastShow is what parsePodcastShow returns. The scanner write
// path converts this into media_items (type='podcast') + episodes +
// media_files rows.
type parsedPodcastShow struct {
Title string
Author string
Year int
Episodes []parsedPodcastEpisode
}
type parsedPodcastEpisode struct {
Path string
Title string
Track int
}
// parsePodcastShow walks a single subdirectory of a podcast library and
// returns the show's metadata + episode list. Each audio file inside
// becomes one episode; tags are read from each file individually so
// per-episode titles surface correctly.
//
// Returns an errFolderHasNoMedia-wrapped error if the folder contains zero
// audio files. Every other error (including an ffprobe binary that cannot be
// executed) is a real failure and must be reported by the caller.
func parsePodcastShow(ctx context.Context, ffprobePath string, folderPath string) (*parsedPodcastShow, error) {
audioFiles, err := listPodcastShowAudioFiles(folderPath)
if err != nil {
return nil, err
}
show := &parsedPodcastShow{}
for idx, path := range audioFiles {
probed, err := ProbeFile(ctx, ffprobePath, path)
if err != nil {
return nil, fmt.Errorf("probe podcast file %s: %w", path, err)
}
tags := probed.FormatTags
if show.Title == "" {
show.Title = firstNonEmpty(tags["album"], tags["show"], filepath.Base(folderPath))
show.Author = firstNonEmpty(tags["artist"], tags["album_artist"])
if year := firstNonEmpty(tags["date"], tags["year"]); year != "" {
if y := parseTagYear(year); y > 0 {
show.Year = y
}
}
}
stem := filepath.Base(path)
stem = stem[:len(stem)-len(filepath.Ext(stem))]
track := idx + 1
if t := tags["track"]; t != "" {
if parsed := parseTrackNumber(t); parsed > 0 {
track = parsed
}
}
show.Episodes = append(show.Episodes, parsedPodcastEpisode{
Path: path,
Title: firstNonEmpty(tags["title"], stem),
Track: track,
})
}
return show, nil
}
func listPodcastShowAudioFiles(folderPath string) ([]string, error) {
entries, err := os.ReadDir(folderPath)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
// Renamed or deleted between the scan walk and this read; treat
// it as an empty show so the caller skips instead of failing.
return nil, fmt.Errorf("podcast show %s: %w", folderPath, errFolderHasNoMedia)
}
return nil, fmt.Errorf("read podcast folder %s: %w", folderPath, err)
}
var audioFiles []string
for _, entry := range entries {
if entry.IsDir() {
continue
}
if SupportsAudioFile(entry.Name()) {
audioFiles = append(audioFiles, filepath.Join(folderPath, entry.Name()))
}
}
if len(audioFiles) == 0 {
return nil, fmt.Errorf("podcast show %s: %w", folderPath, errFolderHasNoMedia)
}
sort.Strings(audioFiles)
return audioFiles, nil
}
// parseTrackNumber accepts ID3-style track values which may be "5" or
// "5/12". Returns 0 if no leading integer is present.
func parseTrackNumber(s string) int {
n := 0
for _, c := range s {
if c < '0' || c > '9' {
break
}
n = n*10 + int(c-'0')
}
return n
}