From 07952c6ebe125120b848e0e9d5de85644185d9a5 Mon Sep 17 00:00:00 2001 From: Terry Wilson <103469370+dkschruteBeets@users.noreply.github.com> Date: Sun, 2 Mar 2025 16:55:19 -0700 Subject: feat: add profile picture support --- src/slskd/Application.cs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'src/slskd/Application.cs') diff --git a/src/slskd/Application.cs b/src/slskd/Application.cs index 791cf245..6a539fa9 100644 --- a/src/slskd/Application.cs +++ b/src/slskd/Application.cs @@ -1662,13 +1662,16 @@ namespace slskd /// A Task resolving the UserInfo instance. private async Task UserInfoResolver(string username, IPEndPoint endpoint) { + var profilePicture = Users.GetProfilePicture(Options.Soulseek.ProfilePicture); + if (Users.IsBlacklisted(username, endpoint.Address)) { return new UserInfo( description: Options.Soulseek.Description, uploadSlots: 0, queueLength: int.MaxValue, - hasFreeUploadSlot: false); + hasFreeUploadSlot: false, + picture: profilePicture); } try @@ -1692,7 +1695,8 @@ namespace slskd description: Options.Soulseek.Description, uploadSlots: group.Slots, queueLength: forecastedPosition, - hasFreeUploadSlot: forecastedPosition == 0); + hasFreeUploadSlot: forecastedPosition == 0, + picture: profilePicture); return info; } -- cgit v1.2.3 From 831e9ce49abbcc64a7b75c6fb2c90ad20b233e00 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 6 Apr 2025 17:25:46 -0500 Subject: formatting/docs --- src/slskd/Application.cs | 3 ++- tests/slskd.Tests.Unit/Users/UserServiceTests.cs | 16 ++++++++-------- 2 files changed, 10 insertions(+), 9 deletions(-) (limited to 'src/slskd/Application.cs') diff --git a/src/slskd/Application.cs b/src/slskd/Application.cs index acfecdbf..1e71652c 100644 --- a/src/slskd/Application.cs +++ b/src/slskd/Application.cs @@ -1662,7 +1662,7 @@ namespace slskd /// A Task resolving the UserInfo instance. private async Task UserInfoResolver(string username, IPEndPoint endpoint) { - var profilePicture = Users.GetProfilePicture(Options.Soulseek.ProfilePicture); + var profilePicture = Users.GetProfilePicture(Options.Soulseek.Picture); if (Users.IsBlacklisted(username, endpoint.Address)) { @@ -1691,6 +1691,7 @@ namespace slskd // i want to know how many slots they have, which gives me an idea of how fast their // queue moves, and the length of the queue *ahead of me*, meaning how long i'd have to // wait until my first download starts. + // revisited 3 years later: why was it important to leave this comment?? var info = new UserInfo( description: Options.Soulseek.Description, uploadSlots: group.Slots, diff --git a/tests/slskd.Tests.Unit/Users/UserServiceTests.cs b/tests/slskd.Tests.Unit/Users/UserServiceTests.cs index 28f54629..205ff0e2 100644 --- a/tests/slskd.Tests.Unit/Users/UserServiceTests.cs +++ b/tests/slskd.Tests.Unit/Users/UserServiceTests.cs @@ -130,9 +130,9 @@ public void Returns_Null_When_Path_Is_NullOrWhitespace(string path) { var (service, _) = GetFixture(); - + var result = service.GetProfilePicture(path); - + Assert.Null(result); } @@ -141,9 +141,9 @@ { const string nonExistentPath = "nonexistent-file.jpg"; var (service, _) = GetFixture(); - + var result = service.GetProfilePicture(nonExistentPath); - + Assert.Null(result); } @@ -155,11 +155,11 @@ { // Write some test data to the temp file System.IO.File.WriteAllBytes(tempFile, new byte[] { 1, 2, 3, 4, 5 }); - + var (service, _) = GetFixture(); - + var result = service.GetProfilePicture(tempFile); - + Assert.NotNull(result); Assert.Equal(new byte[] { 1, 2, 3, 4, 5 }, result); } @@ -174,7 +174,7 @@ } } - private static (UserService governor, Mocks mocks) GetFixture(Options options = null) + private static (UserService service, Mocks mocks) GetFixture(Options options = null) { var mocks = new Mocks(options); var service = new UserService( -- cgit v1.2.3 From 21da11dfae33529d2672da06a8fa52b5343b5239 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Mon, 7 Apr 2025 12:33:22 -0500 Subject: simplify fetch of picture bytes --- src/slskd/Application.cs | 15 +++++-- src/slskd/Users/IUserService.cs | 7 --- src/slskd/Users/UserService.cs | 49 +-------------------- tests/slskd.Tests.Unit/Users/UserServiceTests.cs | 56 +----------------------- 4 files changed, 14 insertions(+), 113 deletions(-) (limited to 'src/slskd/Application.cs') diff --git a/src/slskd/Application.cs b/src/slskd/Application.cs index 1e71652c..4ed9d092 100644 --- a/src/slskd/Application.cs +++ b/src/slskd/Application.cs @@ -1662,7 +1662,16 @@ namespace slskd /// A Task resolving the UserInfo instance. private async Task UserInfoResolver(string username, IPEndPoint endpoint) { - var profilePicture = Users.GetProfilePicture(Options.Soulseek.Picture); + byte[] pictureBytes = null; + + try + { + pictureBytes = await System.IO.File.ReadAllBytesAsync(Options.Soulseek.Picture); + } + catch (Exception ex) + { + Log.Warning("Failed to read Soulseek picture {Picture}: {Message}", Options.Soulseek.Picture, ex.Message); + } if (Users.IsBlacklisted(username, endpoint.Address)) { @@ -1671,7 +1680,7 @@ namespace slskd uploadSlots: 0, queueLength: int.MaxValue, hasFreeUploadSlot: false, - picture: profilePicture); + picture: pictureBytes); } try @@ -1697,7 +1706,7 @@ namespace slskd uploadSlots: group.Slots, queueLength: forecastedPosition, hasFreeUploadSlot: forecastedPosition == 0, - picture: profilePicture); + picture: pictureBytes); return info; } diff --git a/src/slskd/Users/IUserService.cs b/src/slskd/Users/IUserService.cs index e84d7461..75afd442 100644 --- a/src/slskd/Users/IUserService.cs +++ b/src/slskd/Users/IUserService.cs @@ -152,12 +152,5 @@ namespace slskd.Users /// The username of the peer. /// The operation context. Task WatchAsync(string username); - - /// - /// Gets the profile picture as a byte array if it exists. - /// - /// The profile picture path to resolve. - /// The profile picture as a byte array if it exists and can be read, null otherwise. - byte[] GetProfilePicture(string profilePicture); } } \ No newline at end of file diff --git a/src/slskd/Users/UserService.cs b/src/slskd/Users/UserService.cs index d634704a..55b02134 100644 --- a/src/slskd/Users/UserService.cs +++ b/src/slskd/Users/UserService.cs @@ -68,22 +68,16 @@ namespace slskd.Users /// public class UserService : IUserService { - private readonly FileService fileService; - /// /// Initializes a new instance of the class. /// /// /// - /// public UserService( ISoulseekClient soulseekClient, - IOptionsMonitor optionsMonitor, - FileService fileService) + IOptionsMonitor optionsMonitor) { Client = soulseekClient; - this.fileService = fileService; - OptionsMonitor = optionsMonitor; OptionsMonitor.OnChange(options => Configure(options)); @@ -154,47 +148,6 @@ namespace slskd.Users private ConcurrentDictionary UserDictionary { get; set; } = new ConcurrentDictionary(); private ConcurrentDictionary WatchedUsernamesDictionary { get; set; } = new ConcurrentDictionary(); - /// - /// Gets the profile picture as a byte array if it exists. - /// - /// The profile picture path to resolve. - /// The profile picture as a byte array if it exists and can be read, null otherwise. - public byte[] GetProfilePicture(string profilePicture) - { - if (string.IsNullOrWhiteSpace(profilePicture)) - { - Log.Warning("Profile picture path is not set"); - return null; - } - - try - { - // Ensure the path is relative to the app directory for security - var fullPath = Path.GetFullPath( - Path.Combine(AppContext.BaseDirectory, profilePicture)); - - var fileInfo = fileService.ResolveFileInfo(fullPath); - if (!fileInfo.Exists) - { - Log.Warning("Profile picture '{Path}' not found", profilePicture); - return null; - } - - // Read the file into a byte array - return System.IO.File.ReadAllBytes(fileInfo.FullName); - } - catch (UnauthorizedException ex) - { - Log.Warning(ex, "Access to profile picture '{Path}' was denied", profilePicture); - return null; - } - catch (IOException ex) - { - Log.Warning(ex, "Failed to read profile picture '{Path}'", profilePicture); - return null; - } - } - /// /// Gets the name of the group for the specified . /// diff --git a/tests/slskd.Tests.Unit/Users/UserServiceTests.cs b/tests/slskd.Tests.Unit/Users/UserServiceTests.cs index 205ff0e2..8581a966 100644 --- a/tests/slskd.Tests.Unit/Users/UserServiceTests.cs +++ b/tests/slskd.Tests.Unit/Users/UserServiceTests.cs @@ -121,66 +121,12 @@ } } - public class GetProfilePicture - { - [Theory] - [InlineData(null)] - [InlineData("")] - [InlineData(" ")] - public void Returns_Null_When_Path_Is_NullOrWhitespace(string path) - { - var (service, _) = GetFixture(); - - var result = service.GetProfilePicture(path); - - Assert.Null(result); - } - - [Fact] - public void Returns_Null_When_File_Does_Not_Exist() - { - const string nonExistentPath = "nonexistent-file.jpg"; - var (service, _) = GetFixture(); - - var result = service.GetProfilePicture(nonExistentPath); - - Assert.Null(result); - } - - [Fact] - public void Returns_File_Bytes_When_File_Exists() - { - var tempFile = Path.GetTempFileName(); - try - { - // Write some test data to the temp file - System.IO.File.WriteAllBytes(tempFile, new byte[] { 1, 2, 3, 4, 5 }); - - var (service, _) = GetFixture(); - - var result = service.GetProfilePicture(tempFile); - - Assert.NotNull(result); - Assert.Equal(new byte[] { 1, 2, 3, 4, 5 }, result); - } - finally - { - // Clean up - if (System.IO.File.Exists(tempFile)) - { - System.IO.File.Delete(tempFile); - } - } - } - } - private static (UserService service, Mocks mocks) GetFixture(Options options = null) { var mocks = new Mocks(options); var service = new UserService( mocks.SoulseekClient.Object, - mocks.OptionsMonitor, - mocks.FileService); + mocks.OptionsMonitor); return (service, mocks); } -- cgit v1.2.3 From f174c680f6133580033d9baaff71d40bce43e753 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Mon, 7 Apr 2025 12:33:36 -0500 Subject: leave some notes --- src/slskd/Application.cs | 4 ++++ src/slskd/Users/UserService.cs | 3 --- tests/slskd.Tests.Unit/Users/UserServiceTests.cs | 4 ---- 3 files changed, 4 insertions(+), 7 deletions(-) (limited to 'src/slskd/Application.cs') diff --git a/src/slskd/Application.cs b/src/slskd/Application.cs index 4ed9d092..f13a1c9b 100644 --- a/src/slskd/Application.cs +++ b/src/slskd/Application.cs @@ -1666,10 +1666,14 @@ namespace slskd try { + // note: the Picture setting is validated at startup to ensure it exists and that + // it is readable pictureBytes = await System.IO.File.ReadAllBytesAsync(Options.Soulseek.Picture); } catch (Exception ex) { + // this isn't a serious enough problem to prevent us from continuing, so we'll just + // log a warning and continue, omitting the picture Log.Warning("Failed to read Soulseek picture {Picture}: {Message}", Options.Soulseek.Picture, ex.Message); } diff --git a/src/slskd/Users/UserService.cs b/src/slskd/Users/UserService.cs index 55b02134..2e312f69 100644 --- a/src/slskd/Users/UserService.cs +++ b/src/slskd/Users/UserService.cs @@ -19,16 +19,13 @@ using Microsoft.Extensions.Options; namespace slskd.Users { - using System; using System.Collections.Concurrent; using System.Collections.Generic; - using System.IO; using System.Linq; using System.Net; using System.Threading.Tasks; using NetTools; using Serilog; - using slskd.Files; using Soulseek; /// diff --git a/tests/slskd.Tests.Unit/Users/UserServiceTests.cs b/tests/slskd.Tests.Unit/Users/UserServiceTests.cs index 8581a966..ca45d5b9 100644 --- a/tests/slskd.Tests.Unit/Users/UserServiceTests.cs +++ b/tests/slskd.Tests.Unit/Users/UserServiceTests.cs @@ -1,10 +1,8 @@ namespace slskd.Tests.Unit.Users { using System.Collections.Generic; - using System.IO; using AutoFixture.Xunit2; using Moq; - using slskd.Files; using slskd.Users; using Soulseek; using Xunit; @@ -136,12 +134,10 @@ public Mocks(Options options = null) { OptionsMonitor = new TestOptionsMonitor(options ?? new Options()); - FileService = new FileService(OptionsMonitor); } public Mock SoulseekClient { get; } = new Mock(); public TestOptionsMonitor OptionsMonitor { get; init; } - public FileService FileService { get; init; } } } } -- cgit v1.2.3 From 8c98fc35ca41cf52e6af746e608926b516fe3ad8 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Mon, 7 Apr 2025 14:46:58 -0500 Subject: check for configured file before trying to read it --- src/slskd/Application.cs | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) (limited to 'src/slskd/Application.cs') diff --git a/src/slskd/Application.cs b/src/slskd/Application.cs index f13a1c9b..9179987e 100644 --- a/src/slskd/Application.cs +++ b/src/slskd/Application.cs @@ -1664,17 +1664,20 @@ namespace slskd { byte[] pictureBytes = null; - try + if (!string.IsNullOrWhiteSpace(Options.Soulseek.Picture)) { - // note: the Picture setting is validated at startup to ensure it exists and that - // it is readable - pictureBytes = await System.IO.File.ReadAllBytesAsync(Options.Soulseek.Picture); - } - catch (Exception ex) - { - // this isn't a serious enough problem to prevent us from continuing, so we'll just - // log a warning and continue, omitting the picture - Log.Warning("Failed to read Soulseek picture {Picture}: {Message}", Options.Soulseek.Picture, ex.Message); + try + { + // note: the Picture setting is validated at startup to ensure it exists and that + // it is readable + pictureBytes = await System.IO.File.ReadAllBytesAsync(Options.Soulseek.Picture); + } + catch (Exception ex) + { + // this isn't a serious enough problem to prevent us from continuing, so we'll just + // log a warning and continue, omitting the picture + Log.Warning("Failed to read Soulseek picture {Picture}: {Message}", Options.Soulseek.Picture, ex.Message); + } } if (Users.IsBlacklisted(username, endpoint.Address)) -- cgit v1.2.3 From 66c60cadfa300d88f1b66d9e4d6174803b18c3ef Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 12 Apr 2025 16:01:09 -0500 Subject: show Soulseek.NET diagnostic stack traces when debugging --- src/slskd/Application.cs | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'src/slskd/Application.cs') diff --git a/src/slskd/Application.cs b/src/slskd/Application.cs index 9179987e..2449e8e0 100644 --- a/src/slskd/Application.cs +++ b/src/slskd/Application.cs @@ -830,6 +830,12 @@ namespace slskd var logger = Loggers.GetOrAdd(source, Log.ForContext("Context", "Soulseek").ForContext("SubContext", source)); + if (args.IncludesException && OptionsAtStartup.Debug) + { + logger.Write(TranslateLogLevel(args.Level), exception: args.Exception, "{@Message}", args.Message); + return; + } + logger.Write(TranslateLogLevel(args.Level), "{@Message}", args.Message); } -- cgit v1.2.3