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 +++++-- src/slskd/Core/Options.cs | 10 +++++++- src/slskd/Program.cs | 5 ++++ src/slskd/Users/IUserService.cs | 7 ++++++ src/slskd/Users/UserService.cs | 51 ++++++++++++++++++++++++++++++++++++++++- 5 files changed, 77 insertions(+), 4 deletions(-) (limited to 'src') 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; } diff --git a/src/slskd/Core/Options.cs b/src/slskd/Core/Options.cs index 266d04aa..68856b36 100644 --- a/src/slskd/Core/Options.cs +++ b/src/slskd/Core/Options.cs @@ -1531,6 +1531,14 @@ namespace slskd [Description("user description for the Soulseek network")] public string Description { get; init; } = "A slskd user. https://github.com/slskd/slskd"; + /// + /// Gets the file path for the user's profile picture. + /// + [Argument(default, "slsk-profile-picture")] + [EnvironmentVariable("SLSK_PROFILE_PICTURE")] + [Description("path to the user's profile picture file")] + public string ProfilePicture { get; init; } = Program.DefaultProfilePicturePath; + /// /// Gets the local IP address on which to listen for incoming connections. /// @@ -1656,7 +1664,7 @@ namespace slskd [EnvironmentVariable("SLSK_INACTIVITY_TIMEOUT")] [Description("connection inactivity timeout, in milliseconds")] [Range(1000, int.MaxValue)] - public int Inactivity { get; init; } = 15000; + public int Inactivity { get; init; } = 30000; } /// diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index b7fa47d2..c2b28cc9 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -202,6 +202,11 @@ namespace slskd /// public static string DefaultIncompleteDirectory { get; private set; } + /// + /// Gets the default profile picture path. + /// + public static string DefaultProfilePicturePath { get; private set; } = Path.Combine(AppContext.BaseDirectory, "slsk-profile-picture.jpg"); + /// /// Gets the path where application logs are saved. /// diff --git a/src/slskd/Users/IUserService.cs b/src/slskd/Users/IUserService.cs index 75afd442..e84d7461 100644 --- a/src/slskd/Users/IUserService.cs +++ b/src/slskd/Users/IUserService.cs @@ -152,5 +152,12 @@ 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 ead1d812..d634704a 100644 --- a/src/slskd/Users/UserService.cs +++ b/src/slskd/Users/UserService.cs @@ -19,13 +19,16 @@ 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; /// @@ -65,16 +68,21 @@ namespace slskd.Users /// public class UserService : IUserService { + private readonly FileService fileService; + /// /// Initializes a new instance of the class. /// /// /// + /// public UserService( ISoulseekClient soulseekClient, - IOptionsMonitor optionsMonitor) + IOptionsMonitor optionsMonitor, + FileService fileService) { Client = soulseekClient; + this.fileService = fileService; OptionsMonitor = optionsMonitor; OptionsMonitor.OnChange(options => Configure(options)); @@ -146,6 +154,47 @@ 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 . /// -- cgit v1.2.3