Skip to content

Commit a65c738

Browse files
committed
refactor: clean up whitespace and improve code readability in M3U import and related tests
1 parent ee67d1d commit a65c738

6 files changed

Lines changed: 32 additions & 35 deletions

File tree

src/Melodee.Common/Jobs/PlaylistReconciliationJob.cs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using Melodee.Common.Configuration;
2-
using Melodee.Common.Constants;
32
using Melodee.Common.Data;
43
using Melodee.Common.Enums;
54
using Melodee.Common.Services;
@@ -30,7 +29,7 @@ public override async Task Execute(IJobExecutionContext context)
3029

3130
// Find all missing playlist items that haven't been attempted recently
3231
var retryThreshold = SystemClock.Instance.GetCurrentInstant().Minus(Duration.FromHours(1));
33-
32+
3433
var missingItems = await dbContext.PlaylistUploadedFileItems
3534
.Where(x => x.Status == PlaylistItemStatus.Missing)
3635
.Where(x => x.LastAttemptUtc == null || x.LastAttemptUtc < retryThreshold)
@@ -47,14 +46,14 @@ public override async Task Execute(IJobExecutionContext context)
4746
return;
4847
}
4948

50-
Logger.Information("[{JobName}] Attempting to reconcile {ItemCount} missing playlist items.",
49+
Logger.Information("[{JobName}] Attempting to reconcile {ItemCount} missing playlist items.",
5150
nameof(PlaylistReconciliationJob), missingItems.Count);
5251

5352
// Get library path for matching
5453
var librariesResult = await libraryService.GetStorageLibrariesAsync(context.CancellationToken).ConfigureAwait(false);
5554
var libraryPath = librariesResult.Data?.FirstOrDefault()?.Path;
5655

57-
var songMatcher = new SongMatchingService(logger, cacheManager, contextFactory);
56+
var songMatcher = new SongMatchingService(Logger, cacheManager, contextFactory);
5857

5958
var resolvedCount = 0;
6059
var now = SystemClock.Instance.GetCurrentInstant();
@@ -90,7 +89,7 @@ public override async Task Execute(IJobExecutionContext context)
9089
// Find the associated playlist and add the song
9190
var playlist = await dbContext.Playlists
9291
.Include(p => p.Songs)
93-
.FirstOrDefaultAsync(p => p.PlaylistUploadedFileId == item.PlaylistUploadedFileId,
92+
.FirstOrDefaultAsync(p => p.PlaylistUploadedFileId == item.PlaylistUploadedFileId,
9493
context.CancellationToken)
9594
.ConfigureAwait(false);
9695

@@ -104,7 +103,7 @@ public override async Task Execute(IJobExecutionContext context)
104103
{
105104
// Add song to playlist maintaining sort order
106105
var maxOrder = playlist.Songs.Any() ? playlist.Songs.Max(ps => ps.PlaylistOrder) : -1;
107-
106+
108107
var playlistSong = new Data.Models.PlaylistSong
109108
{
110109
PlaylistId = playlist.Id,
@@ -115,7 +114,7 @@ public override async Task Execute(IJobExecutionContext context)
115114

116115
playlist.Songs.Add(playlistSong);
117116
playlist.SongCount = (short)playlist.Songs.Count;
118-
117+
119118
// Efficiently calculate duration by loading song from matchResult instead of querying
120119
var currentDuration = playlist.Songs
121120
.Where(ps => ps.SongId != matchResult.Song.Id)
@@ -130,7 +129,7 @@ public override async Task Execute(IJobExecutionContext context)
130129
}
131130
catch (Exception ex)
132131
{
133-
Logger.Warning(ex, "[{JobName}] Error reconciling item: {Reference}",
132+
Logger.Warning(ex, "[{JobName}] Error reconciling item: {Reference}",
134133
nameof(PlaylistReconciliationJob), item.NormalizedReference);
135134
item.LastAttemptUtc = now;
136135
}

src/Melodee.Common/Migrations/20260117043252_AddPlaylistImportModels.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using System;
21
using Microsoft.EntityFrameworkCore.Migrations;
32
using NodaTime;
43
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;

src/Melodee.Common/Services/Parsing/M3UParser.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,19 @@ public async Task<M3UParseResult> ParseAsync(Stream fileStream, string fileName,
2323
{
2424
var isM3U8 = fileName.EndsWith(".m3u8", StringComparison.OrdinalIgnoreCase);
2525
var encoding = isM3U8 ? Encoding.UTF8 : DetectEncoding(fileStream);
26-
26+
2727
fileStream.Position = 0;
28-
28+
2929
var entries = new List<M3UEntry>();
3030
var lineNumber = 0;
3131

3232
using var reader = new StreamReader(fileStream, encoding, detectEncodingFromByteOrderMarks: true, leaveOpen: true);
33-
33+
3434
string? line;
3535
while ((line = await reader.ReadLineAsync(cancellationToken).ConfigureAwait(false)) != null && !cancellationToken.IsCancellationRequested)
3636
{
3737
lineNumber++;
38-
38+
3939
if (string.IsNullOrWhiteSpace(line))
4040
{
4141
continue;
@@ -166,7 +166,7 @@ private static (string? FileName, string? ArtistFolder, string? AlbumFolder) Ext
166166

167167
// Split path into segments
168168
var segments = normalizedPath.Split('/', StringSplitOptions.RemoveEmptyEntries);
169-
169+
170170
if (segments.Length == 0)
171171
{
172172
return (normalizedPath, null, null);

src/Melodee.Common/Services/PlaylistService.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1312,7 +1312,7 @@ public async Task<OperationResult<PlaylistImportResult>> ImportPlaylistAsync(
13121312
using var memoryStream = new MemoryStream();
13131313
await fileStream.CopyToAsync(memoryStream, cancellationToken).ConfigureAwait(false);
13141314
var fileBytes = memoryStream.ToArray();
1315-
1315+
13161316
// Parse the M3U file
13171317
memoryStream.Position = 0;
13181318
var parser = new Parsing.M3UParser(Logger);
@@ -1341,8 +1341,8 @@ public async Task<OperationResult<PlaylistImportResult>> ImportPlaylistAsync(
13411341
{
13421342
UserId = userId,
13431343
OriginalFileName = fileName,
1344-
ContentType = fileName.EndsWith(".m3u8", StringComparison.OrdinalIgnoreCase)
1345-
? "audio/x-mpegurl; charset=utf-8"
1344+
ContentType = fileName.EndsWith(".m3u8", StringComparison.OrdinalIgnoreCase)
1345+
? "audio/x-mpegurl; charset=utf-8"
13461346
: "audio/x-mpegurl",
13471347
Length = fileBytes.Length,
13481348
FileData = fileBytes,

src/Melodee.Common/Services/SongMatchingService.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,9 @@ public async Task<SongMatchResult> MatchEntryAsync(
113113
.Select(candidatePath => context.Songs
114114
.Include(s => s.Album)
115115
.ThenInclude(a => a.Artist)
116-
.FirstOrDefaultAsync(s =>
117-
s.FileName.Replace('\\', '/').EndsWith("/" + candidatePath) ||
118-
s.FileName.Replace('\\', '/') == candidatePath,
116+
.FirstOrDefaultAsync(s =>
117+
s.FileName.Replace('\\', '/').EndsWith("/" + candidatePath) ||
118+
s.FileName.Replace('\\', '/') == candidatePath,
119119
cancellationToken))
120120
.ToList();
121121

@@ -214,7 +214,7 @@ public async Task<SongMatchResult> MatchEntryAsync(
214214
}
215215

216216
var potentialTitle = Path.GetFileNameWithoutExtension(entry.FileName);
217-
217+
218218
// Remove common track number prefixes (01 - Title, 01. Title, etc.)
219219
potentialTitle = System.Text.RegularExpressions.Regex.Replace(potentialTitle, @"^\d+[\s\.\-_]+", "");
220220

tests/Melodee.Tests.Common/Services/Parsing/M3UParserTests.cs

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System.Text;
22
using Melodee.Common.Services.Parsing;
3-
using Serilog;
43
using Serilog.Core;
54

65
namespace Melodee.Tests.Common.Services.Parsing;
@@ -21,7 +20,7 @@ public async Task ParseAsync_WithSimpleM3U_ParsesEntries()
2120
Artist/Album/01 - Song One.flac
2221
Artist/Album/02 - Song Two.flac
2322
""";
24-
23+
2524
using var stream = new MemoryStream(Encoding.UTF8.GetBytes(content));
2625
var result = await parser.ParseAsync(stream, "test.m3u");
2726

@@ -42,7 +41,7 @@ public async Task ParseAsync_SkipsBlankLines_Successfully()
4241
4342
Artist/Album/Song2.flac
4443
""";
45-
44+
4645
using var stream = new MemoryStream(Encoding.UTF8.GetBytes(content));
4746
var result = await parser.ParseAsync(stream, "test.m3u");
4847

@@ -60,7 +59,7 @@ public async Task ParseAsync_SkipsComments_Successfully()
6059
# This is a comment
6160
Artist/Album/Song2.flac
6261
""";
63-
62+
6463
using var stream = new MemoryStream(Encoding.UTF8.GetBytes(content));
6564
var result = await parser.ParseAsync(stream, "test.m3u");
6665

@@ -72,7 +71,7 @@ public async Task ParseAsync_NormalizesBackslashes_ToForwardSlashes()
7271
{
7372
var parser = CreateParser();
7473
var content = @"D:\Music\Artist\Album\Song.mp3";
75-
74+
7675
using var stream = new MemoryStream(Encoding.UTF8.GetBytes(content));
7776
var result = await parser.ParseAsync(stream, "test.m3u");
7877

@@ -85,7 +84,7 @@ public async Task ParseAsync_DecodesUrlEncodedPaths_Successfully()
8584
{
8685
var parser = CreateParser();
8786
var content = "Artist/Album/Song%20With%20Spaces.flac";
88-
87+
8988
using var stream = new MemoryStream(Encoding.UTF8.GetBytes(content));
9089
var result = await parser.ParseAsync(stream, "test.m3u");
9190

@@ -101,7 +100,7 @@ public async Task ParseAsync_RemovesQuotes_FromPaths()
101100
"Artist/Album/Song.flac"
102101
'Artist/Album/Song2.flac'
103102
""";
104-
103+
105104
using var stream = new MemoryStream(Encoding.UTF8.GetBytes(content));
106105
var result = await parser.ParseAsync(stream, "test.m3u");
107106

@@ -115,7 +114,7 @@ public async Task ParseAsync_ExtractsPathHints_Correctly()
115114
{
116115
var parser = CreateParser();
117116
var content = "Pink Floyd/The Wall/02 - Another Brick in the Wall.flac";
118-
117+
119118
using var stream = new MemoryStream(Encoding.UTF8.GetBytes(content));
120119
var result = await parser.ParseAsync(stream, "test.m3u");
121120

@@ -131,7 +130,7 @@ public async Task ParseAsync_HandlesAbsolutePaths_WithMultipleSegments()
131130
{
132131
var parser = CreateParser();
133132
var content = "/mnt/music/Artist/Album/Song.flac";
134-
133+
135134
using var stream = new MemoryStream(Encoding.UTF8.GetBytes(content));
136135
var result = await parser.ParseAsync(stream, "test.m3u");
137136

@@ -147,7 +146,7 @@ public async Task ParseAsync_HandlesSingleFileNameOnly_WithoutFolders()
147146
{
148147
var parser = CreateParser();
149148
var content = "song.flac";
150-
149+
151150
using var stream = new MemoryStream(Encoding.UTF8.GetBytes(content));
152151
var result = await parser.ParseAsync(stream, "test.m3u");
153152

@@ -163,7 +162,7 @@ public async Task ParseAsync_HandlesM3U8_UsesUTF8Encoding()
163162
{
164163
var parser = CreateParser();
165164
var content = "Artist/Album/日本語のタイトル.flac";
166-
165+
167166
using var stream = new MemoryStream(Encoding.UTF8.GetBytes(content));
168167
var result = await parser.ParseAsync(stream, "test.m3u8");
169168

@@ -182,7 +181,7 @@ public async Task ParseAsync_PreservesSortOrder_FromLineNumbers()
182181
Second.flac
183182
Third.flac
184183
""";
185-
184+
186185
using var stream = new MemoryStream(Encoding.UTF8.GetBytes(content));
187186
var result = await parser.ParseAsync(stream, "test.m3u");
188187

@@ -197,7 +196,7 @@ public async Task ParseAsync_HandlesUrlsAsEntries_WithoutCrashing()
197196
{
198197
var parser = CreateParser();
199198
var content = "http://example.com/stream.mp3";
200-
199+
201200
using var stream = new MemoryStream(Encoding.UTF8.GetBytes(content));
202201
var result = await parser.ParseAsync(stream, "test.m3u");
203202

@@ -212,7 +211,7 @@ public async Task ParseAsync_HandlesSpecialCharacters_InPaths()
212211
{
213212
var parser = CreateParser();
214213
var content = "Artist/Album [2024]/Song (Remastered).flac";
215-
214+
216215
using var stream = new MemoryStream(Encoding.UTF8.GetBytes(content));
217216
var result = await parser.ParseAsync(stream, "test.m3u");
218217

0 commit comments

Comments
 (0)