diff options
| author | Terry Wilson <103469370+dkschruteBeets@users.noreply.github.com> | 2025-03-02 16:55:19 -0700 |
|---|---|---|
| committer | Terry Wilson <103469370+dkschruteBeets@users.noreply.github.com> | 2025-03-02 16:55:19 -0700 |
| commit | 07952c6ebe125120b848e0e9d5de85644185d9a5 (patch) | |
| tree | ac522af44b7908a5cf740375e88fec05f6c800db | |
| parent | 4de21d94899605accdba680eeff95b8dc38b59e2 (diff) | |
| download | slskd-07952c6ebe125120b848e0e9d5de85644185d9a5.tar.gz | |
feat: add profile picture support
| -rw-r--r-- | config/slskd.example.yml | 3 | ||||
| -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 | ||||
| -rw-r--r-- | tests/slskd.Tests.Unit/Users/UserServiceTests.cs | 5 |
7 files changed, 83 insertions, 6 deletions
diff --git a/config/slskd.example.yml b/config/slskd.example.yml index e76af51c..26d54afd 100644 --- a/config/slskd.example.yml +++ b/config/slskd.example.yml @@ -192,6 +192,7 @@ # password: ~ # description: | # A slskd user. https://github.com/slskd/slskd +# profile_picture: slsk-profile-picture.jpg # listen_ip_address: 0.0.0.0 # listen_port: 50300 # diagnostic_level: Info @@ -203,7 +204,7 @@ # connection: # timeout: # connect: 10000 -# inactivity: 15000 +# inactivity: 30000 # buffer: # read: 16384 # write: 16384 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 /// <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> diff --git a/tests/slskd.Tests.Unit/Users/UserServiceTests.cs b/tests/slskd.Tests.Unit/Users/UserServiceTests.cs index 1fd21c32..01fe855e 100644 --- a/tests/slskd.Tests.Unit/Users/UserServiceTests.cs +++ b/tests/slskd.Tests.Unit/Users/UserServiceTests.cs @@ -4,6 +4,7 @@ using AutoFixture.Xunit2; using Microsoft.EntityFrameworkCore; using Moq; + using slskd.Files; using slskd.Users; using Soulseek; using Xunit; @@ -125,7 +126,8 @@ var mocks = new Mocks(options); var service = new UserService( mocks.SoulseekClient.Object, - mocks.OptionsMonitor); + mocks.OptionsMonitor, + mocks.FileService); return (service, mocks); } @@ -139,6 +141,7 @@ public Mock<ISoulseekClient> SoulseekClient { get; } = new Mock<ISoulseekClient>(); public TestOptionsMonitor<Options> OptionsMonitor { get; init; } + public FileService FileService { get; init; } } } } |
