feat: combine upload and Openverse into one import page
Adding music took two navigation items that each did half the job. The new Import page has both sources as tabs and one shared list of what was added in this session, with the reason for every failure and a link to the library. From computer: drop several MP3 or WAV files or pick them with a multi-select dialog. Files are imported one by one on a background thread with a progress bar, and a failing file does not stop the rest. Openverse: search and download show a progress ring with a status text. Results that are already in the library or have no duration are marked in their row and cannot be imported; a double-click imports. To detect duplicates, songs now store the Openverse result id. The repository adds the column to existing databases and a unique index keeps one song per Openverse result. Refs #13 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
a762a2f571
commit
0a0d8e1d4c
17 files changed
+582
-266
No files matched your search
@@ -39,6 +39,7 @@ public sealed class OpenverseTests : IDisposable
|
||||
|
||||
private static OpenverseTrack Track(string url = "https://cdn.example.org/a.mp3") => new()
|
||||
{
|
||||
Id = "a",
|
||||
Title = "Piano Melody",
|
||||
Creator = "benpm",
|
||||
Url = url,
|
||||
@@ -66,6 +67,7 @@ public sealed class OpenverseTests : IDisposable
|
||||
Assert.Equal("https://api.openverse.org/v1/audio/?q=jazz%20piano&page_size=20", requested!.AbsoluteUri);
|
||||
Assert.Equal(2, tracks.Count);
|
||||
var first = tracks[0];
|
||||
Assert.Equal("a", first.Id);
|
||||
Assert.Equal("Piano Melody", first.Title);
|
||||
Assert.Equal("benpm", first.Creator);
|
||||
Assert.Equal("https://cdn.example.org/a.mp3", first.Url);
|
||||
@@ -119,6 +121,8 @@ public sealed class OpenverseTests : IDisposable
|
||||
Assert.Equal(Track().Attribution, stored.Attribution);
|
||||
Assert.Equal(TimeSpan.FromMilliseconds(10193), stored.Duration);
|
||||
Assert.Equal("benpm", stored.Artist);
|
||||
Assert.Equal("a", stored.OpenverseId);
|
||||
Assert.Contains("a", controller.ImportedIds());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -212,4 +216,20 @@ public sealed class OpenverseTests : IDisposable
|
||||
|
||||
public override int Read(byte[] buffer, int offset, int count) => throw new IOException("connection reset");
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Import_TrackAlreadyInLibrary_IsRejectedWithoutDownload()
|
||||
{
|
||||
var downloads = 0;
|
||||
var client = NewClient((_, _) => { downloads++; return Respond(HttpStatusCode.OK, new ByteArrayContent(Audio)); });
|
||||
var songs = new SongRepository(Path.Combine(_directory, "test.db"));
|
||||
var controller = new OpenverseController(client, songs);
|
||||
await controller.ImportAsync(Track());
|
||||
|
||||
var error = await Assert.ThrowsAsync<OpenverseException>(() => controller.ImportAsync(Track()));
|
||||
|
||||
Assert.Contains("already in the library", error.Message);
|
||||
Assert.Equal(1, downloads);
|
||||
Assert.Single(songs.GetAll());
|
||||
Assert.Single(MusicFiles());
|
||||
}}
|
||||
@@ -108,4 +108,24 @@ public sealed class SongImportServiceTests : IDisposable
|
||||
Assert.Throws<SongImportException>(() => _service.Import(Path.Combine(SourceDirectory, "missing.mp3")));
|
||||
Assert.Empty(MusicFiles());
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void UploadController_ImportsEachFileAndReportsFailuresPerFile()
|
||||
{
|
||||
var controller = new Radiosender.Controllers.UploadController(_service);
|
||||
string[] files =
|
||||
[
|
||||
CreateWav("first.wav", TimeSpan.FromSeconds(2)),
|
||||
Path.Combine(SourceDirectory, "notes.txt"),
|
||||
CreateWav("second.wav", TimeSpan.FromSeconds(3)),
|
||||
];
|
||||
File.WriteAllText(files[1], "not audio");
|
||||
|
||||
var results = files.Select(controller.Upload).ToList();
|
||||
|
||||
Assert.Equal([true, false, true], results.Select(r => r.Success));
|
||||
Assert.Equal("first", results[0].Song!.Title);
|
||||
Assert.Null(results[1].Song);
|
||||
Assert.Contains("MP3 and WAV", results[1].Message);
|
||||
Assert.Equal(2, _repository.GetAll().Count);
|
||||
}}
|
||||
@@ -124,4 +124,54 @@ public sealed class SongRepositoryTests : IDisposable
|
||||
|
||||
Assert.Throws<SqliteException>(() => repository.Add(song));
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void OpenverseId_IsStoredAndListed()
|
||||
{
|
||||
var repository = new SongRepository(DatabasePath);
|
||||
var song = NewSong();
|
||||
song.OpenverseId = "ov-1";
|
||||
repository.Add(song);
|
||||
repository.Add(NewSong("Uploaded"));
|
||||
|
||||
Assert.Equal("ov-1", repository.GetById(song.Id)!.OpenverseId);
|
||||
Assert.Equal(["ov-1"], repository.GetOpenverseIds());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void OpenverseId_CannotBeStoredTwice()
|
||||
{
|
||||
var repository = new SongRepository(DatabasePath);
|
||||
repository.Add(new Song { Title = "A", Artist = "", Duration = TimeSpan.FromSeconds(1), FilePath = "a", OpenverseId = "ov-1" });
|
||||
|
||||
Assert.Throws<SqliteException>(() =>
|
||||
repository.Add(new Song { Title = "B", Artist = "", Duration = TimeSpan.FromSeconds(1), FilePath = "b", OpenverseId = "ov-1" }));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_AddsOpenverseIdColumnToOlderDatabase()
|
||||
{
|
||||
Directory.CreateDirectory(_directory);
|
||||
using (var connection = new SqliteConnection($"Data Source={DatabasePath}"))
|
||||
{
|
||||
connection.Open();
|
||||
using var command = connection.CreateCommand();
|
||||
command.CommandText = """
|
||||
CREATE TABLE Songs (
|
||||
Id INTEGER PRIMARY KEY AUTOINCREMENT, Title TEXT NOT NULL, Artist TEXT NOT NULL,
|
||||
DurationMs INTEGER NOT NULL CHECK (DurationMs > 0), FilePath TEXT NOT NULL,
|
||||
Source INTEGER NOT NULL, License TEXT, Attribution TEXT);
|
||||
INSERT INTO Songs (Title, Artist, DurationMs, FilePath, Source) VALUES ('Old', 'Artist', 1000, 'old.mp3', 0);
|
||||
""";
|
||||
command.ExecuteNonQuery();
|
||||
}
|
||||
|
||||
var repository = new SongRepository(DatabasePath);
|
||||
var song = NewSong();
|
||||
song.OpenverseId = "ov-1";
|
||||
repository.Add(song);
|
||||
|
||||
Assert.Null(Assert.Single(repository.GetAll(), s => s.Title == "Old").OpenverseId);
|
||||
Assert.Equal("ov-1", repository.GetById(song.Id)!.OpenverseId);
|
||||
_ = new SongRepository(DatabasePath); // second start must not try to add the column again
|
||||
}}
|
||||
Reference in new issue
Block a user