This commit is contained in:
Chris Chen
2025-11-02 17:05:06 -08:00
parent f8ffd3a7fa
commit afb9b91dbb
4487 changed files with 775 additions and 483943 deletions
-47
View File
@@ -1,47 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Church.Net.Utility
{
public static class DateTimeHelper
{
private static TimeZoneInfo pacificZone = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
public static DateTime GetNextWeekday(DateTime start, DayOfWeek day)
{
// The (... + 7) % 7 ensures we end up with a value in the range [0, 6]
int daysToAdd = ((int)day - (int)start.DayOfWeek + 7) % 7;
return start.AddDays(daysToAdd);
}
public static DateTime Now(string timeZoneCode=null)
{
TimeZoneInfo timeZone;
if (null== timeZoneCode)
{
timeZone = pacificZone;
}
else
{
timeZone = TimeZoneInfo.FindSystemTimeZoneById(timeZoneCode);
}
return TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, pacificZone);
}
public static DateTime Today(string timeZoneCode = null)
{
return Now(timeZoneCode).Date;
}
public static DateTime ToUtc(this DateTime time,string timeZoneCode)
{
TimeZoneInfo timeZone = TimeZoneInfo.FindSystemTimeZoneById(timeZoneCode);
return TimeZoneInfo.ConvertTimeToUtc(time, timeZone);
}
public static DateTime ToLocal(this DateTime time, string timeZoneCode)
{
TimeZoneInfo timeZone = TimeZoneInfo.FindSystemTimeZoneById(timeZoneCode);
return TimeZoneInfo.ConvertTimeFromUtc(time, timeZone);
}
}
}
-70
View File
@@ -1,70 +0,0 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
namespace Church.Net.Utility
{
public static class EnumHelper
{
public static string EnumToDescriptionString(this Enum val)
{
//pull out first value in case of flag enumeration
var firstValue = val.ToString().Split(',').Select(s => s.Trim()).First();
IEnumerable<DescriptionAttribute> attributes = val
.GetType()
.GetField(firstValue)
.GetCustomAttributes<DescriptionAttribute>(false);
return true == attributes?.Any() ? attributes.First().Description : val.ToString();
}
public static T GetEnumValueFromDescription<T>(string description) where T : struct
{
object value = GetEnumValueFromDescription(typeof(T), description);
return value is null ? default : (T)value;
}
public static object GetEnumValueFromDescription(Type type, string description)
{
if (type is null) return null;
if (!type.IsEnum)
{
if (type.IsNullableEnum())
{
type = Nullable.GetUnderlyingType(type);
}
else
{
throw new ArgumentException();
}
}
FieldInfo field = type.GetFields()
.FirstOrDefault(f => f.GetCustomAttributes<DescriptionAttribute>(false)
.FirstOrDefault(a => a.Description == description) != null
)
;
return field == null ? null : Enum.ToObject(type, field?.GetRawConstantValue());
return field?.GetRawConstantValue();
}
public static bool IsNullableEnum(this Type t)
{
Type u = Nullable.GetUnderlyingType(t);
return (u != null) && u.IsEnum;
}
public static TEnum GetDefaultEnumValue<TEnum>() where TEnum : struct
{
return (TEnum)GetDefaultEnumValue(typeof(TEnum));
}
public static object GetDefaultEnumValue(Type type)
{
if (type is null) return null;
IEnumerable<DefaultValueAttribute> attributes = type.GetCustomAttributes<DefaultValueAttribute>(false);
return attributes?.FirstOrDefault()?.Value;
}
}
}
-408
View File
@@ -1,408 +0,0 @@
using SixLabors.ImageSharp.Processing;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace Church.Net.Utility
{
public static class Format
{
public static string Get33BaseGuid()
{
byte[] gb = Guid.NewGuid().ToByteArray();
ulong l = BitConverter.ToUInt64(gb, 0);
return Parse.ULongToBase33(l);
}
private static Random rng = new Random();
public static void Shuffle<T>(this IList<T> list)
{
int n = list.Count;
while (n > 1)
{
n--;
int k = rng.Next(n + 1);
T value = list[k];
list[k] = list[n];
list[n] = value;
}
}
}
public static class StringHelper
{
public static string Get33BaseGuid()
{
byte[] gb = Guid.NewGuid().ToByteArray();
ulong l = BitConverter.ToUInt64(gb, 0);
return Parse.ULongToBase33(l);
}
}
public static class Parse
{
#region "列舉型別宣告(empty)"
#endregion
#region "私有常數(empty)"
#endregion
#region "私有變數(empty)"
#endregion
#region "建構函式/解構函式"
#endregion
#region "介面實作(empty)"
#endregion
#region "公用方法和屬性"
/// <summary>
/// 嘗試轉換為整數型別,若轉換失敗則回傳0
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static int ToInt(string input)
{
return ToInt(input, 0);
}
/// <summary>
/// 嘗試轉換為整數型別,若轉換失敗則回傳 初始值
/// </summary>
/// <param name="input"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
public static int ToInt(string input, int defaultValue)
{
int temp = 0;
if (!int.TryParse(input, out temp))
{
temp = defaultValue;
}
return temp;
}
public static bool ToBool(string input, bool defaultValue = false)
{
bool temp = defaultValue;
if (!bool.TryParse(input, out temp))
{
temp = defaultValue;
}
return temp;
}
#region Hex
public static string ToHex(int value)
{
value = value + 10000;
// Convert integer 182 as a hex in a string variable
string hexValue = value.ToString("X");
return hexValue;
}
public static int HexToInt(string value)
{
// Convert the hex string back to the number
return int.Parse(value, System.Globalization.NumberStyles.HexNumber) - 10000;
}
#endregion
#region 33
private const string CharList = "0123456789ABCDEFGHJKLMNPQRSTUVWXY";
public static String ULongToBase33(int input)
{
return ULongToBase33((ulong)input);
}
public static String ULongToBase33(long input)
{
return ULongToBase33((ulong)input);
}
/// <summary>
///
/// </summary>
/// <param name="input"></param>
/// <param name="minLength"></param>
/// <returns></returns>
public static String ULongToBase33(ulong input)
{
if (input < 0) throw new ArgumentOutOfRangeException("input", input, "33 進位僅可計算正數");
//ulong maxValue = GetLongMax(input);
//ulong currentValue = input;
char[] clistarr = CharList.ToCharArray();
var result = new Stack<char>();
while (input != 0)
{
result.Push(clistarr[input % 33]);
input /= 33;
}
string encodeResult = new string(result.ToArray());
//if (currentValue != maxValue)
//{
// string max32Value = ULongToBase32(maxValue);
// while (encodeResult.Length < max32Value.Length)
// {
// encodeResult = "Y" + encodeResult;
// }
//}
return encodeResult;
}
/// <summary>
///
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static ulong Base33ToULong(string input)
{
ulong result = 0;
try
{
//input = input.Replace("Y", "");
var reversed = input.ToUpper().Reverse();
int pos = 0;
foreach (char c in reversed)
{
result += (ulong)CharList.IndexOf(c) * (ulong)Math.Pow(33, pos);
pos++;
}
}
catch (Exception e)
{
}
return result;
}
#endregion
#endregion
#region "內部方法和屬性(empty)"
#endregion
#region "私用方法和屬性(empty)"
#endregion
#region "Form Event(empty)"
#endregion
}
public static class EnumHelper<T>
{
public static IList<T> GetValues()
{
var enumValues = new List<T>();
foreach (FieldInfo fi in typeof(T).GetFields(BindingFlags.Static | BindingFlags.Public))
{
enumValues.Add((T)Enum.Parse(typeof(T), fi.Name, false));
}
return enumValues;
}
public static T Parse(string value)
{
return (T)Enum.Parse(typeof(T), value, true);
}
public static IList<string> GetNames()
{
return System.Enum.GetNames(typeof(T));
//return T.GetFields(BindingFlags.Static | BindingFlags.Public).Select(fi => fi.Name).ToList();
}
}
public static class ImageHelper
{
public static Bitmap CreateThumbnail(string lcFilename, int lnWidth, int lnHeight)
{
System.Drawing.Bitmap bmpOut = null;
try
{
Bitmap loBMP = new Bitmap(lcFilename);
ImageFormat loFormat = loBMP.RawFormat;
decimal lnRatio;
int lnNewWidth = 0;
int lnNewHeight = 0;
if (loBMP.Width < lnWidth && loBMP.Height < lnHeight)
return loBMP;
if (loBMP.Width > loBMP.Height)
{
lnRatio = (decimal)lnWidth / loBMP.Width;
lnNewWidth = lnWidth;
decimal lnTemp = loBMP.Height * lnRatio;
lnNewHeight = (int)lnTemp;
}
else
{
lnRatio = (decimal)lnHeight / loBMP.Height;
lnNewHeight = lnHeight;
decimal lnTemp = loBMP.Width * lnRatio;
lnNewWidth = (int)lnTemp;
}
bmpOut = new Bitmap(lnNewWidth, lnNewHeight);
Graphics g = Graphics.FromImage(bmpOut);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
g.FillRectangle(Brushes.White, 0, 0, lnNewWidth, lnNewHeight);
g.DrawImage(loBMP, 0, 0, lnNewWidth, lnNewHeight);
loBMP.Dispose();
}
catch
{
return null;
}
return bmpOut;
}
public static Bitmap SaveThumbnail(Bitmap image, string saveTo, int lnWidth, int lnHeight)
{
System.Drawing.Bitmap bmpOut = null;
try
{
Bitmap loBMP = image;
ImageFormat loFormat = loBMP.RawFormat;
decimal lnRatio;
int lnNewWidth = loBMP.Width;
int lnNewHeight = loBMP.Height;
if (loBMP.Width > lnWidth || loBMP.Height > lnHeight)
{
if (loBMP.Width > loBMP.Height)
{
lnRatio = (decimal)lnWidth / loBMP.Width;
lnNewWidth = lnWidth;
decimal lnTemp = loBMP.Height * lnRatio;
lnNewHeight = (int)lnTemp;
}
else
{
lnRatio = (decimal)lnHeight / loBMP.Height;
lnNewHeight = lnHeight;
decimal lnTemp = loBMP.Width * lnRatio;
lnNewWidth = (int)lnTemp;
}
}
bmpOut = new Bitmap(lnNewWidth, lnNewHeight);
Graphics g = Graphics.FromImage(bmpOut);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
g.FillRectangle(Brushes.White, 0, 0, lnNewWidth, lnNewHeight);
g.DrawImage(loBMP, 0, 0, lnNewWidth, lnNewHeight);
bmpOut.Save(saveTo,ImageFormat.Jpeg);
bmpOut.Dispose();
loBMP.Dispose();
}
catch
{
return null;
}
return bmpOut;
}
public static Bitmap Resize(this Bitmap bmp, int width, int height)
{
Bitmap result = new Bitmap(width, height);
using (Graphics g = Graphics.FromImage(result))
{
g.DrawImage(bmp, 0, 0, width, height);
}
return result;
}
public static Bitmap Scalling(this Bitmap bmp, int percentage)
{
return bmp.Resize(bmp.Width * percentage / 100, bmp.Height * percentage / 100);
}
public static void Scalling(this SixLabors.ImageSharp.Image image, int percentage)
{
var scale = (double)percentage/100;
var scaleWidth = (int)(image.Width * scale);
var scaleHeight = (int)(image.Height * scale);
image.Mutate(o => o.Resize(new SixLabors.ImageSharp.Size(scaleWidth, scaleHeight)));
}
public static Bitmap Superimpose(Bitmap largeBmp, Bitmap smallBmp, int? x = null, int? y = null)
{
Graphics g = Graphics.FromImage(largeBmp);
g.CompositingMode = CompositingMode.SourceOver;
smallBmp.MakeTransparent();
int margin = 5;
if (!x.HasValue)
{
x = largeBmp.Width - smallBmp.Width - margin;
}
if (!y.HasValue)
{
y = largeBmp.Height - smallBmp.Height - margin;
}
var scale = 0.8;
var scaleWidth = (int)(smallBmp.Width * scale);
var scaleHeight = (int)(smallBmp.Height * scale);
g.DrawImage(smallBmp, new Rectangle(x.Value, y.Value, scaleWidth, scaleHeight));
return largeBmp;
}
}
}
-38
View File
@@ -1,38 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Church.Net.Utility
{
public static class TokenHelper
{
public static string GenerateToken(string userId, DateTime? expiredTime=null)
{
if (expiredTime == null)
{
expiredTime = DateTime.Now.AddDays(2);
}
byte[] time = BitConverter.GetBytes(expiredTime.Value.ToBinary());
byte[] key = Encoding.ASCII.GetBytes(userId);
// byte[] expiredTimeByte = BitConverter.GetBytes(expiredTime.ToBinary());
string token = Convert.ToBase64String(time.Concat(key).ToArray());
return token;
}
public static string GetUserIdFromToken(string token)
{
byte[] data = Convert.FromBase64String(token);
DateTime when = DateTime.FromBinary(BitConverter.ToInt64(data, 0));
#if DEBUG
when = DateTime.Today.AddDays(1);
#endif
if (when >= DateTime.Now)
{
string userId = Encoding.ASCII.GetString(data.Skip(8).ToArray());
return userId;
}
return null;
}
}
}