1
This commit is contained in:
11
Yes.Domain/Core/Extensions/ArticleSlugExtensions.cs
Normal file
11
Yes.Domain/Core/Extensions/ArticleSlugExtensions.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
namespace Yes.Domain.Core.Extensions
|
||||
{
|
||||
public static class ArticleSlugExtensions
|
||||
{
|
||||
|
||||
public static bool IsSlug(this string input)
|
||||
{
|
||||
return Regex.IsMatch(input, @"^[a-zA-Z0-9-_]+$");
|
||||
}
|
||||
}
|
||||
}
|
||||
52
Yes.Domain/Core/Extensions/ArticleSummaryExtensions.cs
Normal file
52
Yes.Domain/Core/Extensions/ArticleSummaryExtensions.cs
Normal file
@@ -0,0 +1,52 @@
|
||||
|
||||
|
||||
namespace Yes.Domain.Core.Extensions
|
||||
{
|
||||
public static class ArticleSummaryExtensions
|
||||
{
|
||||
|
||||
public static string ToSummary(this string content)
|
||||
{
|
||||
// 去除 HTML 标签
|
||||
string text = RemoveHtmlTags(content);
|
||||
// 去除 Markdown 标签(简单处理,可能不全面)
|
||||
text = Regex.Replace(text, @"\!\[.*?\]\(.*?\)|\[.*?\]\(.*?\)|\*\*.*?\*\*|\*.*?\*|~~.*?~~", "");
|
||||
|
||||
|
||||
int endIndex = GetEndIndex(text, 50, 100);
|
||||
if (endIndex < 50)
|
||||
{
|
||||
return text;
|
||||
}
|
||||
else
|
||||
{
|
||||
return text[..(endIndex + 1)];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static string RemoveHtmlTags(string html)
|
||||
{
|
||||
return Regex.Replace(html, @"<[^>]*>", "");
|
||||
}
|
||||
|
||||
private static int GetEndIndex(string text, int start, int end)
|
||||
{
|
||||
if (text.Length < start)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
end = Math.Min(end, text.Length - 1);
|
||||
|
||||
for (int i = end; i >= start; i--)
|
||||
{
|
||||
if (".。!?”".Contains(text[i]))
|
||||
{
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return end;
|
||||
}
|
||||
}
|
||||
}
|
||||
25
Yes.Domain/Core/Extensions/ArticleTagExtensions.cs
Normal file
25
Yes.Domain/Core/Extensions/ArticleTagExtensions.cs
Normal file
@@ -0,0 +1,25 @@
|
||||
namespace Yes.Domain.Core.Extensions
|
||||
{
|
||||
public static class ArticleTagExtensions
|
||||
{
|
||||
|
||||
|
||||
public static string ToTag(this string[] tags)
|
||||
{
|
||||
return string.Join(",", tags);
|
||||
}
|
||||
|
||||
public static string ToTag(this List<string> tags)
|
||||
{
|
||||
return string.Join(",", tags);
|
||||
}
|
||||
|
||||
public static List<string> ToTags(this string tags)
|
||||
{
|
||||
if (string.IsNullOrEmpty(tags))
|
||||
return new List<string>();
|
||||
|
||||
return tags.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList();
|
||||
}
|
||||
}
|
||||
}
|
||||
15
Yes.Domain/Core/Extensions/DateTimeExtensions.cs
Normal file
15
Yes.Domain/Core/Extensions/DateTimeExtensions.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
namespace Yes.Domain.Core.Extensions
|
||||
{
|
||||
public static class DateTimeExtensions
|
||||
{
|
||||
public static DateTime ToSafeTime(this DateTime dateTime)
|
||||
{
|
||||
var minDateTime = DateTime.Parse("2000-1-1");
|
||||
if (dateTime.Subtract(minDateTime).TotalMinutes < 0)
|
||||
{
|
||||
return minDateTime;
|
||||
}
|
||||
return dateTime;
|
||||
}
|
||||
}
|
||||
}
|
||||
21
Yes.Domain/Core/Extensions/HttpContextExtensions.cs
Normal file
21
Yes.Domain/Core/Extensions/HttpContextExtensions.cs
Normal file
@@ -0,0 +1,21 @@
|
||||
|
||||
namespace Yes.Domain.Core.Extensions
|
||||
{
|
||||
public static class HttpContextExtensions
|
||||
{
|
||||
public static string GetIpAddress(this HttpContext httpContext)
|
||||
{
|
||||
var ipAddress = httpContext.Connection.RemoteIpAddress?.ToString();
|
||||
// 如果使用了代理服务器,可能需要从X-Forwarded-For头获取真实的IP地址
|
||||
if (ipAddress == null || ipAddress == "::1") // "::1" 是IPv6的localhost
|
||||
{
|
||||
var xForwardedForHeader = httpContext.Request.Headers["X-Forwarded-For"].FirstOrDefault();
|
||||
if (!string.IsNullOrEmpty(xForwardedForHeader))
|
||||
{
|
||||
ipAddress = xForwardedForHeader.Split(",").FirstOrDefault()?.Trim() ?? "";
|
||||
}
|
||||
}
|
||||
return ipAddress ?? "";
|
||||
}
|
||||
}
|
||||
}
|
||||
39
Yes.Domain/Core/Extensions/NameValueCollectionExtensions.cs
Normal file
39
Yes.Domain/Core/Extensions/NameValueCollectionExtensions.cs
Normal file
@@ -0,0 +1,39 @@
|
||||
|
||||
|
||||
namespace Yes.Domain.Core.Extensions
|
||||
{
|
||||
public static class NameValueCollectionExtensions
|
||||
{
|
||||
public static int QueryInt(this NameValueCollection collection, string key, int defaultValue = 0)
|
||||
{
|
||||
key = key.ToUpper();
|
||||
var value = collection.QueryString(key);
|
||||
if (string.IsNullOrEmpty(value))
|
||||
{
|
||||
return defaultValue;
|
||||
}
|
||||
if (int.TryParse(value, out int result))
|
||||
{
|
||||
return result;
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
public static string QueryString(this NameValueCollection collection, string key)
|
||||
{
|
||||
key = key.ToUpper();
|
||||
if (collection.AllKeys.Contains(key))
|
||||
{
|
||||
try
|
||||
{
|
||||
return collection[key]?.ToString() ?? "";
|
||||
}
|
||||
catch
|
||||
{
|
||||
return "";
|
||||
}
|
||||
}
|
||||
return "";
|
||||
}
|
||||
}
|
||||
}
|
||||
20
Yes.Domain/Core/Extensions/PasswordExtensions.cs
Normal file
20
Yes.Domain/Core/Extensions/PasswordExtensions.cs
Normal file
@@ -0,0 +1,20 @@
|
||||
namespace Yes.Domain.Core.Extensions
|
||||
{
|
||||
public static class PasswordExtensions
|
||||
{
|
||||
public static string ToMd5(this string input)
|
||||
{
|
||||
if (string.IsNullOrEmpty(input))
|
||||
{
|
||||
return "";
|
||||
}
|
||||
using (var md5 = MD5.Create())
|
||||
{
|
||||
var result = md5.ComputeHash(Encoding.UTF8.GetBytes(input));
|
||||
var strResult = BitConverter.ToString(result);
|
||||
return strResult.Replace("-", "");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
13
Yes.Domain/Core/Extensions/StringExtensions.cs
Normal file
13
Yes.Domain/Core/Extensions/StringExtensions.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
namespace Yes.Domain.Core.Extensions
|
||||
{
|
||||
public static class StringExtensions
|
||||
{
|
||||
public static bool IsUrlValid(this string url)
|
||||
{
|
||||
var urlRegex = new Regex(@"^(https?|http)://[^\s/$.?#].[^\s]*$", RegexOptions.IgnoreCase);
|
||||
return !string.IsNullOrEmpty(url) && urlRegex.IsMatch(url);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user