ChatContext: 3 identische Context-Klassen (PreparerContext, ReviewContext, FinalizerContext) zu einer SubAgentContext-Klasse zusammengefasst. ChatService: 3x40 Zeilen Sub-Agent-Setup durch BuildSubAgentContext() ersetzt. AnthropicService: 5 Hilfsmethoden extrahiert – ExtractWebSearchSources(), TruncateToolResult(), AddToolsToPayload(), ParseServerToolUse(), TrackUsage(). WorkspaceEndpoint: 5 Session/Agent-Endpoint-Paare durch generische Core-Handler mit basePath-Parameter ersetzt. Ungenutzte rmdir-Endpoints entfernt. AgentEndpoint: Doppeltes Agent-DTO-Mapping durch MapAgentList() ersetzt. SessionEndpoint: Doppelte Session/Agent-Auflösung durch ResolvePromptContext() ersetzt. DatabaseService: Toten Code entfernt (SetSessionTelegramChatId, ClearSessionTelegramChatId, GetSessions, GetUserSessions). ConvertDataUrlToWebP und ResizeDataUrlToWebP zu einer Methode zusammengefasst. TelegramEndpoint: Bot-DTO-Mapping durch MapBotResponse() dedupliziert. KreaEndpoint + SkillEndpoint: Thumbnail-Generierung in ImageService.GenerateThumbnail() extrahiert, SixLabors-Imports aus Endpoints entfernt. CronjobEndpoint: 7x ctx.Items[UserId] durch ctx.GetUserId() ersetzt.
99 lines
3.3 KiB
C#
99 lines
3.3 KiB
C#
using System;
|
|
using System.IO;
|
|
using SixLabors.ImageSharp;
|
|
using SixLabors.ImageSharp.Formats.Webp;
|
|
using SixLabors.ImageSharp.Processing;
|
|
|
|
namespace Gott.Services;
|
|
|
|
public class ImageService
|
|
{
|
|
private const int MaxSideLength = 512;
|
|
private const int SmallSideLength = 256;
|
|
private const int WebPQuality = 80;
|
|
private const int WebPQualitySmall = 70;
|
|
|
|
/// <summary>
|
|
/// Nimmt eine Base64-Data-URL (z.B. "data:image/png;base64,..."), skaliert das Bild
|
|
/// auf max 512px Seitenlänge, konvertiert zu WebP und gibt eine neue Data-URL zurück.
|
|
/// Gibt null zurück wenn input null/leer ist.
|
|
/// </summary>
|
|
public string ResizeDataUrl(string dataUrl)
|
|
{
|
|
return ResizeToWebP(dataUrl, MaxSideLength, WebPQuality);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Erzeugt eine kleine Thumbnail-Variante (max 256px, Quality 70) für Dashboard-Listen.
|
|
/// Gibt null zurück wenn input null/leer ist.
|
|
/// </summary>
|
|
public string ResizeDataUrlSmall(string dataUrl)
|
|
{
|
|
return ResizeToWebP(dataUrl, SmallSideLength, WebPQualitySmall);
|
|
}
|
|
|
|
private static string ResizeToWebP(string dataUrl, int maxSide, int quality)
|
|
{
|
|
if (string.IsNullOrEmpty(dataUrl)) return null;
|
|
|
|
// Data-URL parsen: "data:<mediaType>;base64,<data>"
|
|
var commaIndex = dataUrl.IndexOf(',');
|
|
if (commaIndex < 0) return dataUrl;
|
|
|
|
var base64 = dataUrl.Substring(commaIndex + 1);
|
|
var bytes = Convert.FromBase64String(base64);
|
|
|
|
using var image = Image.Load(bytes);
|
|
|
|
// Skalieren wenn nötig
|
|
if (image.Width > maxSide || image.Height > maxSide)
|
|
{
|
|
image.Mutate(x => x.Resize(new ResizeOptions
|
|
{
|
|
Size = new Size(maxSide, maxSide),
|
|
Mode = ResizeMode.Max
|
|
}));
|
|
}
|
|
|
|
// Immer als WebP encodieren
|
|
using var ms = new MemoryStream();
|
|
image.Save(ms, new WebpEncoder { Quality = quality });
|
|
var webpBase64 = Convert.ToBase64String(ms.ToArray());
|
|
|
|
return "data:image/webp;base64," + webpBase64;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Generiert ein WebP-Thumbnail (max 320px) mit Caching.
|
|
/// Gibt den Pfad zum Thumbnail zurück, oder null bei Fehler.
|
|
/// </summary>
|
|
public static async System.Threading.Tasks.Task<string> GenerateThumbnail(string originalPath, string cacheDir, int maxSize = 320, int quality = 75)
|
|
{
|
|
var thumbFilename = Path.GetFileNameWithoutExtension(originalPath) + ".webp";
|
|
var thumbPath = Path.Combine(cacheDir, thumbFilename);
|
|
|
|
if (File.Exists(thumbPath) && File.GetLastWriteTimeUtc(originalPath) <= File.GetLastWriteTimeUtc(thumbPath))
|
|
return thumbPath;
|
|
|
|
try
|
|
{
|
|
Directory.CreateDirectory(cacheDir);
|
|
using var image = await Image.LoadAsync(originalPath);
|
|
if (image.Width > maxSize || image.Height > maxSize)
|
|
{
|
|
image.Mutate(x => x.Resize(new ResizeOptions
|
|
{
|
|
Size = new Size(maxSize, maxSize),
|
|
Mode = ResizeMode.Max
|
|
}));
|
|
}
|
|
await image.SaveAsync(thumbPath, new WebpEncoder { Quality = quality });
|
|
return thumbPath;
|
|
}
|
|
catch
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
}
|