Playlist Import
Melodee Story: Importing M3U/M3U8 Playlists (DB Playlist + Deferred Matching)
Goal
Allow users to upload existing .m3u / .m3u8 playlist files into Melodee so they become normal, playable Melodee playlists, even when some referenced Songs are not currently in the library.
This feature is intentionally simple and user-first:
- User uploads a playlist file.
- Melodee stores the original file.
- Melodee creates a standard database playlist.
- Melodee parses each entry and tries to find the Song in the library.
- Matches are added immediately; missing items remain Songed and will be resolved later.
The resulting playlist behaves like any other Melodee playlist:
- It is playable in the UI.
- It is accessible via the API.
- It is available to Subsonic clients (playlists endpoint).
User Story
As a Melodee user, I want to upload my existing playlist file (M3U/M3U8), so Melodee creates a playlist I can play right away, and automatically completes it over time as missing Songs become available.
Key Behaviors
1) Upload & Create Playlist
- User uploads a
.m3uor.m3u8file. - Server stores the original file bytes for traceability and later re-processing.
- Server creates a DB
Playlistrecord:Name: derived from the filename (editable afterward)UserId: the uploading userSourceType:M3U_IMPORT
2) Parse Entries
-
Parse the file according to M3U conventions:
- Ignore empty lines
- Ignore comments (
#...) including#EXTM3Uand#EXTINF - Treat non-comment, non-empty lines as playlist item references
-
Handle encoding:
.m3u8is UTF-8.m3umay be UTF-8 or local/legacy encodings- If the file includes a UTF-8 BOM, handle it
3) Match Songs (Best Effort)
Each playlist line is treated as a Song reference candidate.
Supported reference forms (common in real-world exports):
- Relative path:
Artist/Album/Song.flac - Absolute path:
/music/Artist/Album/Song.flac - Windows absolute path:
D:\Music\Artist\Album\Song.mp3
Normalization rules:
- Trim whitespace
- Convert backslashes to forward slashes for matching
- URL decode
%xxsequences (and handle literal%safely) - Remove surrounding quotes if present
Primary matching approach (in order):
- Exact path match under library root (preferred)
- If the reference resolves to a path beneath the configured library root, compare against indexed Song file paths.
- Filename match with directory hints
- Use
Artist/Album/Songfolder segments when available.
- Use
- Fallback metadata match (optional if available)
- If the server can map to Song metadata based on filename (or cached scan data), attempt:
- Song title + artist + album
- If the server can map to Song metadata based on filename (or cached scan data), attempt:
Important behaviors:
- Never crash import because an entry is malformed or missing.
- If multiple matches exist (rare, but possible with duplicates):
- Prefer exact file path match
- Otherwise choose the “best scoring” candidate and record the match confidence.
4) Record Missing Items
If a referenced Song cannot be resolved:
- The playlist still imports successfully.
- The item is recorded as missing with enough information to try again later.
Missing entries should store:
- The original raw line
- The normalized candidate string
- Derived hints (filename, artist folder, album folder)
- A
status = Missingflag - A
lastAttemptUtctimestamp
5) Deferred Reconciliation (Background Job)
When new music is added to the library (or on a periodic schedule), Melodee runs a background job that:
- Finds playlists with unresolved items
- Re-attempts matching only for those missing items
- Adds newly matched Songs into the playlist
- Marks those items resolved
This job must be:
- Idempotent (running twice does not duplicate playlist items)
- Efficient (only re-check missing items)
- Safe (does not block UI, can be cancelled/retried)
Trigger options (one or both):
- After a library scan completes
- A periodic schedule (e.g., every N minutes)
6) Normal Playlist Behavior
Once created, the DB playlist is treated exactly like other playlists:
- Users can play it.
- Users can edit it.
- If the user edits the playlist manually, it remains valid.
Note: This is not a full “sync” feature. The uploaded file is a source for import, not a live authoritative external playlist.
Constraints / Non-Goals
- No requirement for automatic filesystem monitoring of
.m3u/.m3u8directories. - No requirement for one-way “continuous sync” from an external file.
- No requirement to preserve original ordering perfectly if duplicates or ambiguous matches exist (best effort).
- No requirement to support remote URLs/streams (treat as missing and keep the entry).
API / UI Requirements
UI
- Add an Import Playlist action in the Blazor UI.
- User chooses a
.m3uor.m3u8file. - UI shows import result summary:
- Total entries
- Matched entries
- Missing entries
Blazor UI must use DI-based application services (not raw HTTP calls to itself).
API
- Provide an API endpoint for uploading/importing a playlist.
- The endpoint returns:
- Playlist ID
- Matched count
- Missing count
- A preview list of missing entries (optional)
Documentation
- Enhance the playlists.md jekyll documentation with details around uploading and backgroud jobs to look for missing songs with future song additions
Unit Tests
- Ensure that ambple unit tests are created for finding songs including Happy Path and Edge cases
Data Model (both inherit from base class DataModelBase)
PlaylistUploadedFile
PlaylistUploadedFileIdUserIdOriginalFileNameContentTypeLength
PlaylistUploadedFileItem
PlaylistUploadedFileItemIdPlaylistUploadedFileIdSongId(nullable)SortOrderStatus(Resolved,Missing)RawReference(original line)NormalizedReferenceHintsJson(filename, artist, album, etc.)LastAttemptUtc
Acceptance Tests
-
Import Upload via UI + API
- Upload a
.m3uplaylist using the Blazor UI. - Blazor UI uses DI-based services (no internal http/api calls).
- API creates a DB playlist and returns an import summary.
- Upload a
-
Import Handles Mixed Matches
- Upload playlist containing 10 references:
- 6 exist in the library
- 4 do not exist
- Result:
- Playlist is created
- 6 Songs are playable in the playlist
- 4 entries are flagged
Missing
- Upload playlist containing 10 references:
-
Missing Entries Do Not Crash
- Import playlist that includes:
- Blank lines
#EXTM3U/#EXTINFlines- A malformed path
- A reference with URL-encoded characters (
%20,%5B,%5D)
- Result:
- Import succeeds
- Non-Song lines are ignored
- Malformed lines become
Missingentries
- Import playlist that includes:
-
Deferred Reconciliation Adds Songs Later
- Import playlist with missing entries.
- Add the previously missing Songs to the library.
- Run/trigger the reconciliation job.
- Result:
- Newly found Songs are added to the playlist
- Their items transition from
MissingtoResolved - No duplicates are introduced
-
Playlist Is Playable Everywhere
- The imported playlist behaves like any other playlist:
- Playable in Blazor UI
- Accessible through Melodee.API playlist endpoints
- Subsonic clients can list and play it
- The imported playlist behaves like any other playlist:
-
Idempotency
- Run the reconciliation job twice in a row.
- Result: playlist membership is unchanged after the second run.
Implementation Notes (Guidance for Agents)
-
Prefer a single service like
PlaylistServicethat:- Accepts the uploaded file
- Parses it into references
- Attempts matching using
SongServiceList() method with filters on Title and TitleNormalized - Creates playlist + playlist items in one transaction
-
Song matching should be deterministic and testable.
-
Persist enough information for later reconciliation.
-
Keep import fast: avoid full library scans; rely on indexed Song data.