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/slskd/Users/UserService.cs | |
| 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/slskd/Users/UserService.cs')
| -rw-r--r-- | src/slskd/Users/UserService.cs | 51 |
1 files changed, 50 insertions, 1 deletions
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> |
