From 61d8ad831e2231a9fb611b8b6f371a9e62ed25f4 Mon Sep 17 00:00:00 2001 From: Hadi Khazaee Asl Date: Fri, 13 Nov 2026 13:48:10 +0330 Subject: [PATCH] last ... --- Extensions/CommonExtensions.cs | 93 ++++++++++++++++++++++++++++++++ Extensions/XCommonsExtensions.cs | 67 ----------------------- Helpers/XDateHelper.cs | 13 ----- 3 files changed, 93 insertions(+), 80 deletions(-) delete mode 100644 Extensions/XCommonsExtensions.cs delete mode 100644 Helpers/XDateHelper.cs diff --git a/Extensions/CommonExtensions.cs b/Extensions/CommonExtensions.cs index b11a300..43c3963 100644 --- a/Extensions/CommonExtensions.cs +++ b/Extensions/CommonExtensions.cs @@ -21,6 +21,7 @@ using System.Security.Cryptography; using Newtonsoft.Json.Serialization; using System.Collections.ObjectModel; using System.Text.RegularExpressions; +using xCommons.Configurations; namespace xCommons.Extensions { @@ -1820,6 +1821,65 @@ namespace xCommons.Extensions return result; } + /// + /// Extract RateLimit Rule based on given Path ... + /// + /// + /// + /// + public static XRateLimitRule GetMatchingRule( + this XRateLimitConfiguration source, + string path + ) + { + // + if (!source.Enabled) + { + return null; + } + + // + var normalizedPath = path.IsNullOrEmpty() + ? "/" + : path.ToNormalString(); + + // + var rule = source.Rules.IsNull() + ? null + : source.Rules + .Where(r => + !r.IsNull() && + r.Enabled && + !r.PathPrefix.IsNullOrEmpty() && + normalizedPath.StartsWith(r.PathPrefix.ToNormalString())) + .OrderByDescending(r => r.PathPrefix.Length) + .FirstOrDefault(); + + // + if (!rule.IsNull()) + { + return rule; + } + + // + if (source.ApplyToAllRoutes) + { + // + return new XRateLimitRule + { + Enabled = true, + Name = "Default", + PathPrefix = "/", + Limit = source.DefaultLimit, + KeyMode = source.DefaultKeyMode, + PeriodSeconds = source.DefaultPeriodSeconds, + }; + } + + // + return null; + } + // #region Int Id Extensions ... public static bool IsValidIntId(this int source) @@ -1925,6 +1985,39 @@ namespace xCommons.Extensions return Convert.ToBase64String(hash); } } + + /// + /// Apply Templates on provided String ... + /// + /// + /// + /// + public static string ApplyTemplates( + this string source, + IDictionary templates = null + ) + { + // + // Validate ... + if (source.IsNullOrEmpty() || + !templates.HasChild()) + { + return source; + } + + // + var existsTemplates = templates.Where( + x => source.Contains(x.Key) + ); + var result = source; + foreach (var template in existsTemplates) + { + result = result.Replace(template.Key, template.Value); + } + + // + return result; + } #endregion // diff --git a/Extensions/XCommonsExtensions.cs b/Extensions/XCommonsExtensions.cs deleted file mode 100644 index 1e0c531..0000000 --- a/Extensions/XCommonsExtensions.cs +++ /dev/null @@ -1,67 +0,0 @@ -using System.Linq; -using xCommons.Configurations; - -namespace xCommons.Extensions -{ - public static class XCommonsExtensions - { - /// - /// Extract RateLimit Rule based on given Path ... - /// - /// - /// - /// - public static XRateLimitRule GetMatchingRule( - this XRateLimitConfiguration source, - string path - ) - { - // - if (!source.Enabled) - { - return null; - } - - // - var normalizedPath = path.IsNullOrEmpty() - ? "/" - : path.ToNormalString(); - - // - var rule = source.Rules.IsNull() - ? null - : source.Rules - .Where(r => - !r.IsNull() && - r.Enabled && - !r.PathPrefix.IsNullOrEmpty() && - normalizedPath.StartsWith(r.PathPrefix.ToNormalString())) - .OrderByDescending(r => r.PathPrefix.Length) - .FirstOrDefault(); - - // - if (!rule.IsNull()) - { - return rule; - } - - // - if (source.ApplyToAllRoutes) - { - // - return new XRateLimitRule - { - Enabled = true, - Name = "Default", - PathPrefix = "/", - Limit = source.DefaultLimit, - KeyMode = source.DefaultKeyMode, - PeriodSeconds = source.DefaultPeriodSeconds, - }; - } - - // - return null; - } - } -} \ No newline at end of file diff --git a/Helpers/XDateHelper.cs b/Helpers/XDateHelper.cs deleted file mode 100644 index b6b2bf1..0000000 --- a/Helpers/XDateHelper.cs +++ /dev/null @@ -1,13 +0,0 @@ -using System; - -namespace xCommons.Helpers { - public partial class XDateHelper { - /// - /// retrieve utc now timestam unix like representation ... - /// - /// - public static long UtcNowTimespan () { - return DateTimeOffset.UtcNow.ToUnixTimeMilliseconds (); - } - } -} \ No newline at end of file