aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJP Dillingham <jp@dillingham.me>2025-04-07 12:33:22 -0500
committerJP Dillingham <jp@dillingham.me>2025-04-07 12:33:22 -0500
commit21da11dfae33529d2672da06a8fa52b5343b5239 (patch)
tree3de39aea286ccb9cc350307817b30e3981f3bbf6
parent831e9ce49abbcc64a7b75c6fb2c90ad20b233e00 (diff)
downloadslskd-21da11dfae33529d2672da06a8fa52b5343b5239.tar.gz
simplify fetch of picture bytes
-rw-r--r--src/slskd/Application.cs15
-rw-r--r--src/slskd/Users/IUserService.cs7
-rw-r--r--src/slskd/Users/UserService.cs49
-rw-r--r--tests/slskd.Tests.Unit/Users/UserServiceTests.cs56
4 files changed, 14 insertions, 113 deletions
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
/// <returns>A Task resolving the UserInfo instance.</returns>
private async Task<UserInfo> 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
/// <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 d634704a..55b02134 100644
--- a/src/slskd/Users/UserService.cs
+++ b/src/slskd/Users/UserService.cs
@@ -68,22 +68,16 @@ 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,
- FileService fileService)
+ IOptionsMonitor<Options> optionsMonitor)
{
Client = soulseekClient;
- this.fileService = fileService;
-
OptionsMonitor = optionsMonitor;
OptionsMonitor.OnChange(options => Configure(options));
@@ -155,47 +149,6 @@ 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 205ff0e2..8581a966 100644
--- a/tests/slskd.Tests.Unit/Users/UserServiceTests.cs
+++ b/tests/slskd.Tests.Unit/Users/UserServiceTests.cs
@@ -121,66 +121,12 @@
}
}
- public class GetProfilePicture
- {
- [Theory]
- [InlineData(null)]
- [InlineData("")]
- [InlineData(" ")]
- public void Returns_Null_When_Path_Is_NullOrWhitespace(string path)
- {
- var (service, _) = GetFixture();
-
- var result = service.GetProfilePicture(path);
-
- Assert.Null(result);
- }
-
- [Fact]
- public void Returns_Null_When_File_Does_Not_Exist()
- {
- const string nonExistentPath = "nonexistent-file.jpg";
- var (service, _) = GetFixture();
-
- var result = service.GetProfilePicture(nonExistentPath);
-
- Assert.Null(result);
- }
-
- [Fact]
- public void Returns_File_Bytes_When_File_Exists()
- {
- var tempFile = Path.GetTempFileName();
- try
- {
- // Write some test data to the temp file
- System.IO.File.WriteAllBytes(tempFile, new byte[] { 1, 2, 3, 4, 5 });
-
- var (service, _) = GetFixture();
-
- var result = service.GetProfilePicture(tempFile);
-
- Assert.NotNull(result);
- Assert.Equal(new byte[] { 1, 2, 3, 4, 5 }, result);
- }
- finally
- {
- // Clean up
- if (System.IO.File.Exists(tempFile))
- {
- System.IO.File.Delete(tempFile);
- }
- }
- }
- }
-
private static (UserService service, Mocks mocks) GetFixture(Options options = null)
{
var mocks = new Mocks(options);
var service = new UserService(
mocks.SoulseekClient.Object,
- mocks.OptionsMonitor,
- mocks.FileService);
+ mocks.OptionsMonitor);
return (service, mocks);
}