mirror of
https://github.com/DrizzleTime/Foxel.git
synced 2026-08-07 06:43:28 +08:00
refactor: 统一命名空间为Foxel.Api,移除不必要的using语句,优化代码结构
This commit is contained in:
@@ -1,5 +1,3 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Foxel.Utils;
|
||||
|
||||
public static class AiHelper
|
||||
@@ -10,17 +8,15 @@ public static class AiHelper
|
||||
/// <param name="aiResponse">AI生成的响应文本</param>
|
||||
/// <param name="logger">日志记录器</param>
|
||||
/// <returns>包含标题和描述的元组</returns>
|
||||
public static (string title, string description) ExtractTitleAndDescription(string aiResponse, ILogger? logger = null)
|
||||
public static (string title, string description) ExtractTitleAndDescription(string aiResponse,
|
||||
ILogger? logger = null)
|
||||
{
|
||||
string title = "AI生成的标题";
|
||||
string description = "AI生成的描述";
|
||||
|
||||
try
|
||||
{
|
||||
// 尝试解析JSON响应
|
||||
if (aiResponse.Contains("{") && aiResponse.Contains("}"))
|
||||
{
|
||||
// 提取JSON部分
|
||||
int jsonStartIndex = aiResponse.IndexOf('{');
|
||||
int jsonEndIndex = aiResponse.LastIndexOf('}') + 1;
|
||||
|
||||
@@ -34,7 +30,8 @@ public static class AiHelper
|
||||
|
||||
try
|
||||
{
|
||||
var result = System.Text.Json.JsonSerializer.Deserialize<ImageAnalysisResult>(jsonPart, options);
|
||||
var result =
|
||||
System.Text.Json.JsonSerializer.Deserialize<ImageAnalysisResult>(jsonPart, options);
|
||||
if (result != null)
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(result.Title))
|
||||
@@ -93,15 +90,14 @@ public static class AiHelper
|
||||
return (title, description);
|
||||
}
|
||||
|
||||
// 用于解析JSON的类
|
||||
public class ImageAnalysisResult
|
||||
{
|
||||
public string Title { get; set; } = string.Empty;
|
||||
public string Description { get; set; } = string.Empty;
|
||||
public string Title { get; init; } = string.Empty;
|
||||
public string Description { get; init; } = string.Empty;
|
||||
}
|
||||
|
||||
public class TagsResult
|
||||
{
|
||||
public string[] Tags { get; set; } = Array.Empty<string>();
|
||||
public string[] Tags { get; init; } = [];
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,6 @@ using SixLabors.ImageSharp.Metadata.Profiles.Exif;
|
||||
using System.Globalization;
|
||||
using Foxel.Models;
|
||||
using Foxel.Models.Enums;
|
||||
using SixLabors.ImageSharp.PixelFormats;
|
||||
|
||||
namespace Foxel.Utils;
|
||||
|
||||
@@ -13,21 +12,6 @@ namespace Foxel.Utils;
|
||||
/// </summary>
|
||||
public static class ImageHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取完整URL路径
|
||||
/// </summary>
|
||||
/// <param name="serverUrl">服务器URL</param>
|
||||
/// <param name="relativePath">相对路径</param>
|
||||
/// <returns>完整URL路径</returns>
|
||||
public static string GetFullPath(string serverUrl, string relativePath)
|
||||
{
|
||||
if (string.IsNullOrEmpty(relativePath))
|
||||
return string.Empty;
|
||||
if (relativePath.StartsWith("https://"))
|
||||
return relativePath;
|
||||
return $"{serverUrl.TrimEnd('/')}{relativePath}";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建缩略图
|
||||
/// </summary>
|
||||
@@ -76,58 +60,6 @@ public static class ImageHelper
|
||||
return thumbnailFileInfo.Length;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 检查图像是否包含透明像素
|
||||
/// </summary>
|
||||
/// <param name="image">要检查的图像</param>
|
||||
/// <returns>如果图像包含透明像素则返回true</returns>
|
||||
private static bool HasTransparency(Image image)
|
||||
{
|
||||
// 检查图像格式是否支持透明度
|
||||
if (image.PixelType.AlphaRepresentation == PixelAlphaRepresentation.None)
|
||||
{
|
||||
return false; // 图像格式不支持透明度
|
||||
}
|
||||
|
||||
// 对于小图片,逐像素检查是否有透明度
|
||||
if (image.Width * image.Height <= 1000 * 1000) // 对于不超过1000x1000的图片
|
||||
{
|
||||
using var imageWithAlpha = image.CloneAs<Rgba32>();
|
||||
|
||||
for (int y = 0; y < imageWithAlpha.Height; y++)
|
||||
{
|
||||
for (int x = 0; x < imageWithAlpha.Width; x++)
|
||||
{
|
||||
if (imageWithAlpha[x, y].A < 255)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
using var imageWithAlpha = image.CloneAs<Rgba32>();
|
||||
int sampleSize = Math.Max(image.Width, image.Height) / 100;
|
||||
sampleSize = Math.Max(1, sampleSize);
|
||||
|
||||
for (int y = 0; y < imageWithAlpha.Height; y += sampleSize)
|
||||
{
|
||||
for (int x = 0; x < imageWithAlpha.Width; x += sampleSize)
|
||||
{
|
||||
if (imageWithAlpha[x, y].A < 255)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据原始文件大小调整质量参数
|
||||
/// </summary>
|
||||
|
||||
Reference in New Issue
Block a user