aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJP Dillingham <jp@dillingham.me>2025-04-05 16:51:39 -0500
committerGitHub <noreply@github.com>2025-04-05 16:51:39 -0500
commitac53fb5b3639292113b06876a05f402f7d1f0520 (patch)
tree29726be4a5ecdb1aae6e4f296ad5af7152ea9ce2
parenteec6e0f098effcb9bfda297a441ca7ffd8d33934 (diff)
parent1a3e8c9d8a1857e1ac0c3fe92d5ab1408aa3d0df (diff)
downloadslskd-ac53fb5b3639292113b06876a05f402f7d1f0520.tar.gz
Merge pull request #1303 from dkschruteBeets/feature/add-profile-picture-support
Feature/add profile picture support
-rw-r--r--config/slskd.example.yml3
-rw-r--r--src/slskd/Application.cs8
-rw-r--r--src/slskd/Core/Options.cs10
-rw-r--r--src/slskd/Program.cs5
-rw-r--r--src/slskd/Users/IUserService.cs7
-rw-r--r--src/slskd/Users/UserService.cs51
-rw-r--r--tests/slskd.Tests.Unit/Users/UserServiceTests.cs61
7 files changed, 138 insertions, 7 deletions
diff --git a/config/slskd.example.yml b/config/slskd.example.yml
index 9cd9584f..69725415 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 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>
diff --git a/tests/slskd.Tests.Unit/Users/UserServiceTests.cs b/tests/slskd.Tests.Unit/Users/UserServiceTests.cs
index 1fd21c32..28f54629 100644
--- a/tests/slskd.Tests.Unit/Users/UserServiceTests.cs
+++ b/tests/slskd.Tests.Unit/Users/UserServiceTests.cs
@@ -1,9 +1,10 @@
namespace slskd.Tests.Unit.Users
{
using System.Collections.Generic;
+ using System.IO;
using AutoFixture.Xunit2;
- using Microsoft.EntityFrameworkCore;
using Moq;
+ using slskd.Files;
using slskd.Users;
using Soulseek;
using Xunit;
@@ -120,12 +121,66 @@
}
}
+ 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 governor, Mocks mocks) GetFixture(Options options = null)
{
var mocks = new Mocks(options);
var service = new UserService(
mocks.SoulseekClient.Object,
- mocks.OptionsMonitor);
+ mocks.OptionsMonitor,
+ mocks.FileService);
return (service, mocks);
}
@@ -135,10 +190,12 @@
public Mocks(Options options = null)
{
OptionsMonitor = new TestOptionsMonitor<Options>(options ?? new Options());
+ FileService = new FileService(OptionsMonitor);
}
public Mock<ISoulseekClient> SoulseekClient { get; } = new Mock<ISoulseekClient>();
public TestOptionsMonitor<Options> OptionsMonitor { get; init; }
+ public FileService FileService { get; init; }
}
}
}