Skip to content

Commit

Permalink
Remove unused paginator service; Remove unused cached database servic…
Browse files Browse the repository at this point in the history
…e; Modify image utilities service; avatar: Use image utilities service
  • Loading branch information
OoLunar committed Nov 21, 2023
1 parent 46267ba commit fc09a65
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 624 deletions.
36 changes: 11 additions & 25 deletions src/Commands/Common/AvatarCommand.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Globalization;
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;
using DSharpPlus;
using DSharpPlus.Commands.ContextChecks;
Expand All @@ -11,14 +9,12 @@
using DSharpPlus.Commands.Trees;
using DSharpPlus.Commands.Trees.Attributes;
using DSharpPlus.Entities;
using Humanizer;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Metadata;
using OoLunar.Tomoe.Services;

namespace OoLunar.Tomoe.Commands.Common
{
[Command("avatar"), TextAlias("pfp")]
public sealed class AvatarCommand(HttpClient httpClient)
public sealed class AvatarCommand(ImageUtilitiesService imageUtilitiesService)
{
[Command("user"), SlashCommandTypes(ApplicationCommandType.SlashCommand, ApplicationCommandType.UserContextMenu)]
public ValueTask UserAsync(CommandContext context, DiscordUser? user = null, ImageFormat imageFormat = ImageFormat.Auto, ushort imageDimensions = 0)
Expand Down Expand Up @@ -65,41 +61,31 @@ public async ValueTask GuildAsync(CommandContext context, DiscordMember? member
private async ValueTask SendAvatarAsync(CommandContext context, string embedTitle, string url, DiscordColor? embedColor = null)
{
await context.DeferResponseAsync();
using HttpResponseMessage response = await httpClient.GetAsync(url);
if (!response.IsSuccessStatusCode)
ImageData? imageData = await imageUtilitiesService.GetImageDataAsync(url);
if (imageData is null)
{
// The embed title is set to something like "Lunar's Avatar", so we can just use the embed title.
await context.EditResponseAsync(new DiscordMessageBuilder().WithContent($"Failed to retrieve {embedTitle}. HTTP Error `{response.StatusCode}`."));
await context.RespondAsync(new DiscordMessageBuilder().WithContent($"Failed to retrieve {embedTitle}."));
return;
}

using Stream imageStream = await response.Content.ReadAsStreamAsync();
Image image = await Image.LoadAsync(imageStream);
DiscordEmbedBuilder embedBuilder = new()
{
Title = embedTitle,
ImageUrl = url,
Color = embedColor ?? new DiscordColor("#6b73db"),
};

imageStream.Position = 0; // For image format
embedBuilder.AddField("Image Format", Image.DetectFormat(imageStream).Name, true);
embedBuilder.AddField("Image Format", imageData.Format, true);
if (url.Contains("a_"))
{
embedBuilder.AddField("Frame Count", image.Frames.Count.ToString("N0", CultureInfo.InvariantCulture), true);
embedBuilder.AddField("Frame Count", imageData.FrameCount.ToString("N0", CultureInfo.InvariantCulture), true);
}

embedBuilder.AddField("File Size", imageStream.Length.Bytes().Humanize(CultureInfo.InvariantCulture), true);
embedBuilder.AddField("Image Resolution", image.Metadata.ResolutionUnits switch
{
PixelResolutionUnit.PixelsPerCentimeter => $"{image.Metadata.HorizontalResolution} x {image.Metadata.VerticalResolution} cm",
PixelResolutionUnit.PixelsPerInch => $"{image.Metadata.HorizontalResolution / 0.254:#.###} x {image.Metadata.VerticalResolution / 0.254:#.###} cm",
PixelResolutionUnit.PixelsPerMeter => $"{image.Metadata.HorizontalResolution / 1000:#.###} x {image.Metadata.VerticalResolution / 1000:#.###} cm",
_ => $"{image.Metadata.HorizontalResolution} x {image.Metadata.VerticalResolution} {image.Metadata.ResolutionUnits.Humanize()}"
});

embedBuilder.AddField("Image Dimensions (Size)", $"{image.Width} x {image.Height} pixels.", false);
await context.EditResponseAsync(new DiscordMessageBuilder().WithEmbed(embedBuilder));
embedBuilder.AddField("File Size", imageData.FileSize, true);
embedBuilder.AddField("Image Resolution", imageData.Resolution, false);
embedBuilder.AddField("Image Dimensions (Size)", imageData.Dimensions, false);
await context.RespondAsync(new DiscordMessageBuilder().WithEmbed(embedBuilder));
}
}
}
2 changes: 0 additions & 2 deletions src/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
using Microsoft.Extensions.Logging;
using OoLunar.Tomoe.Events;
using OoLunar.Tomoe.Services;
using OoLunar.Tomoe.Services.Pagination;
using Serilog;
using Serilog.Events;
using Serilog.Sinks.SystemConsole.Themes;
Expand Down Expand Up @@ -138,7 +137,6 @@ public static async Task Main(string[] args)

serviceCollection.AddSingleton(new HttpClient() { DefaultRequestHeaders = { { "User-Agent", $"OoLunar.Tomoe/{currentAssembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()!.InformationalVersion} Github" } } });
serviceCollection.AddSingleton<ImageUtilitiesService>();
serviceCollection.AddSingleton(typeof(PaginatorService));
serviceCollection.AddSingleton((serviceProvider) =>
{
DiscordEventManager eventManager = new(serviceProvider);
Expand Down
10 changes: 0 additions & 10 deletions src/Services/CachedDatabaseTable`1.cs

This file was deleted.

28 changes: 17 additions & 11 deletions src/Services/ImageUtilitiesService.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System;
using System.Globalization;
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;
Expand All @@ -9,12 +11,16 @@ namespace OoLunar.Tomoe.Services
{
public sealed class ImageUtilitiesService(HttpClient httpClient)
{
private readonly HttpClient _httpClient = httpClient;

public async Task<ImageData> GetImageDataAsync(string url)
public async ValueTask<ImageData?> GetImageDataAsync(string url)
{
using Stream imageData = await (await _httpClient.GetAsync(url)).Content.ReadAsStreamAsync();
return new ImageData(imageData);
using HttpResponseMessage response = await httpClient.GetAsync(url);
if (!response.IsSuccessStatusCode)
{
return null;
}

using Stream imageDataStream = await response.Content.ReadAsStreamAsync();
return new ImageData(imageDataStream);
}
}

Expand All @@ -29,20 +35,20 @@ public sealed class ImageData

public ImageData(Stream imageData)
{
Format = Image.DetectFormat(imageData).Name;
imageData.Position = 0;

Image = Image.Load(imageData);

imageData.Position = 0;
Format = Image.DetectFormat(imageData).Name;
Dimensions = $"{Image.Width} x {Image.Height} pixels.";
Resolution = Image.Metadata.ResolutionUnits switch
{
PixelResolutionUnit.PixelsPerCentimeter => $"{Image.Metadata.HorizontalResolution} x {Image.Metadata.VerticalResolution} cm",
PixelResolutionUnit.PixelsPerInch => $"{Image.Metadata.HorizontalResolution / 0.254:#.###} x {Image.Metadata.VerticalResolution / 0.254:#.###} cm",
PixelResolutionUnit.PixelsPerMeter => $"{Image.Metadata.HorizontalResolution / 1000:#.###} x {Image.Metadata.VerticalResolution / 1000:#.###} cm",
PixelResolutionUnit.PixelsPerInch => $"{Math.Round(Image.Metadata.HorizontalResolution / 0.254, MidpointRounding.AwayFromZero)} x {Math.Round(Image.Metadata.VerticalResolution / 0.254, MidpointRounding.AwayFromZero)} cm",
PixelResolutionUnit.PixelsPerMeter => $"{Math.Round(Image.Metadata.HorizontalResolution / 1000, MidpointRounding.AwayFromZero)} x {Math.Round(Image.Metadata.VerticalResolution / 1000, MidpointRounding.AwayFromZero)} cm",
_ => $"{Image.Metadata.HorizontalResolution} x {Image.Metadata.VerticalResolution} {Image.Metadata.ResolutionUnits.Humanize()}"
};

FileSize = imageData.Length.Bytes().Humanize();
FileSize = imageData.Length.Bytes().Humanize(CultureInfo.InvariantCulture);
}
}
}
46 changes: 0 additions & 46 deletions src/Services/Pagination/Page.cs

This file was deleted.

49 changes: 0 additions & 49 deletions src/Services/Pagination/PageBuilder.cs

This file was deleted.

Loading

0 comments on commit fc09a65

Please sign in to comment.