网站首页 网站源码
website
站点相关全部源代码,隐藏了一些关于服务器的信息
using System;
using System.Linq;
using System.Security.Claims;
using System.Text.Json;
using System.Threading.Tasks;
using Dpz.Core.EnumLibrary;
using Dpz.Core.Public.ViewModel;
using Dpz.Core.Public.ViewModel.V4;
using Dpz.Core.Service.ObjectStorage.Services;
using Dpz.Core.Web.Pager;
using Dpz.Core.WebApi.Middleware;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;

namespace Dpz.Core.WebApi
{
    /// <summary>
    /// Tools Extensions
    /// </summary>
    public static class ToolsExtensions
    {
        /// <summary>
        /// 根据Identity反射获取当前用户信息
        /// 未授权用户返回 null
        /// </summary>
        /// <param name="principal"></param>
        /// <returns></returns>
        public static VmUserInfo GetIdentity(this ClaimsPrincipal principal)
        {
            if (principal.Identity?.IsAuthenticated == true)
            {
                var userInfo = new VmUserInfo();
                foreach (var claims in principal.Claims)
                {
                    var property = typeof(VmUserInfo).GetProperty(claims.Type);
                    if (property == null) continue;
                    if (property.PropertyType == typeof(DateTime?))
                    {
                        property.SetValue(userInfo, DateTime.Parse(claims.Value));
                    }
                    else if (property.PropertyType == typeof(Sex))
                    {
                        var sex = Enum.Parse(typeof(Sex), claims.Value);
                        property.SetValue(userInfo, sex);
                    }
                    else if (property.PropertyType == typeof(bool?))
                    {
                        bool.TryParse(claims.Value, out var result);
                        property.SetValue(userInfo, result);
                    }
                    else if (property.PropertyType == typeof(Permissions?))
                    {
                        if (Enum.TryParse(claims.Value, out Permissions permissions))
                        {
                            property.SetValue(userInfo, permissions);
                        }
                    }
                    else
                    {
                        typeof(VmUserInfo).GetProperty(claims.Type)?.SetValue(userInfo, claims.Value);
                    }
                }

                return userInfo;
            }

            return null;
        }

        /// <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);
        }

        /// <summary>
        /// 使用Response header中间件
        /// </summary>
        /// <param name="builder"></param>
        /// <returns></returns>
        public static IApplicationBuilder UseResponseHeaders(
            this IApplicationBuilder builder)
        {
            return builder.UseMiddleware<HttpResponseHeaderHandel>();
        }

        /// <summary>
        /// 图像类型
        /// </summary>
        public static string[] PictureTypes =>
            typeof(PictureType).GetFields()
                .Where(x => x.IsPublic && x.IsStatic)
                .Select(x => x.Name)
                .ToArray();
        
        /// <summary>
        /// 获取IP地址
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public static string GetIpAddress(this HttpRequest request)
        {
            if (request == null) return "";
#if DEBUG
            return "113.110.235.52";
#endif
            var xff = request.Headers["X-Forwarded-For"];
            return string.IsNullOrEmpty(xff)
                ? request.HttpContext.Connection.RemoteIpAddress.ToString()
                : xff.ToString();
        }
        
        /// <summary>
        /// 
        /// </summary>
        /// <param name="objectStorageService"></param>
        /// <param name="music"></param>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="ArgumentException"></exception>
        public static async Task DeleteMusicAsync(this IObjectStorageOperation objectStorageService,VmMusic music)
        {
            if (music == null)
                throw new ArgumentNullException(nameof(music));
            if (string.IsNullOrEmpty(music.MusicUrl))
                throw new ArgumentException("property MusicUrl is empty or null", nameof(music.MusicUrl));


            await objectStorageService.DeleteAsync(music.MusicUrl);

            if (!string.IsNullOrEmpty(music.CoverUrl))
                await objectStorageService.DeleteAsync(music.CoverUrl);

            if (!string.IsNullOrEmpty(music.LyricUrl))
                await objectStorageService.DeleteAsync(music.LyricUrl);
        }
    }
}
loading