Files
EchoHub/src/EchoHub.Client/Services/AvatarHelper.cs
T
HueByte c8ea124198 Add hex color parsing helper and implement custom list sources for channels and users
- Introduced HexColorHelper for parsing hex color strings to Terminal.Gui Attributes and Colors.
- Created ChannelListSource for managing and rendering a list of channels with unread counts and active indicators.
- Developed UserListSource for displaying online users with per-user nickname colors.
- Refactored MainWindow to utilize ChatMessageManager for handling messages and channel state.
- Updated project references and removed unused content from the server project.
- Adjusted tests to reflect changes in namespaces and structure.
2026-02-22 08:08:21 +01:00

45 lines
1.3 KiB
C#

using Serilog;
namespace EchoHub.Client.Services;
/// <summary>
/// Shared avatar upload logic — resolves a file path or URL to a stream
/// and uploads it via ApiClient.
/// </summary>
internal static class AvatarHelper
{
/// <summary>
/// Upload an avatar from a local file path or HTTP(S) URL.
/// Returns the ASCII art response from the server.
/// </summary>
public static async Task<string?> UploadAsync(ApiClient apiClient, string target)
{
Stream stream;
string fileName;
if (Uri.TryCreate(target, UriKind.Absolute, out var uri)
&& (uri.Scheme == "http" || uri.Scheme == "https"))
{
using var http = new HttpClient();
var bytes = await http.GetByteArrayAsync(uri);
stream = new MemoryStream(bytes);
fileName = Path.GetFileName(uri.LocalPath);
if (string.IsNullOrWhiteSpace(fileName) || !fileName.Contains('.'))
fileName = "avatar.png";
}
else
{
if (!File.Exists(target))
throw new FileNotFoundException($"File not found: {target}");
stream = File.OpenRead(target);
fileName = Path.GetFileName(target);
}
await using (stream)
{
return await apiClient.UploadAvatarAsync(stream, fileName);
}
}
}