diff options
| author | JP Dillingham <jp@dillingham.me> | 2025-04-05 16:51:39 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-04-05 16:51:39 -0500 |
| commit | ac53fb5b3639292113b06876a05f402f7d1f0520 (patch) | |
| tree | 29726be4a5ecdb1aae6e4f296ad5af7152ea9ce2 /src | |
| parent | eec6e0f098effcb9bfda297a441ca7ffd8d33934 (diff) | |
| parent | 1a3e8c9d8a1857e1ac0c3fe92d5ab1408aa3d0df (diff) | |
| download | slskd-ac53fb5b3639292113b06876a05f402f7d1f0520.tar.gz | |
Merge pull request #1303 from dkschruteBeets/feature/add-profile-picture-support
Feature/add profile picture support
Diffstat (limited to 'src')
| -rw-r--r-- | src/slskd/Application.cs | 8 | ||||
| -rw-r--r-- | src/slskd/Core/Options.cs | 10 | ||||
| -rw-r--r-- | src/slskd/Program.cs | 5 | ||||
| -rw-r--r-- | src/slskd/Users/IUserService.cs | 7 | ||||
| -rw-r--r-- | src/slskd/Users/UserService.cs | 51 |
5 files changed, 77 insertions, 4 deletions
diff --git a/src/slskd/Application.cs b/src/slskd/Application.cs index e5681442..acfecdbf 100644 --- a/src/slskd/Application.cs +++ b/src/slskd/Application.cs @@ -1662,13 +1662,16 @@ namespace slskd /// <returns>A Task resolving the UserInfo instance.</returns> private async Task<UserInfo> 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 @@ -1532,6 +1532,14 @@ namespace slskd public string Description { get; init; } = "A slskd user. https://github.com/slskd/slskd"; /// <summary> + /// Gets the file path for the user's profile picture. + /// </summary> + [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; + + /// <summary> /// Gets the local IP address on which to listen for incoming connections. /// </summary> [Argument(default, "slsk-listen-ip-address")] @@ -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; } /// <summary> 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 @@ -203,6 +203,11 @@ namespace slskd public static string DefaultIncompleteDirectory { get; private set; } /// <summary> + /// Gets the default profile picture path. + /// </summary> + public static string DefaultProfilePicturePath { get; private set; } = Path.Combine(AppContext.BaseDirectory, "slsk-profile-picture.jpg"); + + /// <summary> /// Gets the path where application logs are saved. /// </summary> public static string LogDirectory { get; private set; } = null; 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 /// <param name="username">The username of the peer.</param> /// <returns>The operation context.</returns> Task WatchAsync(string username); + + /// <summary> + /// Gets the profile picture as a byte array if it exists. + /// </summary> + /// <param name="profilePicture">The profile picture path to resolve.</param> + /// <returns>The profile picture as a byte array if it exists and can be read, null otherwise.</returns> + 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; /// <summary> @@ -65,16 +68,21 @@ namespace slskd.Users /// </remarks> public class UserService : IUserService { + private readonly FileService fileService; + /// <summary> /// Initializes a new instance of the <see cref="UserService"/> class. /// </summary> /// <param name="soulseekClient"></param> /// <param name="optionsMonitor"></param> + /// <param name="fileService"></param> public UserService( ISoulseekClient soulseekClient, - IOptionsMonitor<Options> optionsMonitor) + IOptionsMonitor<Options> optionsMonitor, + FileService fileService) { Client = soulseekClient; + this.fileService = fileService; OptionsMonitor = optionsMonitor; OptionsMonitor.OnChange(options => Configure(options)); @@ -147,6 +155,47 @@ namespace slskd.Users private ConcurrentDictionary<string, bool> WatchedUsernamesDictionary { get; set; } = new ConcurrentDictionary<string, bool>(); /// <summary> + /// Gets the profile picture as a byte array if it exists. + /// </summary> + /// <param name="profilePicture">The profile picture path to resolve.</param> + /// <returns>The profile picture as a byte array if it exists and can be read, null otherwise.</returns> + 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; + } + } + + /// <summary> /// Gets the name of the group for the specified <paramref name="username"/>. /// </summary> /// <remarks>The group name is fetched from cached data, and lookups should always be fast.</remarks> |
