using Dpz.Core.Public.ViewModel.Response;
using Dpz.Core.WebApi.MessagePackFormatters;
using MessagePack.Resolvers;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options;
namespace Dpz.Core.WebApi;
/// <summary>
/// Tools Extensions
/// </summary>
public static class ToolsExtensions
{
/// <summary>
/// 生成翻页元数据
/// </summary>
/// <param name="pagedList"></param>
/// <param name="header"></param>
/// <returns></returns>
public static void AddPaginationMetadata(this IPagedList pagedList, IHeaderDictionary header)
{
var paginationMetadata = new
{
currentPage = pagedList.CurrentPageIndex,
totalCount = pagedList.TotalItemCount,
pageSize = pagedList.PageSize,
totalPages = pagedList.TotalPageCount,
startItemIndex = pagedList.StartItemIndex,
endItemIndex = pagedList.EndItemIndex,
};
var json = JsonSerializer.Serialize(paginationMetadata);
header.Append("X-Pagination", json);
}
/// <param name="builder"></param>
extension(IApplicationBuilder builder)
{
/// <summary>
/// 使用Response header中间件
/// </summary>
/// <returns></returns>
public IApplicationBuilder UseResponseHeaders()
{
return builder.UseMiddleware<HttpResponseHeaderHandel>();
}
/// <summary>
/// 请求记录中间件
/// </summary>
/// <returns></returns>
public IApplicationBuilder UseRequestRecord()
{
return builder.UseMiddleware<HttpRequestRecord>();
}
/// <summary>
/// 日期格式化中间件
/// </summary>
/// <returns></returns>
public IApplicationBuilder UseDateTimeLocalFormat()
{
return builder.UseMiddleware<DateTimeFormattingMiddleware>();
}
}
/// <summary>
/// 删除音乐文件
/// </summary>
/// <param name="objectStorageService"></param>
/// <param name="musicResponse"></param>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="ArgumentException"></exception>
public static async Task DeleteMusicAsync(
this IObjectStorageOperation objectStorageService,
MusicResponse musicResponse
)
{
if (musicResponse == null)
{
throw new ArgumentNullException(nameof(musicResponse));
}
if (string.IsNullOrEmpty(musicResponse.MusicUrl))
{
throw new ArgumentException(
"property MusicUrl is empty or null",
nameof(musicResponse.MusicUrl)
);
}
await objectStorageService.DeleteAsync(musicResponse.MusicUrl);
if (!string.IsNullOrEmpty(musicResponse.CoverUrl))
{
await objectStorageService.DeleteAsync(musicResponse.CoverUrl);
}
if (!string.IsNullOrEmpty(musicResponse.LyricUrl))
{
await objectStorageService.DeleteAsync(musicResponse.LyricUrl);
}
}
/// <summary>
/// 添加MessagePack序列化
/// </summary>
/// <param name="builder"></param>
/// <param name="setup"></param>
/// <returns></returns>
public static IMvcBuilder AddMessagePackSerializerFormatters(
this IMvcBuilder builder,
Action<MessagePackFormatterOptions>? setup = null
)
{
builder.Services.Configure<MessagePackFormatterOptions>(o =>
{
o.FormatterResolver ??= ContractlessStandardResolver.Instance;
setup?.Invoke(o);
});
builder.Services.TryAddEnumerable(
ServiceDescriptor.Transient<
IConfigureOptions<MvcOptions>,
MessagePackFormatterMvcOptionsSetup
>()
);
return builder;
}
}
⚠⚠ 以下内容为AI分析的结果,请根据实际情况进行判断。
这是一个名为 ToolsExtensions 的静态扩展类,提供了多种实用的扩展方法。让我逐个解释每个方法的功能:
1. 分页元数据扩展方法
public static void AddPaginationMetadata(this IPagedList pagedList, IHeaderDictionary header)
功能:为分页数据生成元数据并添加到HTTP响应头中
- 从
IPagedList对象提取分页信息(当前页、总数、页大小等) - 将这些信息序列化为JSON格式
- 添加到HTTP响应头的 "X-Pagination" 字段中
- 前端可以通过这个头信息了解分页状态
2. 中间件扩展方法组
extension(IApplicationBuilder builder)
注意:这里的语法有问题,应该是 public static class Extensions 或者直接在类中定义。
包含三个中间件注册方法:
UseResponseHeaders():注册HTTP响应头处理中间件UseRequestRecord():注册请求记录中间件,用于记录HTTP请求信息UseDateTimeLocalFormat():注册日期时间格式化中间件,处理日期时间的本地化格式
3. 音乐文件删除扩展方法
public static async Task DeleteMusicAsync(this IObjectStorageOperation objectStorageService, MusicResponse musicResponse)
功能:批量删除音乐相关文件
- 验证输入参数的有效性
- 删除音乐文件(必需)
- 删除封面图片(可选)
- 删除歌词文件(可选)
- 使用对象存储服务进行异步删除操作
4. MessagePack序列化配置扩展方法
public static IMvcBuilder AddMessagePackSerializerFormatters(this IMvcBuilder builder, Action<MessagePackFormatterOptions>? setup = null)
功能:为MVC添加MessagePack序列化支持
- 配置MessagePack格式化选项
- 默认使用
ContractlessStandardResolver解析器 - 允许通过可选的
setup参数进行自定义配置 - 注册MessagePack格式化器到MVC选项中
总体特点
- 扩展方法模式:所有方法都是扩展方法,增强了现有类型的功能
- 异步支持:音乐删除方法支持异步操作
- 参数验证:包含适当的空值检查和参数验证
- 配置灵活性:MessagePack配置支持自定义选项
- Web API友好:专门为Web API场景设计,包含HTTP头处理、中间件注册等功能
这个工具类主要用于Web API项目,提供了分页、文件管理、序列化和中间件等常用功能的便捷扩展。
评论加载中...