Skip to content

Cleanup: centralize library path and category helpers

jedmund requested to merge jedmund/cleanup-library-paths into main

First of six cleanup MRs from the codebase review (Phase 2). Eliminates the duplication that produced the path-traversal bug fixed in 676acf30.

What changes

  • New module `src/lib/server/services/libraryPaths.ts` exports `getUnsortedPath`, `getSortedPath`, `CATEGORIES`, `CATEGORY_FOLDERS`, `parseCategory`, `categoryToFolder`, `folderToCategory`, `assertInsideSortedRoot`.
  • `scanner.ts`, `watcher.ts`, `beetsSync.ts`, `sorter.ts`, `parser.ts`, `api/sort/preview`, `api/cleanup`, `api/preview` now import from `libraryPaths` instead of redefining `getSortedPath`/`getUnsortedPath` locally.
  • `Soundtrack` → `Soundtracks` folder mapping moves out of inline string literals in `parser.ts` and `scanner.ts`.
  • `sorter.ts`'s inline `resolveAndValidateTargetPath` is replaced by the shared `assertInsideSortedRoot` (identical implementation, lifted to the new module).
  • `isValidCategory` / `VALID_CATEGORIES` removed from `src/lib/types/index.ts` — `parseCategory` replaces them.

Behavior

No intentional behavior changes. `pnpm run build` passes.

Why

The reviewer flagged that path and category logic was duplicated across ~6 server modules and that this duplication had already produced one security bug. Consolidating these into a single module makes the next MR (route validators) able to share `parseCategory` and makes the planned scanner.ts split (MR 3) possible without re-duplicating these helpers.

Part of the cleanup plan documented at `.context/attachments/pasted_text_2026-05-18_03-48-04.txt`.

Merge request reports

Loading