feat: add song upload for MP3 and WAV files
Add an Upload page where the user picks an MP3 or WAV file. The new SongImportService copies it into %LOCALAPPDATA%\Radiosender\Music under a unique name, reads the duration with NAudio and title/artist from tags with TagLibSharp (falling back to the file name), and saves the song with Source = Upload. Wrong extensions, unreadable files, garbage data and files without audio are rejected; the copied file is deleted again so nothing is left behind. The page shows the result in a Snackbar. The music folder is injected so the service is testable without UI. Tests cover a generated WAV, duplicate names, wrong extension, garbage files, empty audio and missing files. Refs #4 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
89b7959404
commit
7c95aba30a
7 files changed
+297
No files matched your search
@@ -0,0 +1,111 @@
|
||||
using Microsoft.Data.Sqlite;
|
||||
using NAudio.Wave;
|
||||
using Radiosender.Models;
|
||||
using Radiosender.Services;
|
||||
|
||||
namespace Radiosender.Tests;
|
||||
|
||||
public sealed class SongImportServiceTests : IDisposable
|
||||
{
|
||||
private readonly string _directory = Path.Combine(Path.GetTempPath(), "radiosender-tests", Guid.NewGuid().ToString("N"));
|
||||
private readonly SongRepository _repository;
|
||||
private readonly SongImportService _service;
|
||||
|
||||
private string SourceDirectory => Path.Combine(_directory, "source");
|
||||
private string MusicDirectory => Path.Combine(_directory, "music");
|
||||
|
||||
public SongImportServiceTests()
|
||||
{
|
||||
Directory.CreateDirectory(SourceDirectory);
|
||||
_repository = new SongRepository(Path.Combine(_directory, "test.db"));
|
||||
_service = new SongImportService(_repository, MusicDirectory);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
SqliteConnection.ClearAllPools(); // pooled connections keep the file locked
|
||||
Directory.Delete(_directory, recursive: true);
|
||||
}
|
||||
|
||||
private string CreateWav(string fileName, TimeSpan duration)
|
||||
{
|
||||
var path = Path.Combine(SourceDirectory, fileName);
|
||||
var format = new WaveFormat(8000, 16, 1);
|
||||
using var writer = new WaveFileWriter(path, format);
|
||||
writer.Write(new byte[(int)(format.AverageBytesPerSecond * duration.TotalSeconds)]);
|
||||
return path;
|
||||
}
|
||||
|
||||
private string[] MusicFiles() =>
|
||||
Directory.Exists(MusicDirectory) ? Directory.GetFiles(MusicDirectory) : [];
|
||||
|
||||
[Fact]
|
||||
public void Import_Wav_CopiesFileAndSavesSong()
|
||||
{
|
||||
var source = CreateWav("My Song.wav", TimeSpan.FromSeconds(2));
|
||||
|
||||
var song = _service.Import(source);
|
||||
|
||||
var stored = _repository.GetById(song.Id);
|
||||
Assert.NotNull(stored);
|
||||
Assert.Equal("My Song", stored.Title);
|
||||
Assert.Equal(TimeSpan.FromSeconds(2), stored.Duration);
|
||||
Assert.Equal(SongSource.Upload, stored.Source);
|
||||
Assert.Equal(Path.Combine(MusicDirectory, "My Song.wav"), stored.FilePath);
|
||||
Assert.True(File.Exists(stored.FilePath));
|
||||
Assert.True(File.Exists(source));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Import_SameFileTwice_DoesNotOverwrite()
|
||||
{
|
||||
var source = CreateWav("song.wav", TimeSpan.FromSeconds(1));
|
||||
|
||||
var first = _service.Import(source);
|
||||
var second = _service.Import(source);
|
||||
|
||||
Assert.NotEqual(first.FilePath, second.FilePath);
|
||||
Assert.Equal(Path.Combine(MusicDirectory, "song (2).wav"), second.FilePath);
|
||||
Assert.Equal(2, MusicFiles().Length);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Import_WrongExtension_IsRejected()
|
||||
{
|
||||
var source = Path.Combine(SourceDirectory, "notes.txt");
|
||||
File.WriteAllText(source, "not audio");
|
||||
|
||||
Assert.Throws<SongImportException>(() => _service.Import(source));
|
||||
Assert.Empty(MusicFiles());
|
||||
Assert.Empty(_repository.GetAll());
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("garbage.wav")]
|
||||
[InlineData("garbage.mp3")]
|
||||
public void Import_GarbageFile_IsRejectedWithoutLeftoverCopy(string fileName)
|
||||
{
|
||||
var source = Path.Combine(SourceDirectory, fileName);
|
||||
File.WriteAllBytes(source, Enumerable.Range(0, 4096).Select(i => (byte)(i * 7)).ToArray());
|
||||
|
||||
Assert.Throws<SongImportException>(() => _service.Import(source));
|
||||
Assert.Empty(MusicFiles());
|
||||
Assert.Empty(_repository.GetAll());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Import_EmptyWav_IsRejectedWithoutLeftoverCopy()
|
||||
{
|
||||
var source = CreateWav("silence.wav", TimeSpan.Zero);
|
||||
|
||||
Assert.Throws<SongImportException>(() => _service.Import(source));
|
||||
Assert.Empty(MusicFiles());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Import_MissingFile_IsRejected()
|
||||
{
|
||||
Assert.Throws<SongImportException>(() => _service.Import(Path.Combine(SourceDirectory, "missing.mp3")));
|
||||
Assert.Empty(MusicFiles());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user