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 +---------------------------------------- 3 files changed, 13 insertions(+), 58 deletions(-) (limited to 'src') 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 . /// -- cgit v1.2.3