| | | 1 | | using OtpNet; |
| | | 2 | | using QRCoder; |
| | | 3 | | using System.Text; |
| | | 4 | | using Newtonsoft.Json; |
| | | 5 | | using System.Reflection; |
| | | 6 | | using System.Globalization; |
| | | 7 | | using Newtonsoft.Json.Linq; |
| | | 8 | | using Newtonsoft.Json.Converters; |
| | | 9 | | using System.Text.RegularExpressions; |
| | | 10 | | using Microsoft.Extensions.Primitives; |
| | | 11 | | using Microsoft.AspNetCore.WebUtilities; |
| | | 12 | | |
| | | 13 | | namespace Syki.Shared; |
| | | 14 | | |
| | | 15 | | public static class StringExtensions |
| | | 16 | | { |
| | | 17 | | public static bool IsEmpty(this string? text) |
| | | 18 | | { |
| | 2432 | 19 | | return string.IsNullOrEmpty(text) || string.IsNullOrWhiteSpace(text); |
| | | 20 | | } |
| | | 21 | | |
| | | 22 | | public static bool IsIn(this string? text, params string[] others) |
| | | 23 | | { |
| | 70 | 24 | | if (text.IsEmpty()) |
| | 6 | 25 | | return true; |
| | | 26 | | |
| | 300 | 27 | | foreach (var other in others) |
| | | 28 | | { |
| | 102 | 29 | | if (other.Contains(text!, StringComparison.OrdinalIgnoreCase)) |
| | 32 | 30 | | return true; |
| | | 31 | | } |
| | | 32 | | |
| | 32 | 33 | | return false; |
| | | 34 | | } |
| | | 35 | | |
| | | 36 | | public static bool HasValue(this string? text) |
| | | 37 | | { |
| | 13506 | 38 | | return !string.IsNullOrEmpty(text); |
| | | 39 | | } |
| | | 40 | | |
| | | 41 | | public static bool HasValue(this StringValues text) |
| | | 42 | | { |
| | 0 | 43 | | return !string.IsNullOrEmpty(text); |
| | | 44 | | } |
| | | 45 | | |
| | | 46 | | public static string OnlyNumbers(this string text) |
| | | 47 | | { |
| | 2408 | 48 | | if (text.HasValue()) |
| | | 49 | | { |
| | 2390 | 50 | | return new string(text.Where(char.IsDigit).ToArray()); |
| | | 51 | | } |
| | | 52 | | |
| | 18 | 53 | | return ""; |
| | | 54 | | } |
| | | 55 | | |
| | | 56 | | public static string ToSnakeCase(this string input) |
| | | 57 | | { |
| | 232 | 58 | | if (input.IsEmpty()) { return ""; } |
| | | 59 | | |
| | 220 | 60 | | var startUnderscores = Regex.Match(input, @"^_+"); |
| | 220 | 61 | | return startUnderscores + Regex.Replace(input, @"([a-z0-9])([A-Z])", "$1_$2").ToLower(); |
| | | 62 | | } |
| | | 63 | | |
| | | 64 | | public static string GenerateTOTP(this string key) |
| | | 65 | | { |
| | 10 | 66 | | var totp = new Totp(Base32Encoding.ToBytes(key)); |
| | 10 | 67 | | return totp.ComputeTotp(); |
| | | 68 | | } |
| | | 69 | | |
| | | 70 | | public static string ToBase64(this string value) |
| | | 71 | | { |
| | 0 | 72 | | var bytes = System.Text.Encoding.UTF8.GetBytes(value); |
| | 0 | 73 | | return Convert.ToBase64String(bytes); |
| | | 74 | | } |
| | | 75 | | |
| | | 76 | | public static string Format(this decimal value) |
| | | 77 | | { |
| | 10 | 78 | | return value.ToString("0.00", CultureInfo.InvariantCulture); |
| | | 79 | | } |
| | | 80 | | |
| | | 81 | | public static string Format(this int value) |
| | | 82 | | { |
| | 0 | 83 | | return value.ToString("N0", CultureInfo.CreateSpecificCulture("pt-BR")); |
| | | 84 | | } |
| | | 85 | | |
| | | 86 | | public static bool IsValidEmail(this string email) |
| | | 87 | | { |
| | 2036 | 88 | | if (email.IsEmpty()) return false; |
| | 2016 | 89 | | return Regex.IsMatch(email, @"\A(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](? |
| | | 90 | | } |
| | | 91 | | |
| | 4 | 92 | | private static JsonSerializerSettings _settings = new() |
| | 4 | 93 | | { |
| | 4 | 94 | | Converters = [new StringEnumConverter()], |
| | 4 | 95 | | }; |
| | | 96 | | |
| | | 97 | | public static string Serialize(this object obj) |
| | | 98 | | { |
| | 5864 | 99 | | return JsonConvert.SerializeObject(obj, _settings); |
| | | 100 | | } |
| | | 101 | | |
| | | 102 | | public static string GenerateQrCodeBase64(this string key, string email) |
| | | 103 | | { |
| | | 104 | | const string provider = "Syki"; |
| | | 105 | | |
| | 18 | 106 | | using var qrGenerator = new QRCodeGenerator(); |
| | 18 | 107 | | using var qrCodeData = qrGenerator.CreateQrCode( |
| | 18 | 108 | | $"otpauth://totp/{provider}:{email}?secret={key}&issuer={provider}", |
| | 18 | 109 | | QRCodeGenerator.ECCLevel.Q |
| | 18 | 110 | | ); |
| | | 111 | | |
| | 18 | 112 | | var qrCode = new PngByteQRCode(qrCodeData); |
| | | 113 | | |
| | 18 | 114 | | var bytes = qrCode.GetGraphic(20); |
| | | 115 | | |
| | 18 | 116 | | return string.Format("data:image/png;base64,{0}", Convert.ToBase64String(bytes)); |
| | 18 | 117 | | } |
| | | 118 | | |
| | | 119 | | public static string MinutesToString(this int value) |
| | | 120 | | { |
| | 32 | 121 | | var hours = value / 60; |
| | 32 | 122 | | var minutes = value % 60; |
| | | 123 | | |
| | 34 | 124 | | if (hours == 0 && minutes == 0) return "0"; |
| | 36 | 125 | | if (hours == 0) return $"{minutes}min"; |
| | 40 | 126 | | if (minutes == 0) return $"{hours}h"; |
| | | 127 | | |
| | 8 | 128 | | return $"{hours}h e {minutes}min"; |
| | | 129 | | } |
| | | 130 | | |
| | | 131 | | public static string ToThousandSeparated(this int value) |
| | | 132 | | { |
| | 0 | 133 | | return value.ToString("N0", CultureInfo.CreateSpecificCulture("pt-BR")); |
| | | 134 | | } |
| | | 135 | | |
| | | 136 | | public static string ToTwo(this int value) |
| | | 137 | | { |
| | 0 | 138 | | return value < 10 ? $"0{value}" : value.ToString(); |
| | | 139 | | } |
| | | 140 | | |
| | | 141 | | public static string ToMinuteString(this DateTime date) |
| | | 142 | | { |
| | 0 | 143 | | if (date == DateTime.MinValue) |
| | 0 | 144 | | return "-"; |
| | | 145 | | |
| | 0 | 146 | | return date.ToLocalTime().ToString("dd/MM/yyyy HH:mm:ss"); |
| | | 147 | | } |
| | | 148 | | |
| | | 149 | | public static string ToMinuteString(this DateTime? date) |
| | | 150 | | { |
| | 0 | 151 | | if (date == null) |
| | 0 | 152 | | return "-"; |
| | | 153 | | |
| | 0 | 154 | | return date.Value.ToMinuteString(); |
| | | 155 | | } |
| | | 156 | | |
| | | 157 | | public static string AddQueryString(this string path, object obj) |
| | | 158 | | { |
| | 0 | 159 | | return QueryHelpers.AddQueryString(path, ConvertObjectToDictionary(obj)); |
| | | 160 | | } |
| | | 161 | | |
| | | 162 | | private static Dictionary<string, string?> ConvertObjectToDictionary(object obj) |
| | | 163 | | { |
| | 0 | 164 | | if (obj == null) return []; |
| | | 165 | | |
| | 0 | 166 | | Dictionary<string, string?> dictionary = []; |
| | 0 | 167 | | PropertyInfo[] properties = obj.GetType().GetProperties(); |
| | | 168 | | |
| | 0 | 169 | | foreach (PropertyInfo property in properties) |
| | | 170 | | { |
| | 0 | 171 | | string propertyName = property.Name; |
| | 0 | 172 | | object propertyValue = property.GetValue(obj)!; |
| | | 173 | | |
| | 0 | 174 | | if (propertyValue != null) |
| | | 175 | | { |
| | 0 | 176 | | var valueAsString = propertyValue.ToString(); |
| | | 177 | | |
| | 0 | 178 | | if (property.PropertyType == typeof(DateTime)) |
| | 0 | 179 | | valueAsString = ((DateTime)propertyValue).ToString("yyyy-MM-ddTHH:mm:sszzz"); |
| | | 180 | | |
| | 0 | 181 | | if (property.PropertyType == typeof(DateOnly)) |
| | 0 | 182 | | valueAsString = ((DateOnly)propertyValue).ToString("yyyy-MM-dd"); |
| | | 183 | | |
| | 0 | 184 | | dictionary.Add(propertyName, valueAsString); |
| | | 185 | | } |
| | | 186 | | } |
| | | 187 | | |
| | 0 | 188 | | return dictionary; |
| | | 189 | | } |
| | | 190 | | |
| | | 191 | | public static string ParseJsonString(this string input) |
| | | 192 | | { |
| | 0 | 193 | | if (input.IsEmpty()) return ""; |
| | | 194 | | |
| | | 195 | | try |
| | | 196 | | { |
| | 0 | 197 | | return JToken |
| | 0 | 198 | | .Parse(input) |
| | 0 | 199 | | .ToString(Formatting.Indented); |
| | | 200 | | } |
| | 0 | 201 | | catch |
| | | 202 | | { |
| | 0 | 203 | | return input; |
| | | 204 | | } |
| | 0 | 205 | | } |
| | | 206 | | |
| | | 207 | | public static string GetSqlSpanName(this string sql) |
| | | 208 | | { |
| | 0 | 209 | | var comparer = StringComparison.InvariantCultureIgnoreCase; |
| | 0 | 210 | | var insert = sql.Contains("INSERT", comparer); |
| | 0 | 211 | | var update = sql.Contains("UPDATE", comparer); |
| | 0 | 212 | | var delete = sql.Contains("DELETE", comparer); |
| | 0 | 213 | | var select = sql.Contains("SELECT", comparer); |
| | | 214 | | |
| | 0 | 215 | | var builder = new StringBuilder(); |
| | | 216 | | |
| | 0 | 217 | | if (insert) builder.Append("INSERT "); |
| | 0 | 218 | | if (update) builder.Append("UPDATE "); |
| | 0 | 219 | | if (delete) builder.Append("DELETE"); |
| | | 220 | | |
| | 0 | 221 | | if (!insert && !update && !delete && select) builder.Append("SELECT"); |
| | | 222 | | |
| | 0 | 223 | | return builder.ToString().Trim(); |
| | | 224 | | } |
| | | 225 | | |
| | | 226 | | public static int ToInt(this string value) |
| | | 227 | | { |
| | 400 | 228 | | return int.TryParse(value, out int integer) ? integer : 0; |
| | | 229 | | } |
| | | 230 | | } |