melodee icon indicating copy to clipboard operation
melodee copied to clipboard

Playlist Import

Open sphildreth opened this issue 10 months ago • 0 comments

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:

  1. User uploads a playlist file.
  2. Melodee stores the original file.
  3. Melodee creates a standard database playlist.
  4. Melodee parses each entry and tries to find the Song in the library.
  5. 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 .m3u or .m3u8 file.
  • Server stores the original file bytes for traceability and later re-processing.
  • Server creates a DB Playlist record:
    • Name: derived from the filename (editable afterward)
    • UserId: the uploading user
    • SourceType: M3U_IMPORT

2) Parse Entries

  • Parse the file according to M3U conventions:

    • Ignore empty lines
    • Ignore comments (#...) including #EXTM3U and #EXTINF
    • Treat non-comment, non-empty lines as playlist item references
  • Handle encoding:

    • .m3u8 is UTF-8
    • .m3u may 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 %xx sequences (and handle literal % safely)
  • Remove surrounding quotes if present

Primary matching approach (in order):

  1. 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.
  2. Filename match with directory hints
    • Use Artist/Album/Song folder segments when available.
  3. 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

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 = Missing flag
  • A lastAttemptUtc timestamp

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/.m3u8 directories.
  • 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 .m3u or .m3u8 file.
  • 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

  • PlaylistUploadedFileId
  • UserId
  • OriginalFileName
  • ContentType
  • Length

PlaylistUploadedFileItem

  • PlaylistUploadedFileItemId
  • PlaylistUploadedFileId
  • SongId (nullable)
  • SortOrder
  • Status (Resolved, Missing)
  • RawReference (original line)
  • NormalizedReference
  • HintsJson (filename, artist, album, etc.)
  • LastAttemptUtc

Acceptance Tests

  1. Import Upload via UI + API

    • Upload a .m3u playlist 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.
  2. 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
  3. Missing Entries Do Not Crash

    • Import playlist that includes:
      • Blank lines
      • #EXTM3U / #EXTINF lines
      • A malformed path
      • A reference with URL-encoded characters (%20, %5B, %5D)
    • Result:
      • Import succeeds
      • Non-Song lines are ignored
      • Malformed lines become Missing entries
  4. 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 Missing to Resolved
      • No duplicates are introduced
  5. 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
  6. 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 PlaylistService that:

    • Accepts the uploaded file
    • Parses it into references
    • Attempts matching using SongService List() 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.

sphildreth avatar Jan 18 '26 04:01 sphildreth